forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-awslambda-layer.py
More file actions
77 lines (64 loc) · 2.44 KB
/
build-awslambda-layer.py
File metadata and controls
77 lines (64 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import subprocess
import tempfile
import shutil
from sentry_sdk.consts import VERSION as SDK_VERSION
DIST_REL_PATH = "dist"
DEST_ABS_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", DIST_REL_PATH)
)
DEST_ZIP_FILENAME = f"sentry-python-serverless-{SDK_VERSION}.zip"
WHEELS_FILEPATH = os.path.join(
DIST_REL_PATH, f"sentry_sdk-{SDK_VERSION}-py2.py3-none-any.whl"
)
# Top directory in the ZIP file. Placing the Sentry package in `/python` avoids
# creating a directory for a specific version. For more information, see
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
PACKAGE_PARENT_DIRECTORY = "python"
class PackageBuilder:
def __init__(self, base_dir) -> None:
self.base_dir = base_dir
self.packages_dir = self.get_relative_path_of(PACKAGE_PARENT_DIRECTORY)
def make_directories(self):
os.makedirs(self.packages_dir)
def install_python_binaries(self):
subprocess.run(
[
"pip",
"install",
"--no-cache-dir", # Disables the cache -> always accesses PyPI
"-q", # Quiet
WHEELS_FILEPATH, # Copied to the target directory before installation
"-t", # Target directory flag
self.packages_dir,
],
check=True,
)
def zip(self, filename):
subprocess.run(
[
"zip",
"-q", # Quiet
"-x", # Exclude files
"**/__pycache__/*", # Files to be excluded
"-r", # Recurse paths
filename, # Output filename
PACKAGE_PARENT_DIRECTORY, # Files to be zipped
],
cwd=self.base_dir,
check=True, # Raises CalledProcessError if exit status is non-zero
)
def get_relative_path_of(self, subfile):
return os.path.join(self.base_dir, subfile)
def build_packaged_zip():
with tempfile.TemporaryDirectory() as tmp_dir:
package_builder = PackageBuilder(tmp_dir)
package_builder.make_directories()
package_builder.install_python_binaries()
package_builder.zip(DEST_ZIP_FILENAME)
if not os.path.exists(DIST_REL_PATH):
os.makedirs(DIST_REL_PATH)
shutil.copy(
package_builder.get_relative_path_of(DEST_ZIP_FILENAME), DEST_ABS_PATH
)
build_packaged_zip()