From c0b27272152a5177f1b7867a2463e11ab79b995f Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Wed, 14 Feb 2024 14:48:35 -0600 Subject: [PATCH 1/2] feat(zip): add feature to allow appending to an existing zip file --- pymake/pymake.py | 35 ++++++++++++++++++++++++----------- pymake/utils/download.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/pymake/pymake.py b/pymake/pymake.py index ac84737..22ab447 100644 --- a/pymake/pymake.py +++ b/pymake/pymake.py @@ -54,6 +54,7 @@ import argparse import os +import pathlib as pl import shutil import sys import time @@ -256,29 +257,41 @@ def compress_targets(self): targets.append(target) # add code.json - if "code.json" not in targets: - targets.append("code.json") + if pl.Path("code.json").exists(): + if "code.json" not in targets: + targets.append("code.json") # delete the zip file if it exists if os.path.exists(zip_pth): - if self.verbose: - msg = f"Deleting existing zipfile '{zip_pth}'" - print(msg) - os.remove(zip_pth) + if self.keep: + if self.verbose: + print( + "Appending files to existing " + + f"zipfile '{zip_pth}'" + ) + else: + if self.verbose: + print(f"Deleting existing zipfile '{zip_pth}'") + os.remove(zip_pth) # print a message describing the zip process if self.verbose: - msg = ( + print( f"Compressing files in '{appdir}' " + f"directory to zip file '{zip_pth}'" ) - print(msg) for idx, target in enumerate(targets): - msg = f" {idx + 1:>3d}. adding " + f"'{target}' to zipfile" - print(msg) + print( + f" {idx + 1:>3d}. adding " + f"'{target}' to zipfile" + ) # compress the compiled executables - if not zip_all(zip_pth, dir_pths=appdir, patterns=targets): + if not zip_all( + zip_pth, + dir_pths=appdir, + patterns=targets, + append=self.keep, + ): self.returncode = 1 return diff --git a/pymake/utils/download.py b/pymake/utils/download.py index c11df28..878ab02 100644 --- a/pymake/utils/download.py +++ b/pymake/utils/download.py @@ -95,7 +95,13 @@ def extractall(self, path=None, members=None, pwd=None): self.extract(zipinfo, path, pwd) @staticmethod - def compressall(path, file_pths=None, dir_pths=None, patterns=None): + def compressall( + path, + file_pths=None, + dir_pths=None, + patterns=None, + append=False, + ): """Compress selected files or files in selected directories. Parameters @@ -108,6 +114,9 @@ def compressall(path, file_pths=None, dir_pths=None, patterns=None): directory paths to include in the output zip file (default is None) patterns : str or list of str file patterns to include in the output zip file (default is None) + append : bool + boolean indicating if file paths should be appended to an existing + zip file Returns ------- @@ -159,10 +168,15 @@ def compressall(path, file_pths=None, dir_pths=None, patterns=None): tlist.append(file_pth) file_pths = tlist + if append and pl.Path(path).exists(): + mode = "a" + else: + mode = "w" + # write the zipfile success = True if len(file_pths) > 0: - zf = ZipFile(path, "w", ZIP_DEFLATED) + zf = ZipFile(path, mode=mode, compression=ZIP_DEFLATED) # write files to zip file for file_pth in file_pths: @@ -471,7 +485,13 @@ def download_and_unzip( return success -def zip_all(path, file_pths=None, dir_pths=None, patterns=None): +def zip_all( + path, + file_pths=None, + dir_pths=None, + patterns=None, + append=False, +): """Compress all files in the user-provided list of file paths and directory paths that match the provided file patterns. @@ -487,13 +507,20 @@ def zip_all(path, file_pths=None, dir_pths=None, patterns=None): patterns : str or list file pattern or list of file patterns s to match to when creating a list of files that will be compressed + append : bool + boolean indicating if file paths should be appended to an existing + zip file Returns ------- """ return pymakeZipFile.compressall( - path, file_pths=file_pths, dir_pths=dir_pths, patterns=patterns + path, + file_pths=file_pths, + dir_pths=dir_pths, + patterns=patterns, + append=append, ) From 9790e678c441086cfe3ea75f64ac9e03bf53e05a Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Wed, 14 Feb 2024 15:41:42 -0600 Subject: [PATCH 2/2] * codeacy fix --- pymake/utils/download.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pymake/utils/download.py b/pymake/utils/download.py index 878ab02..ea6d921 100644 --- a/pymake/utils/download.py +++ b/pymake/utils/download.py @@ -173,21 +173,14 @@ def compressall( else: mode = "w" - # write the zipfile success = True if len(file_pths) > 0: - zf = ZipFile(path, mode=mode, compression=ZIP_DEFLATED) - - # write files to zip file - for file_pth in file_pths: - arcname = os.path.basename(file_pth) - zf.write(file_pth, arcname=arcname) - - # close the zip file - zf.close() + with ZipFile(path, mode=mode, compression=ZIP_DEFLATED) as zf: + for file_pth in file_pths: + arcname = os.path.basename(file_pth) + zf.write(file_pth, arcname=arcname) else: - msg = "No files to add to the zip file" - print(msg) + print("No files to add to the zip file") success = False return success