import os import shutil import subprocess import sys import tempfile import boto3 S3_BUCKET = "compute-engine-task-layer" class LayerBuilder: def __init__(self): pass # self.s3 = boto3.client("s3") # self.lmd = boto3.client('lambda') # def upload(self, mod): # print(f"uploading {mod}...") # self.s3.upload_file(f"{mod}.zip", S3_BUCKET, f"{mod}.zip") # print(f"upload {mod} success") # def publish(self, mod): # print(f"publishing {mod}...") # self.lmd.publish_layer_version( # LayerName=mod, # Description="compute engine python layer", # Content={ # "S3Bucket": S3_BUCKET, # "S3Key": f"{mod}.zip", # }, # CompatibleRuntimes=[ # "python3.9", # ], # CompatibleArchitectures=[ # "x86_64", # ], # ) # print(f"publish {mod} success") def build(self, target, mod): dirpath = tempfile.mkdtemp() os.makedirs(os.path.join(dirpath, "python")) if os.path.exists(os.path.join(target, "requirements.txt")): subprocess.check_call( [ sys.executable, "-m", "pip", "install", "-r", os.path.join(target, "requirements.txt"), "--target", os.path.join(dirpath, "python"), ] ) subfolders = [ (f.path, f.name) for f in os.scandir(target) if f.is_dir() and not f.name.startswith(".") ] for folder, name in subfolders: shutil.copytree(folder, os.path.join(dirpath, "python", name)) zfile = shutil.make_archive(mod, "zip", dirpath) shutil.rmtree(dirpath) # self.upload(mod) # os.remove(zfile) # self.publish(mod) if __name__ == "__main__": builder = LayerBuilder() modules = [ (f.path, f.name) for f in os.scandir(os.getcwd()) if f.is_dir() and not f.name.startswith(".") ] for path, mod in modules: builder.build(path, mod)