/
tasks.py
389 lines (310 loc) · 12.8 KB
/
tasks.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Copyright 2017 Alethea Katherine Flowers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import importlib.util
import io
import json
import os
import sys
import types
from argparse import Namespace
from typing import List, Union
from colorlog.escape_codes import parse_colors
import nox
from nox import _options, registry
from nox._version import InvalidVersionSpecifier, VersionCheckFailed, check_nox_version
from nox.logger import logger
from nox.manifest import WARN_PYTHONS_IGNORED, Manifest
from nox.sessions import Result
def _load_and_exec_nox_module(global_config: Namespace) -> types.ModuleType:
"""
Loads, executes, then returns the global_config nox module.
Args:
global_config (Namespace): The global config.
Raises:
IOError: If the nox module cannot be loaded. This
exception is chosen such that it will be caught
by load_nox_module and logged appropriately.
Returns:
types.ModuleType: The initialised nox module.
"""
spec = importlib.util.spec_from_file_location(
"user_nox_module", global_config.noxfile
)
if not spec:
raise IOError(f"Could not get module spec from {global_config.noxfile}")
module = importlib.util.module_from_spec(spec)
if not module:
raise IOError(f"Noxfile {global_config.noxfile} is not a valid python module.")
sys.modules["user_nox_module"] = module
loader = spec.loader
if not loader: # pragma: no cover
raise IOError(f"Could not get module loader for {global_config.noxfile}")
# See https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
loader.exec_module(module)
return module
def load_nox_module(global_config: Namespace) -> Union[types.ModuleType, int]:
"""Load the user's noxfile and return the module object for it.
.. note::
This task has two side effects; it makes ``global_config.noxfile``
an absolute path, and changes the working directory of the process.
Args:
global_config (.nox.main.GlobalConfig): The global config.
Returns:
module: The module designated by the Noxfile path.
"""
try:
# Save the absolute path to the Noxfile.
# This will inoculate it if nox changes paths because of an implicit
# or explicit chdir (like the one below).
global_config.noxfile = os.path.realpath(
# Be sure to expand variables
os.path.expandvars(global_config.noxfile)
)
noxfile_parent_dir = os.path.realpath(os.path.dirname(global_config.noxfile))
# Check ``nox.needs_version`` by parsing the AST.
check_nox_version(global_config.noxfile)
# Move to the path where the Noxfile is.
# This will ensure that the Noxfile's path is on sys.path, and that
# import-time path resolutions work the way the Noxfile author would
# guess. The original working directory (the directory that Nox was
# invoked from) gets stored by the .invoke_from "option" in _options.
os.chdir(noxfile_parent_dir)
return _load_and_exec_nox_module(global_config)
except (VersionCheckFailed, InvalidVersionSpecifier) as error:
logger.error(str(error))
return 2
except FileNotFoundError:
logger.error(
f"Failed to load Noxfile {global_config.noxfile}, no such file exists."
)
return 2
except (IOError, OSError):
logger.exception(f"Failed to load Noxfile {global_config.noxfile}")
return 2
def merge_noxfile_options(
module: types.ModuleType, global_config: Namespace
) -> types.ModuleType:
"""Merges any modifications made to ``nox.options`` by the Noxfile module
into global_config.
Args:
module (module): The Noxfile module.
global_config (~nox.main.GlobalConfig): The global configuration.
"""
_options.options.merge_namespaces(global_config, nox.options)
return module
def discover_manifest(
module: Union[types.ModuleType, int], global_config: Namespace
) -> Manifest:
"""Discover all session functions in the noxfile module.
Args:
module (module): The Noxfile module.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
~.Manifest: A manifest of session functions.
"""
# Find any function added to the session registry (meaning it was
# decorated with @nox.session); do not sort these, as they are being
# sorted by decorator call time.
functions = registry.get()
# Get the docstring from the noxfile
module_docstring = module.__doc__
# Return the final dictionary of session functions.
return Manifest(functions, global_config, module_docstring)
def filter_manifest(
manifest: Manifest, global_config: Namespace
) -> Union[Manifest, int]:
"""Filter the manifest according to the provided configuration.
Args:
manifest (~.Manifest): The manifest of sessions to be run.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
Union[~.Manifest,int]: ``3`` if a specified session is not found,
the manifest otherwise (to be sent to the next task).
"""
# Filter by the name of any explicit sessions.
# This can raise KeyError if a specified session does not exist;
# log this if it happens.
if global_config.sessions:
try:
manifest.filter_by_name(global_config.sessions)
except KeyError as exc:
logger.error("Error while collecting sessions.")
logger.error(exc.args[0])
return 3
# Filter by python interpreter versions.
# This function never errors, but may cause an empty list of sessions
# (which is an error condition later).
if global_config.pythons:
manifest.filter_by_python_interpreter(global_config.pythons)
# Filter by keywords.
if global_config.keywords:
try:
ast.parse(global_config.keywords, mode="eval")
except SyntaxError:
logger.error(
"Error while collecting sessions: keywords argument must be a Python expression."
)
return 3
# This function never errors, but may cause an empty list of sessions
# (which is an error condition later).
manifest.filter_by_keywords(global_config.keywords)
# Return the modified manifest.
return manifest
def honor_list_request(
manifest: Manifest, global_config: Namespace
) -> Union[Manifest, int]:
"""If --list was passed, simply list the manifest and exit cleanly.
Args:
manifest (~.Manifest): The manifest of sessions to be run.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
Union[~.Manifest,int]: ``0`` if a listing is all that is requested,
the manifest otherwise (to be sent to the next task).
"""
if not global_config.list_sessions:
return manifest
# If the user just asked for a list of sessions, print that
# and any docstring specified in noxfile.py and be done.
if manifest.module_docstring:
print(manifest.module_docstring.strip(), end="\n\n")
print(f"Sessions defined in {global_config.noxfile}:\n")
reset = parse_colors("reset") if global_config.color else ""
selected_color = parse_colors("cyan") if global_config.color else ""
skipped_color = parse_colors("white") if global_config.color else ""
for session, selected in manifest.list_all_sessions():
output = "{marker} {color}{session}{reset}"
if selected:
marker = "*"
color = selected_color
else:
marker = "-"
color = skipped_color
if session.description is not None:
output += " -> {description}"
print(
output.format(
color=color,
reset=reset,
session=session.friendly_name,
description=session.description,
marker=marker,
)
)
print(
f"\nsessions marked with {selected_color}*{reset} are selected, sessions marked with {skipped_color}-{reset} are skipped."
)
return 0
def verify_manifest_nonempty(
manifest: Manifest, global_config: Namespace
) -> Union[Manifest, int]:
"""Abort with an error code if the manifest is empty.
Args:
manifest (~.Manifest): The manifest of sessions to be run.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
Union[~.Manifest,int]: ``3`` on an empty manifest, the manifest
otherwise.
"""
if not manifest:
return 3
return manifest
def run_manifest(manifest: Manifest, global_config: Namespace) -> List[Result]:
"""Run the full manifest of sessions.
Args:
manifest (~.Manifest): The manifest of sessions to be run.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
tuple[~nox.sessions.Session,~.SessionStatus]: A two-tuple of the
sessions and the result of each session that was run.
"""
results = []
# Iterate over each session in the manifest, and execute it.
#
# Note that it is possible for the manifest to be altered in any given
# iteration.
for session in manifest:
# possibly raise warnings associated with this session
if WARN_PYTHONS_IGNORED in session.func.should_warn:
logger.warning(
f"Session {session.name} is set to run with venv_backend='none', "
f"IGNORING its python={session.func.should_warn[WARN_PYTHONS_IGNORED]} parametrization. "
)
result = session.execute()
name = session.friendly_name
status = result.imperfect
result.log(f"Session {name} {status}.")
results.append(result)
# Sanity check: If we are supposed to stop on the first error case,
# the abort now.
if not result and global_config.stop_on_first_error:
return results
# The entire manifest has been processed; return the results.
return results
def print_summary(results: List[Result], global_config: Namespace) -> List[Result]:
"""Print a summary of the results.
Args:
results (Sequence[~nox.sessions.Result]): A list of Result objects.
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
results (Sequence[~nox.sessions.Result]): The results passed
to this function, unmodified.
"""
# Sanity check: Do not print results if there was only one session run.
if len(results) <= 1:
return results
# Iterate over the results and print the result for each in a
# human-readable way.
logger.warning("Ran multiple sessions:")
for result in results:
name = result.session.friendly_name
status = result.status.name.lower()
result.log(f"* {name}: {status}")
# Return the results that were sent to this function.
return results
def create_report(results: List[Result], global_config: Namespace) -> List[Result]:
"""Write a report to the location designated in the config, if any.
Args:
results (Sequence[~nox.sessions.Result]): A list of Result objects
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
results (Sequence[~nox.sessions.Result]): The results passed
to this function, unmodified.
"""
# Sanity check: If no JSON report was requested, this is a no-op.
if global_config.report is None:
return results
# Write the JSON report.
with io.open(global_config.report, "w") as report_file:
json.dump(
{
"result": int(all(results)),
"sessions": [result.serialize() for result in results],
},
report_file,
indent=2,
)
# Return back the results passed to this task.
return results
def final_reduce(results: List[Result], global_config: Namespace) -> int:
"""Reduce the results to a final exit code.
Args:
results (Sequence[~nox.sessions.Result]): A list of Result objects
global_config (~nox.main.GlobalConfig): The global configuration.
Returns:
int: The final status code; ``0`` for success and ``1`` for failure.
"""
if not all(results):
return 1
return 0