Archive creation, minimal management
This commit is contained in:
parent
2f5c6ec4a8
commit
75b0f5c668
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
|
@ -0,0 +1,65 @@
|
||||||
|
#!/bin/python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
# Top Level Subcommands
|
||||||
|
subparsers = parser.add_subparsers(dest='command',
|
||||||
|
required=True)
|
||||||
|
|
||||||
|
# Archive : Picture Library Management
|
||||||
|
add_archive_parser(subparsers)
|
||||||
|
|
||||||
|
# Run
|
||||||
|
args = parser.parse_args()
|
||||||
|
args.func(args)
|
||||||
|
|
||||||
|
|
||||||
|
def add_archive_parser(top_level_parsers: argparse._SubParsersAction):
|
||||||
|
parser: argparse.ArgumentParser = top_level_parsers.add_parser("archive")
|
||||||
|
subsub = parser.add_subparsers(dest='archive',
|
||||||
|
required=True)
|
||||||
|
|
||||||
|
add = subsub.add_parser("add")
|
||||||
|
add.add_argument("files",
|
||||||
|
help="files to add to archive",
|
||||||
|
default=[],
|
||||||
|
nargs="+")
|
||||||
|
add.add_argument("-a", "--archive",
|
||||||
|
help="path to archive",
|
||||||
|
dest="archive_path",
|
||||||
|
nargs=1,
|
||||||
|
required=True)
|
||||||
|
|
||||||
|
add.add_argument("-p", "--path",
|
||||||
|
help="path inside archive where files will be placed",
|
||||||
|
dest="basepath",
|
||||||
|
default="")
|
||||||
|
add.set_defaults(func=run_archive_add)
|
||||||
|
|
||||||
|
create = subsub.add_parser("create")
|
||||||
|
create.add_argument("-a", "--archive",
|
||||||
|
help="path to archive",
|
||||||
|
dest="archive_path",
|
||||||
|
nargs=1,
|
||||||
|
required=True)
|
||||||
|
create.set_defaults(func=run_archive_create)
|
||||||
|
|
||||||
|
|
||||||
|
def run_archive_add(args):
|
||||||
|
from pictures.add import add
|
||||||
|
add(archive_path=args.archive_path[0],
|
||||||
|
files_to_add=args.files,
|
||||||
|
basepath=args.basepath)
|
||||||
|
|
||||||
|
|
||||||
|
def run_archive_create(args):
|
||||||
|
from pictures.create import create
|
||||||
|
create(args.archive_path[0])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -0,0 +1,46 @@
|
||||||
|
# Util functions for picture archive
|
||||||
|
|
||||||
|
from zipfile import ZipFile, is_zipfile, ZIP_LZMA
|
||||||
|
from os.path import isfile, basename
|
||||||
|
|
||||||
|
|
||||||
|
ARCHIVE_MARKER_FILE = "PICARCHIVE"
|
||||||
|
|
||||||
|
|
||||||
|
def check_valid_archive(path: str) -> bool:
|
||||||
|
if not isfile(path):
|
||||||
|
return False
|
||||||
|
if not is_zipfile(path):
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
with ZipFile(path, 'r') as zip:
|
||||||
|
if ARCHIVE_MARKER_FILE not in zip.namelist():
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def create_new_archive(path: str):
|
||||||
|
if check_valid_archive(path):
|
||||||
|
raise FileExistsError("Archive already exists")
|
||||||
|
if isfile(path):
|
||||||
|
raise FileExistsError("A file with that name already exists")
|
||||||
|
|
||||||
|
with ZipFile(path, 'x',
|
||||||
|
compression=ZIP_LZMA,
|
||||||
|
allowZip64=True) as zip:
|
||||||
|
with zip.open(ARCHIVE_MARKER_FILE, 'w') as f:
|
||||||
|
f.write("horses".encode())
|
||||||
|
|
||||||
|
|
||||||
|
def get_already_existing_files(path: str, files: [str]) -> [str]:
|
||||||
|
if not check_valid_archive(path):
|
||||||
|
raise ValueError("Archive is not valid")
|
||||||
|
|
||||||
|
with ZipFile(path, 'r') as zip:
|
||||||
|
existing_files_set = {basename(p) for p in zip.namelist()}
|
||||||
|
|
||||||
|
arg_files_set = {basename(p) for p in files}
|
||||||
|
return list(existing_files_set & arg_files_set)
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Add function for archive
|
||||||
|
from pictures._util import check_valid_archive, get_already_existing_files
|
||||||
|
from zipfile import ZipFile
|
||||||
|
from os.path import join
|
||||||
|
|
||||||
|
|
||||||
|
def add(archive_path: str, files_to_add: [str],
|
||||||
|
basepath: str = "",
|
||||||
|
error_on_duplicate=False,
|
||||||
|
overwrite_duplicate=False):
|
||||||
|
if not check_valid_archive(archive_path):
|
||||||
|
raise ValueError("Archive is not valid")
|
||||||
|
|
||||||
|
if overwrite_duplicate:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
if error_on_duplicate and len(
|
||||||
|
e := get_already_existing_files(archive_path, files_to_add)) > 0:
|
||||||
|
raise Exception(f"Erroring on duplicate files: {e}")
|
||||||
|
|
||||||
|
with ZipFile(archive_path, 'a') as zip:
|
||||||
|
for file in files_to_add:
|
||||||
|
basepath_corrected_path = join(basepath, file)
|
||||||
|
with (
|
||||||
|
zip.open(basepath_corrected_path, 'w') as zf,
|
||||||
|
open(file, 'rb') as of
|
||||||
|
):
|
||||||
|
zf.write(of.read())
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Create new archive
|
||||||
|
from pictures._util import create_new_archive
|
||||||
|
|
||||||
|
def create(path: str):
|
||||||
|
create_new_archive(path)
|
|
@ -0,0 +1,2 @@
|
||||||
|
Jinja2==3.1.4
|
||||||
|
MarkupSafe==3.0.2
|
Loading…
Reference in New Issue