Exemplo n.º 1
0
 def resolve_from_dir(node: DirectoryInfo):
     init = node.get_fileinfo('__init__.py')
     if init.is_file():
         store['pkg_root'] = init.path
         pkg_parts.append(node.path.name)
         name_parts.append(node.path.name)
         resolve_from_dir(node.get_parent())
Exemplo n.º 2
0
 def _find_pipfile(dir: DirectoryInfo, deep: int):
     if deep == 0:
         return None
     pf = dir.get_fileinfo('Pipfile')
     if pf.is_file():
         return pf.path
     return _find_pipfile(dir.get_parent(), deep - 1)
Exemplo n.º 3
0
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, ''))
Exemplo n.º 4
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
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
    def __init__(self, ctx: click.Context, root=None):
        if root is None:
            cwd = Path.from_cwd()
        else:
            dirinfo =DirectoryInfo(root)
            if not dirinfo.is_directory():
                ctx.fail(f'{root} is not a dir')
            cwd = dirinfo.path

        from .core.ioc import pkgit_ioc
        pkgit_ioc.register_value('cwd', cwd)
Exemplo n.º 7
0
def browse():
    '''
    open browser and goto pypi website to find this package.
    '''
    setup_file_info = DirectoryInfo('.').get_fileinfo('setup.py')
    if not setup_file_info.is_file():
        click.echo('no package found.')
        return
    attrs = get_setup_attrs(setup_file_info.path)
    package_name = attrs['name']
    setup_cli = NamedSetupCli(package_name)
    setup_cli.browse()
Exemplo n.º 8
0
def test_get_tree():
    with tempfile.TemporaryDirectory() as tmpdir:
        tree = {
            'a.txt': b'abc',
            'b.txt': b'cde',
            'subdir': {
                'e.txt': b'ddd'
            }
        }
        dir_info = DirectoryInfo(tmpdir)
        dir_info.make_tree(tree)

        assert dir_info.get_tree() == tree
Exemplo n.º 9
0
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)
Exemplo n.º 10
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')
Exemplo n.º 11
0
 def dump(self, dir_info: DirectoryInfo):
     file_info = dir_info.get_fileinfo(GIST_CONFIG_NAME)
     file_info.dump({
         'id': self.gist.id,
         'files': self._files,
         'snapver': get_gist_version(self.gist)
     })
Exemplo n.º 12
0
def input_scripts(defval, **kwargs):
    '''
    return a list of .py file.
    each files will be copy to python script directory and make it available for general use.
    '''
    filelist = []
    for package in [x.path.name for x in DirectoryInfo('.').list_items() if isinstance(x, DirectoryInfo)]:
        filelist.extend(_list_python_scripts(package))

    if not filelist:
        logger.error('no python files was founds.')
        return

    if defval:
        filelist = [x for x in filelist if x not in defval]

    def pick():
        print(yellow('[?]'), 'please pick a file that will be copy to python script directory:')
        idx = pick_item(filelist)
        if idx == -1:
            return

        defval.append(filelist.pop(idx))
        return _pick_more('scripts')

    while pick():
        pass

    return defval
Exemplo n.º 13
0
def remove_expired_files(path: str, before: datetime):
    path = resolve_path(path)

    dir_info = DirectoryInfo(path)
    if dir_info.is_directory():
        for item in dir_info.iter_items(depth=99999):
            if item.node_type == NodeType.file:
                atime_int = os.path.getatime(item.path)
                access_time = datetime.fromtimestamp(atime_int)
                if before > access_time:
                    item.delete()
                    click.echo(click.style('Removed', fg='yellow') + ' ',
                               nl=False)
                else:
                    click.echo(click.style('Skiped', fg='cyan') + '  ',
                               nl=False)
                click.echo(f'{item.path!s}.')
Exemplo n.º 14
0
def test_make_tree_with_stream():
    with tempfile.TemporaryDirectory() as tmpdir:
        tree = {
            'a.txt': b'abc',
            'b.txt': b'cde',
            'subdir': {
                'e.txt': b'ddd'
            }
        }
        dir_info = DirectoryInfo(tmpdir)
        dir_info.make_tree(tree)

        with dir_info.get_tree(as_stream=True) as stree:
            with tempfile.TemporaryDirectory() as tmpdir2:
                dir_info_2 = DirectoryInfo(tmpdir2)
                dir_info_2.make_tree(stree)
                assert dir_info_2.get_tree() == tree
Exemplo n.º 15
0
def test_get_unique_name():
    with tempfile.TemporaryDirectory() as tmpdir:
        dir_info = DirectoryInfo(tmpdir)
        name = 'abc'

        unique_name = dir_info.get_unique_name(name, '.py')
        assert unique_name == 'abc.py'

        dir_info.get_fileinfo(unique_name).write_text('')
        unique_name = dir_info.get_unique_name(name, '.py')
        assert unique_name == 'abc (1).py'

        dir_info.get_fileinfo(unique_name).write_text('')
        unique_name = dir_info.get_unique_name(name, '.py')
        assert unique_name == 'abc (2).py'
