Skip to content

Commit e7c5ad4

Browse files
committedJan 9, 2024
refactoring
1 parent 7499cde commit e7c5ad4

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed
 

‎process_executable_file.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
'linux': Template('cycode-linux$suffix$ext'),
2323
'windows': Template('cycode-win$suffix.exe$ext'),
2424
}
25+
_WINDOWS = 'windows'
26+
_WINDOWS_EXECUTABLE_SUFFIX = '.exe'
2527

2628
DirHashes = List[Tuple[str, str]]
2729

@@ -31,21 +33,38 @@ def get_hash_of_file(file_path: Union[str, Path]) -> str:
3133
return hashlib.sha256(f.read()).hexdigest()
3234

3335

34-
def calculate_hash_of_every_file_in_the_directory(dir_path: Path) -> DirHashes:
36+
def get_hashes_of_many_files(root: str, file_paths: List[str]) -> DirHashes:
37+
hashes = []
38+
39+
for file_path in file_paths:
40+
file_path = os.path.join(root, file_path)
41+
file_hash = get_hash_of_file(file_path)
42+
43+
hashes.append((file_hash, file_path))
44+
45+
return hashes
46+
47+
48+
def get_hashes_of_every_file_in_the_directory(dir_path: Path) -> DirHashes:
3549
hashes = []
3650

3751
for root, _, files in os.walk(dir_path):
38-
for file in files:
39-
file_path = os.path.join(root, file)
40-
file_hash = get_hash_of_file(file_path)
52+
hashes.extend(get_hashes_of_many_files(root, files,))
4153

42-
relative_file_path = file_path[file_path.find(dir_path.name):]
43-
hashes.append((file_hash, relative_file_path))
54+
return hashes
55+
56+
57+
def normalize_hashes_db(hashes: DirHashes, dir_path: Path) -> DirHashes:
58+
normalized_hashes = []
59+
60+
for file_hash, file_path in hashes:
61+
relative_file_path = file_path[file_path.find(dir_path.name):]
62+
normalized_hashes.append((file_hash, relative_file_path))
4463

4564
# sort by file path
46-
hashes.sort(key=lambda x: x[1])
65+
normalized_hashes.sort(key=lambda hash_item: hash_item[1])
4766

48-
return hashes
67+
return normalized_hashes
4968

5069

5170
def is_arm() -> bool:
@@ -111,8 +130,9 @@ def process_executable_file(input_path: Path, is_onedir: bool) -> str:
111130
hash_file_path = get_cli_hash_path(output_path, is_onedir)
112131

113132
if is_onedir:
114-
hashes = calculate_hash_of_every_file_in_the_directory(input_path)
115-
write_hashes_db_to_file(hashes, hash_file_path)
133+
hashes = get_hashes_of_every_file_in_the_directory(input_path)
134+
normalized_hashes = normalize_hashes_db(hashes, input_path)
135+
write_hashes_db_to_file(normalized_hashes, hash_file_path)
116136
else:
117137
file_hash = get_hash_of_file(input_path)
118138
write_hash_to_file(file_hash, hash_file_path)
@@ -131,9 +151,9 @@ def main() -> None:
131151
input_path = Path(args.input)
132152
is_onedir = input_path.is_dir()
133153

134-
if get_os_name() == 'windows' and not is_onedir and input_path.suffix != '.exe':
154+
if get_os_name() == _WINDOWS and not is_onedir and input_path.suffix != _WINDOWS_EXECUTABLE_SUFFIX:
135155
# add .exe on windows if was missed (to simplify GHA workflow)
136-
input_path = input_path.with_suffix('.exe')
156+
input_path = input_path.with_suffix(_WINDOWS_EXECUTABLE_SUFFIX)
137157

138158
artifact_name = process_executable_file(input_path, is_onedir)
139159

0 commit comments

Comments
 (0)