Skip to content

Commit aaad43a

Browse files
author
Taniya Mathur
committed
Fix validate_lambda_builds Mac compatibility
- Replace YAML parsing with simple string-based template parsing - Fix function name extraction to work consistently across all platforms - Resolves issue where Mac machines showed incorrect function names in validation
1 parent 07b8a34 commit aaad43a

1 file changed

Lines changed: 28 additions & 59 deletions

File tree

publish.py

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import hashlib
1414
import json
1515
import os
16-
import platform
1716
import shutil
1817
import subprocess
1918
import sys
@@ -53,7 +52,7 @@ def __init__(self, verbose=False):
5352
self.public = False
5453
self.main_template = "idp-main.yaml"
5554
self.use_container_flag = ""
56-
self.stat_cmd = None
55+
5756
self.s3_client = None
5857
self.cf_client = None
5958
self._is_lib_changed = False
@@ -228,12 +227,6 @@ def setup_environment(self):
228227
self.prefix_and_version = f"{self.prefix}/{self.version}"
229228
self.bucket = f"{self.bucket_basename}-{self.region}"
230229

231-
# Set platform-specific commands
232-
if platform.machine() == "x86_64":
233-
self.stat_cmd = "stat --format='%Y'"
234-
else:
235-
self.stat_cmd = "stat -f %m"
236-
237230
# Set UDOP model path based on region
238231
if self.region == "us-east-1":
239232
self.public_sample_udop_model = "s3://aws-ml-blog-us-east-1/artifacts/genai-idp/udop-finetuning/rvl-cdip/model.tar.gz"
@@ -883,57 +876,33 @@ def _check_requirements_has_idp_common_pkg(self, func_dir):
883876
def _extract_function_name(self, dir_name, template_path):
884877
"""Extract CloudFormation function name from template by matching CodeUri."""
885878
try:
886-
import yaml
887-
888-
# Create a custom loader that ignores CloudFormation intrinsic functions
889-
class CFLoader(yaml.SafeLoader):
890-
pass
891-
892-
def construct_unknown(loader, node):
893-
return None
894-
895-
# Add constructors for CloudFormation intrinsic functions
896-
cf_functions = [
897-
"!Ref",
898-
"!GetAtt",
899-
"!Join",
900-
"!Sub",
901-
"!Select",
902-
"!Split",
903-
"!Base64",
904-
"!GetAZs",
905-
"!ImportValue",
906-
"!FindInMap",
907-
"!Equals",
908-
"!And",
909-
"!Or",
910-
"!Not",
911-
"!If",
912-
"!Condition",
913-
]
914-
915-
for func in cf_functions:
916-
CFLoader.add_constructor(func, construct_unknown)
917-
918-
with open(template_path, "r") as f:
919-
template = yaml.load(f, Loader=CFLoader)
920-
921-
resources = template.get("Resources", {})
922-
for resource_name, resource_config in resources.items():
923-
if (
924-
resource_config
925-
and resource_config.get("Type") == "AWS::Serverless::Function"
926-
):
927-
properties = resource_config.get("Properties", {})
928-
if properties:
929-
code_uri = properties.get("CodeUri", "")
930-
if isinstance(code_uri, str):
931-
code_uri = code_uri.rstrip("/")
932-
code_dir = (
933-
code_uri.split("/")[-1] if "/" in code_uri else code_uri
934-
)
935-
if code_dir == dir_name:
936-
return resource_name
879+
with open(template_path, "r", encoding="utf-8") as f:
880+
lines = f.readlines()
881+
882+
for i, line in enumerate(lines):
883+
# Look for CodeUri that matches our directory
884+
if "CodeUri:" in line:
885+
code_uri = (
886+
line.split("CodeUri:")[-1].strip().strip("\"'").rstrip("/")
887+
)
888+
code_dir = code_uri.split("/")[-1] if "/" in code_uri else code_uri
889+
890+
if code_dir == dir_name:
891+
# Found matching CodeUri, now look backwards for the resource name
892+
# Look for AWS::Serverless::Function type first
893+
for j in range(i - 1, max(0, i - 50), -1):
894+
if "Type: AWS::Serverless::Function" in lines[j]:
895+
# Found the function type, now look backwards for resource name
896+
for k in range(j - 1, max(0, j - 10), -1):
897+
stripped = lines[k].strip()
898+
# Resource names are at the start of line and end with ':'
899+
if (
900+
stripped
901+
and not stripped.startswith(" ")
902+
and stripped.endswith(":")
903+
):
904+
return stripped.rstrip(":")
905+
break
937906

938907
return dir_name
939908

0 commit comments

Comments
 (0)