-
Notifications
You must be signed in to change notification settings - Fork 47
CM-30406 - Attach onedir CLI to GitHub releases with checksums #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarshalX
merged 8 commits into
main
from
CM-30406-attach-onedir-cli-to-git-hub-releases-with-checksums
Jan 9, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f0aa1b
CM-30406 - Attach onedir CLI to GitHub releases with checksums
MarshalX 09bc110
simplify workflow
MarshalX bf7c627
remove test branch from triggers
MarshalX 89871ed
remove env vars
MarshalX 99dc010
remove test trigger
MarshalX cff2b19
make filepath relative in hashes DB
MarshalX 7499cde
move constants to the top
MarshalX e7c5ad4
refactoring
MarshalX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#!/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. | ||
""" | ||
|
||
import argparse | ||
import hashlib | ||
import os | ||
import platform | ||
from pathlib import Path | ||
from string import Template | ||
from typing import List, Tuple, Union | ||
|
||
_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.exe$ext'), | ||
} | ||
|
||
DirHashes = List[Tuple[str, str]] | ||
|
||
|
||
def get_hash_of_file(file_path: Union[str, Path]) -> str: | ||
with open(file_path, 'rb') as f: | ||
return hashlib.sha256(f.read()).hexdigest() | ||
|
||
|
||
def calculate_hash_of_every_file_in_the_directory(dir_path: Path) -> DirHashes: | ||
hashes = [] | ||
|
||
for root, _, files in os.walk(dir_path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
file_hash = get_hash_of_file(file_path) | ||
|
||
relative_file_path = file_path[file_path.find(dir_path.name):] | ||
hashes.append((file_hash, relative_file_path)) | ||
|
||
# sort by file path | ||
hashes.sort(key=lambda x: x[1]) | ||
|
||
return hashes | ||
|
||
|
||
def is_arm() -> bool: | ||
return platform.machine().lower() in ('arm', 'arm64', 'aarch64') | ||
|
||
|
||
def get_os_name() -> str: | ||
return platform.system().lower() | ||
|
||
|
||
def get_cli_file_name(suffix: str = '', ext: str = '') -> str: | ||
os_name = get_os_name() | ||
if os_name not in _OS_TO_CLI_DIST_TEMPLATE: | ||
raise Exception(f'Unsupported OS: {os_name}') | ||
|
||
template = _OS_TO_CLI_DIST_TEMPLATE[os_name] | ||
return template.substitute(suffix=suffix, ext=ext) | ||
|
||
|
||
def get_cli_file_suffix(is_onedir: bool) -> str: | ||
suffixes = [] | ||
|
||
if is_arm(): | ||
suffixes.append('-arm') | ||
if is_onedir: | ||
suffixes.append('-onedir') | ||
|
||
return ''.join(suffixes) | ||
|
||
|
||
def write_hash_to_file(file_hash: str, output_path: str) -> None: | ||
with open(output_path, 'w') as f: | ||
f.write(file_hash) | ||
|
||
|
||
def write_hashes_db_to_file(hashes: DirHashes, output_path: str) -> None: | ||
content = '' | ||
for file_hash, file_path in hashes: | ||
content += f'{file_hash} {file_path}\n' | ||
|
||
with open(output_path, 'w') as f: | ||
f.write(content) | ||
|
||
|
||
def get_cli_filename(is_onedir: bool) -> str: | ||
return get_cli_file_name(get_cli_file_suffix(is_onedir)) | ||
|
||
|
||
def get_cli_path(output_path: Path, is_onedir: bool) -> str: | ||
return os.path.join(output_path, get_cli_filename(is_onedir)) | ||
|
||
|
||
def get_cli_hash_filename(is_onedir: bool) -> str: | ||
return get_cli_file_name(suffix=get_cli_file_suffix(is_onedir), ext=_HASH_FILE_EXT) | ||
|
||
|
||
def get_cli_hash_path(output_path: Path, is_onedir: bool) -> str: | ||
return os.path.join(output_path, get_cli_hash_filename(is_onedir)) | ||
|
||
|
||
def process_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) | ||
|
||
if is_onedir: | ||
hashes = calculate_hash_of_every_file_in_the_directory(input_path) | ||
write_hashes_db_to_file(hashes, hash_file_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)) | ||
|
||
return get_cli_filename(is_onedir) | ||
|
||
|
||
def main() -> 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() | ||
|
||
if get_os_name() == 'windows' and not is_onedir and input_path.suffix != '.exe': | ||
MarshalX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# add .exe on windows if was missed (to simplify GHA workflow) | ||
input_path = input_path.with_suffix('.exe') | ||
|
||
artifact_name = process_executable_file(input_path, is_onedir) | ||
|
||
print(artifact_name) # noqa: T201 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.