Coverage for scripts/function_naming.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-07 16:54 +0000

1#!/usr/bin/env python3 

2 

3import hashlib 

4import re 

5 

6 

7HANDLER_PACKAGE_PREFIX = "async_durable_execution_examples." 

8LAMBDA_FUNCTION_NAME_MAX_LENGTH = 64 

9DEFAULT_FUNCTION_NAME_PREFIX_HEADROOM = 16 

10DEFAULT_FUNCTION_NAME_SUFFIX_MAX_LENGTH = ( 

11 LAMBDA_FUNCTION_NAME_MAX_LENGTH - DEFAULT_FUNCTION_NAME_PREFIX_HEADROOM 

12) 

13HASH_LENGTH = 8 

14 

15 

16def to_logical_id(handler_name: str) -> str: 

17 """Convert a handler module name to a CloudFormation logical id.""" 

18 handler_base = handler_name.replace(".handler", "") 

19 words = [word for word in re.split(r"[^A-Za-z0-9]+", handler_base) if word] 

20 return "".join(word[:1].upper() + word[1:] for word in words) 

21 

22 

23def to_function_name_suffix( 

24 handler_name: str, *, max_length: int = DEFAULT_FUNCTION_NAME_SUFFIX_MAX_LENGTH 

25) -> str: 

26 """Convert a handler module name to a Lambda-safe deploy name suffix. 

27 

28 The suffix reserves some room for the runtime prefix used in CI/CD while still 

29 keeping the mapping deterministic for integration tests. 

30 """ 

31 shortened_handler_name = handler_name.removeprefix(HANDLER_PACKAGE_PREFIX) 

32 logical_id = to_logical_id(shortened_handler_name) 

33 if len(logical_id) <= max_length: 

34 return logical_id 

35 

36 truncated_length = max_length - HASH_LENGTH - 1 

37 if truncated_length < 1: 

38 msg = f"Function name suffix max_length must be at least {HASH_LENGTH + 2}" 

39 raise ValueError(msg) 

40 

41 digest = hashlib.sha1(handler_name.encode("utf-8")).hexdigest()[:HASH_LENGTH] 

42 return f"{logical_id[:truncated_length]}-{digest}"