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..ea6d921 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,21 +168,19 @@ def compressall(path, file_pths=None, dir_pths=None, patterns=None): tlist.append(file_pth) file_pths = tlist - # write the zipfile + if append and pl.Path(path).exists(): + mode = "a" + else: + mode = "w" + success = True if len(file_pths) > 0: - zf = ZipFile(path, "w", 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 @@ -471,7 +478,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 +500,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, )