Exemplo n.º 1
0
    def install_rclone(self):
        import platform
        import zipfile

        rclone_dir = Vim.UserInput(
            'Rclone not in PATH. Install it at (modify/enter)',
            os.path.expanduser('~/rclone'))
        Shell.mkdir(rclone_dir)

        system = platform.system().lower()
        processor = 'amd64'
        if '386' in platform.processor():
            processor = '386'
        else:
            # Should support arm??
            pass

        url = f'https://downloads.rclone.org/rclone-current-{system}-{processor}.zip'
        zip_fname = os.path.join(rclone_dir, 'rclone.zip')
        Shell.urldownload(url, zip_fname)
        zip_ref = zipfile.ZipFile(zip_fname, 'r')
        zip_ref.extractall(rclone_dir)
        for entry in zip_ref.NameToInfo:
            if entry.endswith('rclone'):
                Shell.cp(os.path.join(rclone_dir, entry), rclone_dir)
                Shell.chmod(os.path.join(rclone_dir, 'rclone'), 755)

        zip_ref.close()
        os.remove(zip_fname)

        shellrc = Vim.UserInput(
            'Update PATH in (leave blank to set manually later)',
            Shell.shellrc())
        if len(shellrc) > 0:
            with open(shellrc, 'a') as f:
                f.write(f'PATH={rclone_dir}:$PATH\n')
        os.environ['PATH'] += ':' + rclone_dir
Exemplo n.º 2
0
    def __init__(self, path):
        self.rules = []

        if not os.path.isfile(path):
            Shell.cp(os.path.join(config_dir, 'rifle.conf'), path)

        with open(path, 'r') as f:
            for i, line in enumerate(f):
                try:
                    # remove things after the first # (including)
                    line = line[:line.index('#')]
                except ValueError:
                    pass

                line = line.strip()
                if len(line) == 0:
                    continue
                sp = line.split('=')
                if len(sp) != 2:
                    Vim.ErrorMsg(
                        'invalid rule: rifle.conf line {}. There should be one'
                        ' and only one "=" for each line'.format(i + 1))
                    continue

                tests = []
                for test in sp[0].strip().split(','):
                    testSp = [e for e in test.split(' ') if e != '']
                    tests.append(globals()[testSp[0]](*testSp[1:]))
                command = sp[1].strip()

                # simple case, used specify only the command
                # For sophisicated command like bash -c "command {}"
                # user should add '{}' themselves
                if '{}' not in command:
                    command = command + ' {}'
                self.rules.append((tests, command))