Skip to content

Commit 1442cd5

Browse files
Poc-4 (#4)
* Replace root sys.path * Add --output-dir arg
1 parent 090fe0e commit 1442cd5

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

package_python_function/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ def main() -> None:
88
args = parse_args()
99
project_path = Path(args.project).resolve()
1010
venv_path = Path(args.venv).resolve()
11-
output_path = Path(args.output).resolve()
12-
packager = Packager(venv_path, project_path, output_path)
11+
output_dir_path = Path(args.output_dir).resolve()
12+
output_file_path = Path(args.output).resolve() if args.output else None
13+
packager = Packager(venv_path, project_path, output_dir_path, output_file_path)
1314
packager.package()
1415

1516

1617
def parse_args() -> argparse.Namespace:
1718
arg_parser = argparse.ArgumentParser()
1819
arg_parser.add_argument("venv", type=str)
1920
arg_parser.add_argument("--project", type=str, default='pyproject.toml')
20-
arg_parser.add_argument("--output", type=str, default='.')
21+
arg_parser.add_argument("--output-dir", type=str, default='.')
22+
arg_parser.add_argument("--output", type=str, default='')
2123
return arg_parser.parse_args()

package_python_function/packager.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
class Packager:
1010
AWS_LAMBDA_MAX_UNZIP_SIZE = 262144000
1111

12-
def __init__(self, venv_path: Path, project_path: Path, output_path: Path):
12+
def __init__(self, venv_path: Path, project_path: Path, output_dir: Path, output_file: Path | None):
1313
self.project = PythonProject(project_path)
1414
self.venv_path = venv_path
15-
self.output_path = output_path
16-
self._uncompressed_bytes = 0
1715

18-
@property
19-
def output_file_path(self) -> Path:
20-
if self.output_path.is_dir():
21-
return self.output_path / f'{self.project.name}.zip'
22-
return self.output_path
16+
self.output_dir = output_file.parent if output_file else output_dir
17+
self.output_file = output_file if output_file else output_dir / f'{self.project.name}.zip'
18+
19+
self._uncompressed_bytes = 0
2320

2421
@property
2522
def input_path(self) -> Path:
@@ -30,11 +27,11 @@ def input_path(self) -> Path:
3027

3128
def package(self) -> None:
3229
print("Packaging:", self.project.path)
33-
print("Output:", self.output_file_path)
30+
print("Output:", self.output_file)
3431
print("Input:", self.input_path)
3532
print("Entrypoint Package name:", self.project.entrypoint_package_name)
3633

37-
self.output_file_path.parent.mkdir(parents=True, exist_ok=True)
34+
self.output_dir.mkdir(parents=True, exist_ok=True)
3835

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

7168
def generate_nested_zip(self, inner_zip_path: Path) -> None:
72-
with zipfile.ZipFile(self.output_file_path, 'w') as outer_zip_file:
69+
with zipfile.ZipFile(self.output_file, 'w') as outer_zip_file:
7370
entrypoint_dir = Path(self.project.entrypoint_package_name)
7471
outer_zip_file.write(
7572
inner_zip_path,

0 commit comments

Comments
 (0)