@@ -23,11 +23,11 @@ class LocalLambdaPowertoolsLayer(BaseLocalLambdaLayer):
2323
2424 def __init__ (self , output_dir : Path = CDK_OUT_PATH , architecture : Architecture = Architecture .X86_64 ):
2525 super ().__init__ (output_dir )
26- self .package = f"{ SOURCE_CODE_ROOT_PATH } [all,redis,datamasking]"
26+ self .source_root = SOURCE_CODE_ROOT_PATH
27+ self .extras = "all,redis,datamasking"
2728
2829 self .platform_args = self ._resolve_platform (architecture )
2930 self .build_args = f"{ self .platform_args } --only-binary=:all: --upgrade"
30- self .build_command = f"python -m pip install { self .package } { self .build_args } --target { self .target_dir } "
3131 self .cleanup_command = (
3232 f"rm -rf { self .target_dir } /boto* { self .target_dir } /s3transfer* && "
3333 f"rm -rf { self .target_dir } /*dateutil* { self .target_dir } /urllib3* { self .target_dir } /six* && "
@@ -42,7 +42,23 @@ def build(self) -> str:
4242 self .before_build ()
4343
4444 if self ._has_source_changed ():
45- subprocess .run (self .build_command , shell = True , check = True )
45+ # Build wheel first, then install with platform constraints
46+ dist_dir = self .source_root / "dist"
47+ subprocess .run (f"rm -rf { dist_dir } " , shell = True , check = True )
48+ subprocess .run (
49+ f"python -m pip wheel { self .source_root } --no-deps -w { dist_dir } " ,
50+ shell = True ,
51+ check = True ,
52+ )
53+
54+ # Find the built wheel
55+ wheel_file = next (dist_dir .glob ("*.whl" ))
56+
57+ # Install the wheel with extras and platform constraints
58+ install_cmd = (
59+ f"python -m pip install '{ wheel_file } [{ self .extras } ]' { self .build_args } --target { self .target_dir } "
60+ )
61+ subprocess .run (install_cmd , shell = True , check = True )
4662
4763 self .after_build ()
4864
@@ -52,7 +68,7 @@ def after_build(self):
5268 subprocess .run (self .cleanup_command , shell = True , check = True )
5369
5470 def _has_source_changed (self ) -> bool :
55- """Hashes source code and
71+ """Hashes source code and checks if rebuild is needed.
5672
5773 Returns
5874 -------
@@ -83,5 +99,5 @@ def _resolve_platform(self, architecture: Architecture) -> str:
8399
84100 return self ._build_platform_args (platforms )
85101
86- def _build_platform_args (self , platforms : list [str ]) :
102+ def _build_platform_args (self , platforms : tuple [str , ...]) -> str :
87103 return " " .join ([f"--platform { platform } " for platform in platforms ])
0 commit comments