|
from zipfile import ZipFile |
|
from pathlib import Path |
|
import shutil |
|
|
|
|
|
|
|
def extract(file, prefix): |
|
proj_dir = Path( |
|
f'projects/{prefix}_{file.orig_name.replace(".", "_").replace("@", "_")}' |
|
) |
|
with ZipFile(file, "r") as zip_ref: |
|
zip_ref.extractall(proj_dir) |
|
|
|
return proj_dir |
|
|
|
|
|
def cleanup(proj_dir: Path, files): |
|
""" |
|
Delete all files/folders in prefix that dont have filenames in files |
|
:param proj_dir: |
|
:param files: |
|
:return: |
|
""" |
|
for file in proj_dir.glob("*"): |
|
if file.name not in files: |
|
if file.is_file(): |
|
file.unlink() |
|
else: |
|
shutil.rmtree(file) |
|
|