-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathtoxfile.py
184 lines (148 loc) · 5.51 KB
/
toxfile.py
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
"""Project-local tox env customizations."""
import platform
import ssl
from base64 import b64encode
from hashlib import sha256
from logging import getLogger
from os import environ, getenv
from pathlib import Path
from tox.execute.request import StdinSource
from tox.plugin import impl
from tox.tox_env.api import ToxEnv
IS_GITHUB_ACTIONS_RUNTIME = getenv('GITHUB_ACTIONS') == 'true'
FILE_APPEND_MODE = 'a'
UNICODE_ENCODING = 'utf-8'
SYS_PLATFORM = platform.system()
IS_WINDOWS = SYS_PLATFORM == 'Windows'
logger = getLogger(__name__)
def _log_debug_before_run_commands(msg: str) -> None:
logger.debug(
'%s%s> %s', # noqa: WPS323
'toxfile',
':tox_before_run_commands',
msg,
)
def _log_info_before_run_commands(msg: str) -> None:
logger.info(
'%s%s> %s', # noqa: WPS323
'toxfile',
':tox_before_run_commands',
msg,
)
def _log_warning_before_run_commands(msg: str) -> None:
logger.warning(
'%s%s> %s', # noqa: WPS323
'toxfile',
':tox_before_run_commands',
msg,
)
@impl
def tox_before_run_commands(tox_env: ToxEnv) -> None: # noqa: WPS210, WPS213
"""Display test runtime info when in GitHub Actions CI/CD.
This also injects ``SOURCE_DATE_EPOCH`` env var into build-dists.
:param tox_env: A tox environment object.
"""
if tox_env.name == 'build-dists':
_log_debug_before_run_commands(
'Setting the Git HEAD-based epoch for reproducibility in GHA...',
)
git_executable = 'git'
git_log_cmd = ( # noqa: WPS317
git_executable,
'-c', 'core.pager=', # prevents ANSI escape sequences
'log',
'-1',
'--pretty=%ct', # noqa: WPS323
)
tox_env.conf['allowlist_externals'].append(git_executable)
git_log_outcome = tox_env.execute(git_log_cmd, StdinSource.OFF)
tox_env.conf['allowlist_externals'].pop()
if git_log_outcome.exit_code:
_log_warning_before_run_commands(
f'Failed to look up Git HEAD timestamp. {git_log_outcome !s}',
)
return
git_head_timestamp = git_log_outcome.out.strip()
_log_info_before_run_commands(
f'Setting `SOURCE_DATE_EPOCH={git_head_timestamp !s}` environment '
'variable to facilitate build reproducibility...',
)
tox_env.environment_variables['SOURCE_DATE_EPOCH'] = git_head_timestamp
if tox_env.name not in {'py', 'python'} or not IS_GITHUB_ACTIONS_RUNTIME:
_log_debug_before_run_commands(
'Not logging runtime info because this is not a test run on '
'GitHub Actions platform...',
)
return
_log_info_before_run_commands('INFO Logging runtime details...')
systeminfo_executable = 'systeminfo'
systeminfo_cmd = (systeminfo_executable,)
if IS_WINDOWS:
tox_env.conf['allowlist_externals'].append(systeminfo_executable)
tox_env.execute(systeminfo_cmd, stdin=StdinSource.OFF, show=True)
tox_env.conf['allowlist_externals'].pop()
else:
_log_debug_before_run_commands(
f'Not running {systeminfo_executable !s} because this is '
'not Windows...',
)
_log_info_before_run_commands('Logging platform information...')
print( # noqa: WPS421
'Current platform information:\n'
f'{platform.platform()=}'
f'{platform.system()=}'
f'{platform.version()=}'
f'{platform.uname()=}'
f'{platform.release()=}',
)
_log_info_before_run_commands('Logging current OpenSSL module...')
print( # noqa: WPS421
'Current OpenSSL module:\n'
f'{ssl.OPENSSL_VERSION=}\n'
f'{ssl.OPENSSL_VERSION_INFO=}\n'
f'{ssl.OPENSSL_VERSION_NUMBER=}',
)
def _log_debug_after_run_commands(msg: str) -> None:
logger.debug(
'%s%s> %s', # noqa: WPS323
'toxfile',
':tox_after_run_commands',
msg,
)
def _compute_sha256sum(file_path: Path) -> str:
return sha256(file_path.read_bytes()).hexdigest()
def _produce_sha256sum_line(file_path: Path) -> str:
sha256_str = _compute_sha256sum(file_path)
return f'{sha256_str !s} {file_path.name !s}'
@impl
def tox_after_run_commands(tox_env: ToxEnv) -> None:
"""Compute combined dists hash post build-dists under GHA.
:param tox_env: A tox environment object.
"""
if tox_env.name == 'build-dists' and IS_GITHUB_ACTIONS_RUNTIME:
_log_debug_after_run_commands(
'Computing and storing the base64 representation '
'of the combined dists SHA-256 hash in GHA...',
)
dists_dir_path = Path(__file__).parent / 'dist'
emulated_sha256sum_output = '\n'.join(
_produce_sha256sum_line(artifact_path)
for artifact_path in dists_dir_path.glob('*')
)
emulated_base64_w0_output = b64encode(
emulated_sha256sum_output.encode(),
).decode()
with Path(environ['GITHUB_OUTPUT']).open(
encoding=UNICODE_ENCODING,
mode=FILE_APPEND_MODE,
) as outputs_file:
print( # noqa: WPS421
'combined-dists-base64-encoded-sha256-hash='
f'{emulated_base64_w0_output !s}',
file=outputs_file,
)
def tox_append_version_info() -> str:
"""Produce text to be rendered in ``tox --version``.
:returns: A string with the plugin details.
"""
return '[toxfile]' # Broken: https://github.com/tox-dev/tox/issues/3508