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
8 changes: 5 additions & 3 deletions package_python_function/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def main() -> None:
args = parse_args()
project_path = Path(args.project).resolve()
venv_path = Path(args.venv).resolve()
output_path = Path(args.output).resolve()
packager = Packager(venv_path, project_path, output_path)
output_dir_path = Path(args.output_dir).resolve()
output_file_path = Path(args.output).resolve() if args.output else None
packager = Packager(venv_path, project_path, output_dir_path, output_file_path)
packager.package()


def parse_args() -> argparse.Namespace:
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("venv", type=str)
arg_parser.add_argument("--project", type=str, default='pyproject.toml')
arg_parser.add_argument("--output", type=str, default='.')
arg_parser.add_argument("--output-dir", type=str, default='.')
arg_parser.add_argument("--output", type=str, default='')
return arg_parser.parse_args()
21 changes: 9 additions & 12 deletions package_python_function/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
class Packager:
AWS_LAMBDA_MAX_UNZIP_SIZE = 262144000

def __init__(self, venv_path: Path, project_path: Path, output_path: Path):
def __init__(self, venv_path: Path, project_path: Path, output_dir: Path, output_file: Path | None):
self.project = PythonProject(project_path)
self.venv_path = venv_path
self.output_path = output_path
self._uncompressed_bytes = 0

@property
def output_file_path(self) -> Path:
if self.output_path.is_dir():
return self.output_path / f'{self.project.name}.zip'
return self.output_path
self.output_dir = output_file.parent if output_file else output_dir
self.output_file = output_file if output_file else output_dir / f'{self.project.name}.zip'

self._uncompressed_bytes = 0

@property
def input_path(self) -> Path:
Expand All @@ -30,11 +27,11 @@ def input_path(self) -> Path:

def package(self) -> None:
print("Packaging:", self.project.path)
print("Output:", self.output_file_path)
print("Output:", self.output_file)
print("Input:", self.input_path)
print("Entrypoint Package name:", self.project.entrypoint_package_name)

self.output_file_path.parent.mkdir(parents=True, exist_ok=True)
self.output_dir.mkdir(parents=True, exist_ok=True)

with NamedTemporaryFile() as dependencies_zip:
self.zip_all_dependencies(Path(dependencies_zip.name))
Expand Down Expand Up @@ -66,10 +63,10 @@ def zip_dir(path: Path) -> None:
else:
print(f"TODO Error. The unzipped size it too large for AWS Lambda.")
else:
shutil.copy(str(target_path), str(self.output_file_path))
shutil.copy(str(target_path), str(self.output_file))

def generate_nested_zip(self, inner_zip_path: Path) -> None:
with zipfile.ZipFile(self.output_file_path, 'w') as outer_zip_file:
with zipfile.ZipFile(self.output_file, 'w') as outer_zip_file:
entrypoint_dir = Path(self.project.entrypoint_package_name)
outer_zip_file.write(
inner_zip_path,
Expand Down
Loading