in muss/utils/resources.py [0:0]
def extract(filepath, output_dir):
output_dir = Path(output_dir)
# Infer extract function based on extension
extensions_to_functions = {
'.tar.gz': untar,
'.tar.bz2': untar,
'.tgz': untar,
'.zip': unzip,
'.gz': ungzip,
'.bz2': unbz2,
}
def get_extension(filename, extensions):
possible_extensions = [ext for ext in extensions if filename.endswith(ext)]
if len(possible_extensions) == 0:
raise Exception(f'File {filename} has an unknown extension')
# Take the longest (.tar.gz should take precedence over .gz)
return max(possible_extensions, key=lambda ext: len(ext))
filename = os.path.basename(filepath)
extension = get_extension(filename, list(extensions_to_functions))
extract_function = extensions_to_functions[extension]
# Extract files in a temporary dir then move the extracted item back to
# the ouput dir in order to get the details of what was extracted
tmp_extract_dir = Path(tempfile.mkdtemp())
# Extract
extract_function(filepath, output_dir=tmp_extract_dir)
extracted_items = os.listdir(tmp_extract_dir)
output_paths = []
for name in extracted_items:
extracted_path = tmp_extract_dir / name
output_path = output_dir / name
move_with_overwrite(extracted_path, output_path)
output_paths.append(output_path)
return output_paths