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 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'
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) })
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())
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)
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'
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()
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])
def test_get_fileinfo(): dir_info = DirectoryInfo('.') assert isinstance(dir_info.get_fileinfo('a'), FileInfo)