Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathprocess_executable_file.py
More file actions
Latest commit
executable file
·188 lines (123 loc) · 5.5 KB
/
Copy pathprocess_executable_file.py
File metadata and controls
executable file
·188 lines (123 loc) · 5.5 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
"""Used in the GitHub Actions workflow (build_executable.yml) to process the executable file.
This script calculates hash and renames executable file depending on the OS, arch, and build mode.
It also creates a file with the hash of the executable file.
It uses SHA256 algorithm to calculate the hash.
It returns the name of the executable file which is used as artifact name.
"""
importargparse
importhashlib
importos
importplatform
importshutil
frompathlibimportPath
fromstringimportTemplate
fromtypingimportUnion
_ARCHIVE_FORMAT='zip'
_HASH_FILE_EXT='.sha256'
_OS_TO_CLI_DIST_TEMPLATE= {
'darwin': Template('cycode-mac$suffix$ext'),
'linux': Template('cycode-linux$suffix$ext'),
'windows': Template('cycode-win$suffix$ext'),
}
_WINDOWS='windows'
_WINDOWS_EXECUTABLE_SUFFIX='.exe'
DirHashes=list[tuple[str, str]]
defget_hash_of_file(file_path: Union[str, Path]) ->str:
withopen(file_path, 'rb') asf:
returnhashlib.sha256(f.read()).hexdigest()
defget_hashes_of_many_files(root: str, file_paths: list[str]) ->DirHashes:
hashes= []
forfile_pathinfile_paths:
file_path=os.path.join(root, file_path)
file_hash=get_hash_of_file(file_path)
hashes.append((file_hash, file_path))
returnhashes
defget_hashes_of_every_file_in_the_directory(dir_path: Path) ->DirHashes:
hashes= []
forroot, _, filesinos.walk(dir_path):
hashes.extend(
get_hashes_of_many_files(
root,
files,
)
)
returnhashes
defnormalize_hashes_db(hashes: DirHashes, dir_path: Path) ->DirHashes:
normalized_hashes= []
forfile_hash, file_pathinhashes:
relative_file_path=file_path[file_path.find(dir_path.name) :]
normalized_hashes.append((file_hash, relative_file_path))
# sort by file path
normalized_hashes.sort(key=lambdahash_item: hash_item[1])
returnnormalized_hashes
defis_arm() ->bool:
returnplatform.machine().lower() in ('arm', 'arm64', 'aarch64')
defget_os_name() ->str:
returnplatform.system().lower()
defget_cli_file_name(suffix: str='', ext: str='') ->str:
os_name=get_os_name()
ifos_namenotin_OS_TO_CLI_DIST_TEMPLATE:
raiseException(f'Unsupported OS: {os_name}')
return_OS_TO_CLI_DIST_TEMPLATE[os_name].substitute(suffix=suffix, ext=ext)
defget_cli_file_suffix(is_onedir: bool) ->str:
suffixes= []
ifis_arm():
suffixes.append('-arm')
ifis_onedir:
suffixes.append('-onedir')
return''.join(suffixes)
defwrite_hash_to_file(file_hash: str, output_path: str) ->None:
withopen(output_path, 'w') asf:
f.write(file_hash)
defwrite_hashes_db_to_file(hashes: DirHashes, output_path: str) ->None:
content=''
forfile_hash, file_pathinhashes:
content+=f'{file_hash}{file_path}\n'
withopen(output_path, 'w') asf:
f.write(content)
defget_cli_filename(is_onedir: bool) ->str:
# onedir is distributed as an archive of a directory, so only onefile carries .exe
ext=_WINDOWS_EXECUTABLE_SUFFIXifget_os_name() ==_WINDOWSandnotis_onedirelse''
returnget_cli_file_name(suffix=get_cli_file_suffix(is_onedir), ext=ext)
defget_cli_path(output_path: Path, is_onedir: bool) ->str:
returnos.path.join(output_path, get_cli_filename(is_onedir))
defget_cli_hash_filename(is_onedir: bool) ->str:
returnget_cli_filename(is_onedir) +_HASH_FILE_EXT
defget_cli_hash_path(output_path: Path, is_onedir: bool) ->str:
returnos.path.join(output_path, get_cli_hash_filename(is_onedir))
defget_cli_archive_filename(is_onedir: bool) ->str:
returnget_cli_filename(is_onedir)
defget_cli_archive_path(output_path: Path, is_onedir: bool) ->str:
returnos.path.join(output_path, get_cli_archive_filename(is_onedir))
defarchive_directory(input_path: Path, output_path: str) ->None:
shutil.make_archive(output_path.removesuffix(f'.{_ARCHIVE_FORMAT}'), _ARCHIVE_FORMAT, input_path)
defprocess_executable_file(input_path: Path, is_onedir: bool) ->str:
output_path=input_path.parent
hash_file_path=get_cli_hash_path(output_path, is_onedir)
ifis_onedir:
hashes=get_hashes_of_every_file_in_the_directory(input_path)
normalized_hashes=normalize_hashes_db(hashes, input_path)
write_hashes_db_to_file(normalized_hashes, hash_file_path)
archived_file_path=get_cli_archive_path(output_path, is_onedir)
archive_directory(input_path, f'{archived_file_path}.{_ARCHIVE_FORMAT}')
shutil.rmtree(input_path)
else:
file_hash=get_hash_of_file(input_path)
write_hash_to_file(file_hash, hash_file_path)
# for example rename cycode-cli to cycode-mac or cycode-mac-arm-onedir
os.rename(input_path, get_cli_path(output_path, is_onedir))
returnget_cli_filename(is_onedir)
defmain() ->None:
parser=argparse.ArgumentParser()
parser.add_argument('input', help='Path to executable or directory')
args=parser.parse_args()
input_path=Path(args.input)
is_onedir=input_path.is_dir()
ifget_os_name() ==_WINDOWSandnotis_onedirandinput_path.suffix!=_WINDOWS_EXECUTABLE_SUFFIX:
# add .exe on windows if was missed (to simplify GHA workflow)
input_path=input_path.with_suffix(_WINDOWS_EXECUTABLE_SUFFIX)
artifact_name=process_executable_file(input_path, is_onedir)
print(artifact_name) # noqa: T201
if__name__=='__main__':
main()