def extract_file(src_path: Path, dest_path: Path, compression='tar'): """ Extract a compressed file and put completion file in destination folder once complete. Skips extraction if a completion file found in the destination folder :param src_path: Path to a compressed file :param dest_path: Path to put extracted contents :param compression: Either 'tar' or 'zip' """ completion_file = dest_path / '.complete' if completion_file.exists(): logging.info('Expansion of %s already complete', src_path) else: if compression == 'tar': openmode = 'r:gz' if src_path.name.endswith('.tar.gz') else 'r' extractor = tarfile.TarFile.open(str(src_path), mode=openmode) assert completion_file.name not in extractor.getnames() elif compression == 'zip': extractor = zipfile.ZipFile(str(src_path), 'r') else: raise ValueError('Unsupported compression') with extractor: if dest_path.exists(): logging.info('Removing partially expanded %s', dest_path) shutil.rmtree(str(dest_path)) logging.info('Expanding %s', dest_path) dest_path.mkdir() extractor.extractall(str(dest_path)) completion_file.touch() logging.info('Expansion of %s is complete', dest_path)
def get_file_count(path: Path, glob: str) -> int: """ Return the count of files in a folder :param path: A path to a folder to check :param glob: The glob pattern :return: Number of files counted """ if path.exists() and path.is_dir(): return sum([1 for f in path.glob(glob) if f.is_file()]) else: return 0
def create_or_update_symlink(symlink: Path, target: Path): if symlink.is_symlink(): # noinspection PyUnresolvedReferences current_target = symlink.readlink() if current_target == target: return logging.warning('Removing stale symlink from %s to %s.', symlink, current_target) symlink.unlink() elif symlink.exists(): raise RuntimeError( f'Will not overwrite {symlink} with link to {target}') logging.info('Linking %s to %s', symlink, target) symlink.symlink_to(target, target_is_directory=True)