22
22
'linux' : Template ('cycode-linux$suffix$ext' ),
23
23
'windows' : Template ('cycode-win$suffix.exe$ext' ),
24
24
}
25
+ _WINDOWS = 'windows'
26
+ _WINDOWS_EXECUTABLE_SUFFIX = '.exe'
25
27
26
28
DirHashes = List [Tuple [str , str ]]
27
29
@@ -31,21 +33,38 @@ def get_hash_of_file(file_path: Union[str, Path]) -> str:
31
33
return hashlib .sha256 (f .read ()).hexdigest ()
32
34
33
35
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 :
35
49
hashes = []
36
50
37
51
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 ,))
41
53
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 ))
44
63
45
64
# sort by file path
46
- hashes .sort (key = lambda x : x [1 ])
65
+ normalized_hashes .sort (key = lambda hash_item : hash_item [1 ])
47
66
48
- return hashes
67
+ return normalized_hashes
49
68
50
69
51
70
def is_arm () -> bool :
@@ -111,8 +130,9 @@ def process_executable_file(input_path: Path, is_onedir: bool) -> str:
111
130
hash_file_path = get_cli_hash_path (output_path , is_onedir )
112
131
113
132
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 )
116
136
else :
117
137
file_hash = get_hash_of_file (input_path )
118
138
write_hash_to_file (file_hash , hash_file_path )
@@ -131,9 +151,9 @@ def main() -> None:
131
151
input_path = Path (args .input )
132
152
is_onedir = input_path .is_dir ()
133
153
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 :
135
155
# 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 )
137
157
138
158
artifact_name = process_executable_file (input_path , is_onedir )
139
159
0 commit comments