-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdependency_hash_generator.py
More file actions
121 lines (104 loc) · 4.12 KB
/
dependency_hash_generator.py
File metadata and controls
121 lines (104 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""Utility Class for Getting Function or Layer Manifest Dependency Hashes"""
import hashlib
import pathlib
from typing import Any, Optional
from samcli.lib.build.workflow_config import get_workflow_config
from samcli.lib.utils.hash import file_checksum
# Mapping of dependency managers to their lock file names
LOCK_FILE_MAPPING = {
"npm": "package-lock.json",
"npm-esbuild": "package-lock.json",
"bundler": "Gemfile.lock",
"gradle": "gradle.lockfile",
"cli-package": "packages.lock.json",
"modules": "go.sum",
"cargo": "Cargo.lock",
"uv": "uv.lock",
"poetry": "poetry.lock",
}
# TODO Expand this class to hash specific sections of the manifest
class DependencyHashGenerator:
_code_uri: str
_base_dir: str
_code_dir: str
_runtime: str
_manifest_path_override: Optional[str]
_hash_generator: Any
_calculated: bool
_hash: Optional[str]
def __init__(
self,
code_uri: str,
base_dir: str,
runtime: str,
manifest_path_override: Optional[str] = None,
hash_generator: Any = None,
):
"""
Parameters
----------
code_uri : str
Relative path specified in the function/layer resource
base_dir : str
Absolute path which the function/layer dir is located
runtime : str
Runtime of the function/layer
manifest_path_override : Optional[str], optional
Override default manifest path for each runtime, by default None
hash_generator : Any, optional
Hash generation function. Can be hashlib.md5(), hashlib.sha256(), etc, by default None
"""
self._code_uri = code_uri
self._base_dir = base_dir
self._code_dir = str(pathlib.Path(self._base_dir, self._code_uri).resolve())
self._runtime = runtime
self._manifest_path_override = manifest_path_override
self._hash_generator = hash_generator
self._calculated = False
self._hash = None
def _calculate_dependency_hash(self) -> Optional[str]:
"""Calculate the manifest file hash, including lock file if applicable
Returns
-------
Optional[str]
Returns combined hash of manifest and lock file (if present).
If manifest does not exist or not supported, None will be returned.
"""
if self._manifest_path_override:
manifest_file = self._manifest_path_override
config = None
else:
config = get_workflow_config(self._runtime, self._code_dir, self._base_dir)
manifest_file = config.manifest_name
if not manifest_file:
return None
manifest_path = pathlib.Path(self._code_dir, manifest_file).resolve()
if not manifest_path.is_file():
return None
manifest_hash = file_checksum(str(manifest_path), hash_generator=self._hash_generator)
# Check if there's a lock file for this dependency manager
if config and config.dependency_manager in LOCK_FILE_MAPPING:
lock_file_name = LOCK_FILE_MAPPING[config.dependency_manager]
lock_file_path = pathlib.Path(self._code_dir, lock_file_name).resolve()
# If lock file exists, combine hashes
if lock_file_path.is_file():
lock_file_hash = file_checksum(str(lock_file_path), hash_generator=self._hash_generator)
# Combine both hashes into a single hash
combined = f"{manifest_hash}:{lock_file_hash}"
hasher = self._hash_generator() if self._hash_generator else hashlib.md5()
hasher.update(combined.encode("utf-8"))
return hasher.hexdigest()
return manifest_hash
@property
def hash(self) -> Optional[str]:
"""
Returns
-------
Optional[str]
Hash for dependencies in the manifest.
If the manifest does not exist or not supported, this value will be None.
"""
if not self._calculated:
self._hash = self._calculate_dependency_hash()
self._calculated = True
return self._hash