<commit_before>#!/usr/bin/env python3

import os, getpass

# You can change these settings to your own preference

package_url = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=999&x=id%3Dbikioccmkafdpakkkcpdbppfkghcmihk%26installsource%3Dondemand%26uc'
icon_url = 'https://drive.google.com/uc?export=view&id=0B-sCqfnhKgTLbmdTSEpTaVVuRGM'

install_dir = os.path.dirname(os.path.abspath(__file__))
launcher_file = os.path.join('/home', getpass.getuser(), '.local/share/applications/signal.desktop')
log_file_name = 'install.log'


# Do not make changes below this line, unless you know what you are doing
# ------------------------------------------------------------------------------

import sys, shutil, urllib.request, json, random, textwrap, subprocess, logging, locale, argparse

logging.basicConfig(filename=os.path.join(install_dir, log_file_name), format='%(asctime)s %(levelname)s: %(message)s', level = logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler()) # also log to stderr

def log_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return

    logging.error('Uncaught exception', exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = log_exception

locale.setlocale(locale.LC_ALL, 'C.UTF-8')


class SignalInstaller(object):
    def __init__(self, install_dir, package_url, icon_url, launcher_file, log_file_name, cron):
        logging.info('----------------')
        logging.info('Init')

        self.path = install_dir
        self.package_url = package_url
        self.icon_url = icon_url
        self.launcher_file = launcher_file
        self.cron = cron

        self.icon_file_name = 'signal.png'
        self.package_file_name = 'signal.zip'
        self.log_file_name = log_file_name

    def main(self):

        if self.path != os.path.dirname(os.path.abspath(__file__)):
            if not os.path.exists(self.path):
                os.makedirs(self.path, exist_ok=True)

            shutil.copy(os.path.abspath(__file__), self.path)

        installed_version = self.getInstalledVersion()
        latest_version = self.getLatestVersion()

        if not installed_version or latest_version > installed_version:
            logging.info('New version found, downloading')

            package_file = os.path.join(self.path, self.package_file_name)
            urllib.request.urlretrieve(self.package_url, package_file)

            if latest_version > installed_version:
                self.cleanOldFiles(self.path, [os.path.basename(__file__), self.icon_file_name, self.package_file_name, self.log_file_name])

            self.unpack(package_file)
            os.remove(package_file)

            if not installed_version:
                if self.launcher_file:
                    self.createLauncher()

                if self.cron:
                    self.createCronJob()

        logging.info('Done')

    def unpack(self, file):
        logging.info('Unpacking')

        debug = logging.getLogger().isEnabledFor(logging.DEBUG)
        unzip_cmd = ['unzip', '' if debug else '-q', file, '-d', self.path]

        try:
            output = subprocess.check_output(unzip_cmd,
                                            stderr = subprocess.STDOUT,
                                            universal_newlines = True,
                                            timeout = 10)

            if output:
                logging.info(output)

        except subprocess.TimeoutExpired:
            logging.error('Timeout while unpacking, command was "%s"' % ' '.join(unzip_cmd))

        except subprocess.CalledProcessError as e:                                                                                                   
            if e.returncode == 1:
                logging.warning('Unpacked successfully, but there were some warnings:')
                logging.warning(e.output)
            else:
                logging.error(e.output)

    def createLauncher(self):

        logging.info('Retrieving icon')
        icon_file = os.path.join(self.path, self.icon_file_name)
        urllib.request.urlretrieve(self.icon_url, icon_file)
<commit_msg>Change unpacking mechanism from system unzip to Python zipfile module<commit_after>#!/usr/bin/env python3

import os, getpass

# You can change these settings to your own preference

package_url = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=999&x=id%3Dbikioccmkafdpakkkcpdbppfkghcmihk%26installsource%3Dondemand%26uc'
icon_url = 'https://drive.google.com/uc?export=view&id=0B-sCqfnhKgTLbmdTSEpTaVVuRGM'

install_dir = os.path.dirname(os.path.abspath(__file__))
launcher_file = os.path.join('/home', getpass.getuser(), '.local/share/applications/signal.desktop')
log_file_name = 'install.log'


# Do not make changes below this line, unless you know what you are doing
# ------------------------------------------------------------------------------

import sys, shutil, urllib.request, json, random, textwrap, logging, locale, argparse, zipfile

logging.basicConfig(filename=os.path.join(install_dir, log_file_name), format='%(asctime)s %(levelname)s: %(message)s', level = logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler()) # also log to stderr

def log_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return

    logging.error('Uncaught exception', exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = log_exception

locale.setlocale(locale.LC_ALL, 'C.UTF-8')


class SignalInstaller(object):
    def __init__(self, install_dir, package_url, icon_url, launcher_file, log_file_name, cron):
        logging.info('----------------')
        logging.info('Init')

        self.path = install_dir
        self.package_url = package_url
        self.icon_url = icon_url
        self.launcher_file = launcher_file
        self.cron = cron

        self.icon_file_name = 'signal.png'
        self.package_file_name = 'signal.zip'
        self.log_file_name = log_file_name

    def main(self):

        if self.path != os.path.dirname(os.path.abspath(__file__)):
            if not os.path.exists(self.path):
                os.makedirs(self.path, exist_ok=True)

            shutil.copy(os.path.abspath(__file__), self.path)

        installed_version = self.getInstalledVersion()
        latest_version = self.getLatestVersion()

        if not installed_version or latest_version > installed_version:
            logging.info('New version found, downloading')

            package_file = os.path.join(self.path, self.package_file_name)
            urllib.request.urlretrieve(self.package_url, package_file)

            if latest_version > installed_version:
                self.cleanOldFiles(self.path, [os.path.basename(__file__), self.icon_file_name, self.package_file_name, self.log_file_name])


            self.unpack(package_file)
            os.remove(package_file)

            if not installed_version:
                if self.launcher_file:
                    self.createLauncher()

                if self.cron:
                    self.createCronJob()

        logging.info('Done')

    def unpack(self, file):
        logging.info('Unpacking ' + file)

        with zipfile.ZipFile(file) as z:
            z.extractall(self.path)


    def createLauncher(self):

        logging.info('Retrieving icon')
        icon_file = os.path.join(self.path, self.icon_file_name)
        urllib.request.urlretrieve(self.icon_url, icon_file)

{'fraction_non_alphanumeric': 0.1257959714100065, 'fraction_numerical': 0.004028589993502274, 'mean_word_length': 2.5141552511415526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '700425', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2276, 'n_words': 414}