|
2 | 2 |
|
3 | 3 | import platform |
4 | 4 | import ssl |
| 5 | +from base64 import b64encode |
| 6 | +from hashlib import sha256 |
5 | 7 | from logging import getLogger |
6 | | -from os import getenv |
| 8 | +from os import environ, getenv |
| 9 | +from pathlib import Path |
7 | 10 |
|
8 | 11 | from tox.execute.request import StdinSource |
9 | 12 | from tox.plugin import impl |
10 | 13 | from tox.tox_env.api import ToxEnv |
11 | 14 |
|
12 | 15 |
|
13 | 16 | IS_GITHUB_ACTIONS_RUNTIME = getenv('GITHUB_ACTIONS') == 'true' |
| 17 | +FILE_APPEND_MODE = 'a' |
| 18 | +UNICODE_ENCODING = 'utf-8' |
14 | 19 | SYS_PLATFORM = platform.system() |
15 | 20 | IS_WINDOWS = SYS_PLATFORM == 'Windows' |
16 | 21 |
|
@@ -82,6 +87,55 @@ def tox_before_run_commands(tox_env: ToxEnv) -> None: # noqa: WPS213 |
82 | 87 | ) |
83 | 88 |
|
84 | 89 |
|
| 90 | +def _log_debug_after_run_commands(msg: str) -> None: |
| 91 | + logger.debug( |
| 92 | + '%s%s> %s', # noqa: WPS323 |
| 93 | + 'toxfile', |
| 94 | + ':tox_after_run_commands', |
| 95 | + msg, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def _compute_sha256sum(file_path: Path) -> str: |
| 100 | + return sha256(file_path.read_bytes()).hexdigest() |
| 101 | + |
| 102 | + |
| 103 | +def _produce_sha256sum_line(file_path: Path) -> str: |
| 104 | + sha256_str = _compute_sha256sum(file_path) |
| 105 | + return f'{sha256_str !s} {file_path.name !s}' |
| 106 | + |
| 107 | + |
| 108 | +@impl |
| 109 | +def tox_after_run_commands(tox_env: ToxEnv) -> None: |
| 110 | + """Compute combined dists hash post build-dists under GHA. |
| 111 | +
|
| 112 | + :param tox_env: A tox environment object. |
| 113 | + """ |
| 114 | + if tox_env.name == 'build-dists' and IS_GITHUB_ACTIONS_RUNTIME: |
| 115 | + _log_debug_after_run_commands( |
| 116 | + 'Computing and storing the base64 representation ' |
| 117 | + 'of the combined dists SHA-256 hash in GHA...', |
| 118 | + ) |
| 119 | + dists_dir_path = Path(__file__).parent / 'dist' |
| 120 | + emulated_sha256sum_output = '\n'.join( |
| 121 | + _produce_sha256sum_line(artifact_path) |
| 122 | + for artifact_path in dists_dir_path.glob('*') |
| 123 | + ) |
| 124 | + emulated_base64_w0_output = b64encode( |
| 125 | + emulated_sha256sum_output.encode(), |
| 126 | + ).decode() |
| 127 | + |
| 128 | + with Path(environ['GITHUB_OUTPUT']).open( |
| 129 | + encoding=UNICODE_ENCODING, |
| 130 | + mode=FILE_APPEND_MODE, |
| 131 | + ) as outputs_file: |
| 132 | + print( # noqa: WPS421 |
| 133 | + 'combined-dists-base64-encoded-sha256-hash=' |
| 134 | + f'{emulated_base64_w0_output !s}', |
| 135 | + file=outputs_file, |
| 136 | + ) |
| 137 | + |
| 138 | + |
85 | 139 | def tox_append_version_info() -> str: |
86 | 140 | """Produce text to be rendered in ``tox --version``. |
87 | 141 |
|
|
0 commit comments