Example #1
0
def pull_gist(gist, dir_info: DirectoryInfo, logger):
    '''pull items from gist to dir.'''
    assert gist and dir_info and logger

    with tempfile.TemporaryDirectory('-gistsync') as tempdir_name:
        tempdir_info = DirectoryInfo(tempdir_name)
        config_builder = ConfigBuilder(gist)

        for gist_file in gist.files.values():
            tempfile_info = tempdir_info.get_fileinfo(gist_file.filename)
            response = requests.get(gist_file.raw_url)
            try:
                tempfile_info.write_bytes(response.content)
                config_builder.add_file(tempfile_info)
            except OSError as err:
                # some filename only work on linux.
                logger.error(f'cannot sync gist: {err}')
                return
        config_builder.dump(tempdir_info)

        dir_info.ensure_created()
        for item in dir_info.list_items():
            item.delete()

        for item in tempdir_info.list_items():
            assert isinstance(item, FileInfo)
            file_info = dir_info.get_fileinfo(item.path.name)
            item.copy_to(file_info.path)

    logger.info('local updated from remote')
def remove(value):
    '''remove value from folder or file name.'''
    if not value:
        raise RuntimeException('length of value must > 0.')
    folder = DirectoryInfo(os.getcwd())
    print('execute remove "%s"' % (value))
    for item in folder.list_items():
        _rename_by_name(item, _get_name(item).replace(value, ''))
Example #3
0
def _list_python_scripts(pack_root):
    filelist = []
    root_dir = DirectoryInfo(pack_root)
    for f in [x for x in root_dir.list_items(depth=100) if isinstance(x, FileInfo)]:
        if not f.path.ext.equals('.py'):
            continue
        path = str(f.path)
        filelist.append(path)
    return filelist
def _take(width, callback):
    if width <= 0:
        raise RuntimeException('width should > 0.')
    folder = DirectoryInfo(os.getcwd())
    for item in folder.list_items():
        name = _get_name(item)
        if len(name) > width:
            _rename_by_name(item, callback(name))
        else:
            print('ignored ' + name)
def repair_full_angle():
    '''repair filename include url.'''
    folder = DirectoryInfo(os.getcwd())
    for item in folder.list_items():
        name = _get_name(item)
        for replacement in [
                ('0', '0'), ('1', '1'), ('2', '2'),
                ('3', '3'), ('4', '4'), ('5', '5'),
                ('6', '6'), ('7', '7'), ('8', '8'),
                ('9', '9'), ('(', '('), (')', ')'),
            ]:
            name = name.replace(replacement[0], replacement[1])
        _rename_by_name(item, name)
Example #6
0
def get_files(dir_info: DirectoryInfo, config_builder: ConfigBuilder, logger):
    '''get the files as a dict which use as argument for github api.'''

    update_content = {}
    for item in dir_info.list_items():
        if not isinstance(item, FileInfo):
            continue
        if item.path.name == GIST_CONFIG_NAME:
            continue
        if isinstance(item, FileInfo):
            update_content[item.path.name] = github.InputFileContent(item.read_text())
            config_builder.add_file(item)

    return update_content
Example #7
0
def check_local_changed(config: dict, dir_info: DirectoryInfo):
    '''
    check whether the gist dir is changed.
    return True if changed.
    '''
    config_files = config['files']

    files = [z for z in dir_info.list_items() if isinstance(z, FileInfo)] # only files
    if len(files) != len(config_files) + 1:
        # has one file named `.gist.json`
        return True

    for file in config_files:
        file_path = os.path.join(dir_info.path, file['name'])
        if not os.path.isfile(file_path):
            return True
        if compute_sha1(file_path) != file['sha1']:
            return True