Exemplo n.º 16
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
Exemplo n.º 17
0
def test_load_context(data, lock: bool, atomic: bool):
    with tempfile.TemporaryDirectory() as tmpdir:
        dir_info = DirectoryInfo(tmpdir)
        file_info = dir_info.get_fileinfo('test_load_context.json')

        # test create
        assert not file_info.is_exists()
        with file_info.load_context(lock=lock, atomic=atomic) as s:
            assert s.data is None
            assert not file_info.is_exists()
            s.data = data
        assert file_info.is_exists()

        # test read and write
        with file_info.load_context(lock=lock, atomic=atomic) as s:
            assert s.data == data
            s.data = {}
        with file_info.load_context(lock=lock, atomic=atomic) as s:
            assert s.data == {}

        # test remove
        with file_info.load_context(lock=lock, atomic=atomic) as s:
            s.data = None
        assert not file_info.is_exists()
Exemplo n.º 18
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
Exemplo n.º 19
0
def test_load_context_with_error(data, lock, atomic):
    with tempfile.TemporaryDirectory() as tmpdir:
        file_info = DirectoryInfo(tmpdir).get_fileinfo('a.json')
        assert not file_info.is_exists()

        with file_info.load_context(lock=lock, atomic=atomic) as s:
            assert s.data is None
            s.data = data
        assert file_info.is_exists()

        with contextlib.suppress(ValueError):
            with file_info.load_context(lock=lock, atomic=atomic) as s:
                assert s.data == data
                s.data = {}
                raise ValueError  # raise any error

        with file_info.load_context(lock=lock, atomic=atomic) as s:
            assert s.data == data  # still not changes
Exemplo n.º 20
0
def push_gist(gist, dir_info: DirectoryInfo, logger):
    '''push items from dir to gist.'''
    assert gist and dir_info and logger

    added, deled = dir_info.get_diff_files()

    config_builder = ConfigBuilder(gist)
    update_content = get_files(dir_info, config_builder, logger)
    for name in deled:
        update_content[name] = None
    gist.edit(files=update_content)
    config_builder.dump(dir_info)

    logger.info('remote updated.')
    if added:
        z = ', '.join(added)
        logger.info(f'remote added files: {z}')
    if deled:
        z = ', '.join(deled)
        logger.info(f'remote deled files: {z}')
Exemplo n.º 21
0
def test_make_tree():
    with tempfile.TemporaryDirectory() as tmpdir:
        dir_info = DirectoryInfo(tmpdir)
        dir_info.make_tree({
            'a.txt': 'abc',
            'b.txt': b'cde',
            'subdir': {
                'e.txt': 'ddd'
            }
        })

        assert dir_info.get_fileinfo('a.txt').read_text() == 'abc'
        assert dir_info.get_fileinfo('b.txt').read_text() == 'cde'

        subdir = dir_info.get_dirinfo('subdir')
        assert subdir.is_directory()
        assert subdir.get_fileinfo('e.txt').read_text() == 'ddd'
Exemplo n.º 22
0
def test_dump_load_with_format(data, format, ext):
    with tempfile.TemporaryDirectory() as tmpdir:
        file_info = DirectoryInfo(tmpdir).get_fileinfo(f'data' + ext)
        file_info.dump(data, format)
        assert data == file_info.load(format)
Exemplo n.º 23
0
def test_dump_load_with_ext(data, ext):
    with tempfile.TemporaryDirectory() as tmpdir:
        file_info = DirectoryInfo(tmpdir).get_fileinfo(f'data_.{ext}')
        file_info.dump(data)
        assert data == file_info.load()
Exemplo n.º 24
0
def test_dir_node_type():
    assert DirectoryInfo('.').node_type is NodeType.dir
Exemplo n.º 25
0
def test_node_get_parent_posix():
    # parent of root is None
    top_dir = DirectoryInfo('/')
    assert top_dir.get_parent() is None
Exemplo n.º 26
0
def test_get_file_hash():
    with tempfile.TemporaryDirectory() as tmpdir:
        fi = DirectoryInfo(tmpdir).get_fileinfo('tmp.txt')
        fi.write_bytes(b'd1s5a')
        hashs = fi.get_file_hash('crc32', 'md5', 'sha1')
        assert hashs == ('2c34fc25', 'f8fc4601b857c7acc459f7118fbca878', 'fe0b025ab8735a2a7bd431b249b42e888e2df1f6')
Exemplo n.º 27
0
def test_get_fileinfo():
    dir_info = DirectoryInfo('.')
    assert isinstance(dir_info.get_fileinfo('a'), FileInfo)
Exemplo n.º 28
0
 def _get_exists_or_first(self, root: DirectoryInfo, names: tuple):
     for name in names:
         fi = root.get_fileinfo(name)
         if fi.is_file():
             return fi
     return root.get_fileinfo(names[0])
Exemplo n.º 29
0
def test_get_dirinfo():
    dir_info = DirectoryInfo('.')
    assert isinstance(dir_info.get_dirinfo('a'), DirectoryInfo)