From 1fbbaca5d793adf46d23cd154ebb29bbba375f7c Mon Sep 17 00:00:00 2001 From: Keplyx Date: Sun, 22 Apr 2018 19:04:05 +0200 Subject: [PATCH] Added album compression script --- photos/compress.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 photos/compress.py diff --git a/photos/compress.py b/photos/compress.py new file mode 100644 index 0000000..a6b7214 --- /dev/null +++ b/photos/compress.py @@ -0,0 +1,78 @@ +#!/usr/bin/python + +import os +import sys +from zipfile import ZipFile, ZIP_DEFLATED + + +def zip_dir(path): + """ + Compress files in the specified directory and sub-directories + Create one zip per folder + + :param path: directory to get files in + :return: + """ + for root, dirs, files in os.walk(path): + if is_directory_valid(root, len(files)): + print("Compressing '" + root + "'", end="") + with ZipFile(os.path.join(root, get_current_dir(root)) + ".zip", "w", ZIP_DEFLATED) as zip_file: + for fn in files: + if not fn.endswith("zip"): + print(".", end="") + absolute_file_name = os.path.join(root, fn) + zippped_file_name = absolute_file_name[len(root)+len(os.sep):] # XXX: relative path + zip_file.write(absolute_file_name, zippped_file_name) + print("done") + print("COMPRESSION FINISHED") + + +def is_directory_valid(path, files_number): + """ + Check if the given path is not hidden or emptty + :param files_number: number of files in the folder + :param path: Path to check + :return: True if path contains a hidden folder or is empty, False otherwise + """ + directories = path.split(os.sep) + valid = files_number > 0 + for dn in directories: + if dn.startswith(".") or not valid: + valid = False + break + return valid + + +def get_current_dir(path): + """ + Get the name of the current directory + :param path: Path to search the name in + :return: directory name + """ + return os.path.basename(os.path.normpath(path)) + + +def get_confirmation(path): + """ + Tell the user which folders will be compressed, and asks for confirmation + :param path: Root path for search + :return: True if user confirmed, False otherwise + """ + print("The following folders will be compressed (hidden and empty folders are ignored):") + for root, dirs, files in os.walk(path): + if is_directory_valid(root, len(files)): + print(root) + confirmation = input("Are you sure you want to proceed? [Y/n]") + return confirmation == "Y" or confirmation == "y" + + +if __name__ == '__main__': + # Get path from arguments or use the script's path + if len(sys.argv) > 1: + directory = sys.argv[1] + else: + directory = os.path.dirname(os.path.realpath(__file__)) + if get_confirmation(directory): + zip_dir(directory) + else: + print("Compression aborted")