Skip to content

Commit f75e6ed

Browse files
Taniya Mathurgudivt
authored andcommitted
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 15a7434 commit f75e6ed

File tree

1 file changed

+27
-60
lines changed

1 file changed

+27
-60
lines changed

publish.py

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -894,68 +894,35 @@ def _check_requirements_has_idp_common_pkg(self, func_dir):
894894
def _extract_function_name(self, dir_name, template_path):
895895
"""Extract CloudFormation function name from template by matching CodeUri."""
896896
try:
897-
# Create a custom loader that ignores CloudFormation intrinsic functions
898-
class CFLoader(yaml.SafeLoader):
899-
pass
900-
901-
def construct_unknown(loader, node):
902-
if isinstance(node, yaml.ScalarNode):
903-
return loader.construct_scalar(node)
904-
elif isinstance(node, yaml.SequenceNode):
905-
return loader.construct_sequence(node)
906-
elif isinstance(node, yaml.MappingNode):
907-
return loader.construct_mapping(node)
908-
return None
909-
910-
# Add constructors for CloudFormation intrinsic functions
911-
cf_functions = [
912-
"!Ref",
913-
"!GetAtt",
914-
"!Join",
915-
"!Sub",
916-
"!Select",
917-
"!Split",
918-
"!Base64",
919-
"!GetAZs",
920-
"!ImportValue",
921-
"!FindInMap",
922-
"!Equals",
923-
"!And",
924-
"!Or",
925-
"!Not",
926-
"!If",
927-
"!Condition",
928-
]
929-
930-
for func in cf_functions:
931-
CFLoader.add_constructor(func, construct_unknown)
932-
933897
with open(template_path, "r", encoding="utf-8") as f:
934-
template = yaml.load(f, Loader=CFLoader)
935-
936-
if not template or not isinstance(template, dict):
937-
raise Exception(f"Failed to parse YAML template: {template_path}")
898+
lines = f.readlines()
938899

939-
resources = template.get("Resources", {})
940-
for resource_name, resource_config in resources.items():
941-
if (
942-
resource_config
943-
and isinstance(resource_config, dict)
944-
and resource_config.get("Type") == "AWS::Serverless::Function"
945-
):
946-
properties = resource_config.get("Properties", {})
947-
if properties and isinstance(properties, dict):
948-
code_uri = properties.get("CodeUri", "")
949-
if isinstance(code_uri, str):
950-
code_uri = code_uri.rstrip("/")
951-
code_dir = (
952-
code_uri.split("/")[-1] if "/" in code_uri else code_uri
953-
)
954-
if code_dir == dir_name:
955-
return resource_name
956-
raise Exception(
957-
f"No CloudFormation function found for directory {dir_name} in template {template_path}"
958-
)
900+
for i, line in enumerate(lines):
901+
# Look for CodeUri that matches our directory
902+
if "CodeUri:" in line:
903+
code_uri = (
904+
line.split("CodeUri:")[-1].strip().strip("\"'").rstrip("/")
905+
)
906+
code_dir = code_uri.split("/")[-1] if "/" in code_uri else code_uri
907+
908+
if code_dir == dir_name:
909+
# Found matching CodeUri, now look backwards for the resource name
910+
# Look for AWS::Serverless::Function type first
911+
for j in range(i - 1, max(0, i - 50), -1):
912+
if "Type: AWS::Serverless::Function" in lines[j]:
913+
# Found the function type, now look backwards for resource name
914+
for k in range(j - 1, max(0, j - 10), -1):
915+
stripped = lines[k].strip()
916+
# Resource names are at the start of line and end with ':'
917+
if (
918+
stripped
919+
and not stripped.startswith(" ")
920+
and stripped.endswith(":")
921+
):
922+
return stripped.rstrip(":")
923+
break
924+
925+
return dir_name
959926

960927
except Exception as e:
961928
self.console.print(

0 commit comments

Comments
 (0)