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