Skip to content

Commit 96b9c8f

Browse files
committed
🧪 Move SLSA SHA-256 generation into toxfile
1 parent 0abaad4 commit 96b9c8f

3 files changed

Lines changed: 62 additions & 13 deletions

File tree

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ per-file-ignores =
103103
# If other ignores are added for a specific file in the section following this,
104104
# these will need to be added to that line as well.
105105

106+
# FIXME: toxfile is currently rather complicated, allowing these temporarily:
107+
# WPS202 Found too many module members: 8 > 7
108+
toxfile.py: WPS202
109+
106110
# There are multiple `assert`s (S101)
107111
# and subprocesses (import – S404; call – S603) in tests:
108112
cheroot/test/test_*.py: S101, S404, S603

.github/workflows/ci-cd.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ jobs:
536536
TOXENV: cleanup-dists,build-dists
537537

538538
outputs:
539-
dists-base64-hash: ${{ steps.dist-hashes.outputs.combined-hash }}
539+
dists-base64-hash: >-
540+
${{ steps.tox-run.outputs.combined-dists-base64-encoded-sha256-hash }}
540541
541542
steps:
542543
- name: Switch to using Python 3.11
@@ -652,6 +653,7 @@ jobs:
652653
echo "SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)"
653654
>> "${GITHUB_ENV}"
654655
- name: Build dists
656+
id: tox-run
655657
run: >-
656658
python -m
657659
tox
@@ -665,17 +667,6 @@ jobs:
665667
ls -1
666668
'dist/${{ needs.pre-setup.outputs.sdist-artifact-name }}'
667669
'dist/${{ needs.pre-setup.outputs.wheel-artifact-name }}'
668-
- name: Generate dist hashes to be used for provenance
669-
id: dist-hashes
670-
run: >-
671-
echo "combined-hash=$(
672-
sha256sum
673-
'${{ needs.pre-setup.outputs.sdist-artifact-name }}'
674-
'${{ needs.pre-setup.outputs.wheel-artifact-name }}'
675-
| base64 -w0
676-
)"
677-
>> "${GITHUB_OUTPUT}"
678-
working-directory: dist
679670
- name: Store the distribution packages
680671
uses: actions/upload-artifact@v4
681672
with:

toxfile.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import platform
44
import ssl
5+
from base64 import b64encode
6+
from hashlib import sha256
57
from logging import getLogger
6-
from os import getenv
8+
from os import environ, getenv
9+
from pathlib import Path
710

811
from tox.execute.request import StdinSource
912
from tox.plugin import impl
1013
from tox.tox_env.api import ToxEnv
1114

1215

1316
IS_GITHUB_ACTIONS_RUNTIME = getenv('GITHUB_ACTIONS') == 'true'
17+
FILE_APPEND_MODE = 'a'
18+
UNICODE_ENCODING = 'utf-8'
1419
SYS_PLATFORM = platform.system()
1520
IS_WINDOWS = SYS_PLATFORM == 'Windows'
1621

@@ -82,6 +87,55 @@ def tox_before_run_commands(tox_env: ToxEnv) -> None: # noqa: WPS213
8287
)
8388

8489

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+
85139
def tox_append_version_info() -> str:
86140
"""Produce text to be rendered in ``tox --version``.
87141

0 commit comments

Comments
 (0)