Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions pymake/pymake.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import argparse
import os
import pathlib as pl
import shutil
import sys
import time
Expand Down Expand Up @@ -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
Expand Down
50 changes: 35 additions & 15 deletions pymake/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
-------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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,
)


Expand Down