| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #!/usr/bin/env python3
- """打包 Lambda 函数为 zip,跨平台 (Windows/Linux/Mac)。"""
- import os
- import sys
- import shutil
- import zipfile
- import subprocess
- SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
- LAMBDAS_DIR = os.path.join(SCRIPT_DIR, '..', 'lambdas')
- LAMBDAS_DIR = os.path.normpath(LAMBDAS_DIR)
- FUNCTIONS = ['submit', 'download', 'transcribe_start', 'transcribe_check', 'summarize', 'notify']
- def build_layer():
- """打包 Lambda Layer (requests + pycryptodome)"""
- print("Building Lambda layer...")
- layer_dir = os.path.join(LAMBDAS_DIR, 'layer_build', 'python')
- layer_zip = os.path.join(LAMBDAS_DIR, 'layer.zip')
- if os.path.exists(os.path.join(LAMBDAS_DIR, 'layer_build')):
- shutil.rmtree(os.path.join(LAMBDAS_DIR, 'layer_build'))
- os.makedirs(layer_dir)
- # 全部指定 Linux 平台安装,确保 native 包兼容 Lambda
- subprocess.check_call([
- sys.executable, '-m', 'pip', 'install',
- 'requests', 'pycryptodome', 'openai', 'tiktoken',
- '--platform', 'manylinux2014_x86_64',
- '--implementation', 'cp',
- '--python-version', '3.12',
- '--only-binary=:all:',
- '-t', layer_dir, '-q',
- ])
- with zipfile.ZipFile(layer_zip, 'w', zipfile.ZIP_DEFLATED) as zf:
- for root, dirs, files in os.walk(os.path.join(LAMBDAS_DIR, 'layer_build')):
- for f in files:
- full = os.path.join(root, f)
- arcname = os.path.relpath(full, os.path.join(LAMBDAS_DIR, 'layer_build'))
- zf.write(full, arcname)
- shutil.rmtree(os.path.join(LAMBDAS_DIR, 'layer_build'))
- size = os.path.getsize(layer_zip) / 1024 / 1024
- print(f" layer.zip ({size:.1f} MB)")
- def build_function(name):
- """打包单个 Lambda 函数"""
- zip_path = os.path.join(LAMBDAS_DIR, f'{name}.zip')
- with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
- zf.write(os.path.join(LAMBDAS_DIR, f'{name}.py'), f'{name}.py')
- zf.write(os.path.join(LAMBDAS_DIR, 'shared.py'), 'shared.py')
- size = os.path.getsize(zip_path) / 1024
- print(f" {name}.zip ({size:.1f} KB)")
- def main():
- print(f"Lambda 目录: {LAMBDAS_DIR}\n")
- build_layer()
- print()
- for fn in FUNCTIONS:
- build_function(fn)
- print("\n完成!")
- if __name__ == '__main__':
- main()
|