Skip to content

Commit d037914

Browse files
committed
Created Nox settings TOML file. Automatic conversion of old settings. Added an option to select a different Nox environment directory.
1 parent c9ea211 commit d037914

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Project ###
22
noxenv.txt
3+
noxsettings.toml
34

45
### Python ###
56
*.pyc

CONTRIBUTING.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,25 @@ Steps without :bash:`sudo` access (e.g. on a cluster):
9090
#. Install the project, dependencies and extras: :bash:`poetry install --with test,docs -E cuda -E opencl -E hip`, leaving out :bash:`-E cuda`, :bash:`-E opencl` or :bash:`-E hip` if this does not apply on your system. To go all-out, use :bash:`--all-extras`.
9191
* If you run into "keyring" or other seemingly weird issues, this is a known issue with Poetry on some systems. Do: :bash:`pip install keyring`, :bash:`python3 -m keyring --disable`.
9292
* Depending on the environment, it may be necessary or convenient to install extra packages such as :bash:`cupy-cuda11x` / :bash:`cupy-cuda12x`, and :bash:`cuda-python`. These are currently not defined as dependencies for kernel-tuner, but can be part of tests.
93+
* Verify that your development environment has no missing installs or updates with :bash:`poetry install --sync --dry-run --with test`.
9394
#. Check if the environment is setup correctly by running :bash:`pytest`. All tests should pass, except if you're not on a GPU node, or one or more extras has been left out in the previous step, then these tests will skip gracefully.
94-
#. Set Nox to use the correct backend:
95-
* If you used Mamba in step 2: :bash:`echo "mamba" > noxenv.txt`.
96-
* If you used Miniconda or Anaconda in step 2: :bash:`echo "conda" > noxenv.txt`.
97-
* If you alternatively set up with Venv: :bash:`echo "venv" > noxenv.txt`.
98-
* If you set up with Virtualenv, do not create this file, as this is already the default.
99-
* Be sure to adjust or remove this file when changing backends.
95+
#. Set Nox to use the correct backend and location:
96+
* Run :bash:`conda -- create-settings-file` to automatically create a settings file.
97+
* In this settings file :bash:`noxsettings.toml`, change the :bash:`venvbackend`:
98+
* If you used Mamba in step 2, to :bash:`mamba`.
99+
* If you used Miniconda or Anaconda in step 2, to :bash:`conda`.
100+
* If you used Venv in step 2, to :bash:`venv`.
101+
* If you used Virtualenv in step 2, this is already the default.
102+
* Be sure to adjust this when changing backends.
103+
* The settings file also has :bash:`envdir`, which allows you to `change the directory Nox caches environments in<https://nox.thea.codes/en/stable/usage.html#opt-envdir>`_, particularly helpful if you have a diskquota on your user directory.
100104
#. [Optional] Run the tests on Nox as described below.
101105

102106

103107
Running tests
104108
-------------
105109
To run the tests you can use :bash:`nox` (to run against all supported Python versions in isolated environments) and :bash:`pytest` (to run against the local Python version, see below) in the top-level directory.
106110
For full coverage, make Nox use the additional tests (such as cupy and cuda-python) with :bash:`nox -- additional-tests`.
107-
The Nox isolated environments can take up to 1 gigabyte in size, so users tight on diskspace can run :bash:`nox` with the :bash:`small-disk` option. This removes the other environment caches before each session is ran (note that this will take longer to run).
111+
The Nox isolated environments can take up to 1 gigabyte in size, so users tight on diskspace can run :bash:`nox` with the :bash:`small-disk` option. This removes the other environment caches before each session is ran (note that this will take longer to run). A better option would be to change the location environments are stored in with :bash:`envdir` in the :bash:`noxsettings.toml` file.
108112
Please note that the command-line options can be combined, e.g. :bash:`nox -- additional-tests skip-hip small-disk`.
109113
If you do not have fully compatible hardware or environment, you can use the following options:
110114
* :bash:`nox -- skip-cuda` to skip tests involving CUDA.

noxfile.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,57 @@
1717
python_versions_to_test = ["3.8", "3.9", "3.10", "3.11"]
1818
nox.options.stop_on_first_error = True
1919
nox.options.error_on_missing_interpreters = True
20+
nox.options.default_venv_backend = 'virtualenv'
2021

21-
# set the default environment from the 'noxenv' file, if it exists
22-
environment_file_path = Path("./noxenv.txt")
23-
if environment_file_path.exists():
24-
env_values = ('none', 'virtualenv', 'conda', 'mamba', 'venv') # from https://nox.thea.codes/en/stable/usage.html#changing-the-sessions-default-backend
25-
environment = environment_file_path.read_text()
26-
assert isinstance(environment, str), "File 'noxenv.txt' does not contain text"
27-
environment = environment.strip()
28-
assert environment in env_values, f"File 'noxenv.txt' contains {environment}, must be one of {','.join(env_values)}"
29-
nox.options.default_venv_backend = environment
22+
# workspace level settings
23+
settings_file_path = Path("./noxsettings.toml")
24+
venvbackend_values = ('none', 'virtualenv', 'conda', 'mamba', 'venv') # from https://nox.thea.codes/en/stable/usage.html#changing-the-sessions-default-backend
3025

31-
32-
# @session
26+
@session # to only run on the current python interpreter
27+
def create_settings(session: Session) -> None:
28+
"""One-time creation of noxsettings.toml."""
29+
if session.posargs:
30+
# check if the trigger argument was used
31+
arg_trigger = any(arg.lower() == "create-settings-file" for arg in session.posargs)
32+
# create settings file if the trigger is used or old settings exist
33+
noxenv_file_path = Path("./noxenv.txt")
34+
if arg_trigger or (noxenv_file_path.exists() and not settings_file_path.exists()):
35+
# default values
36+
venvbackend = nox.options.default_venv_backend
37+
envdir = ""
38+
# conversion from old notenv.txt
39+
if noxenv_file_path.exists():
40+
venvbackend = noxenv_file_path.read_text().strip()
41+
noxenv_file_path.unlink()
42+
# write the settings
43+
assert venvbackend in venvbackend_values, f"{venvbackend=}, must be one of {','.join(venvbackend_values)}"
44+
settings = (f'venvbackend = "{venvbackend}"\n'
45+
f'envdir = "{envdir}"\n')
46+
settings_file_path.write_text(settings)
47+
# exit to make sure the user checks the settings are correct
48+
if arg_trigger:
49+
session.warn(f"Settings file '{settings_file_path}' created, exiting. Please check settings are correct before running Nox again.")
50+
exit(1)
51+
52+
# obtain workspace level settings from the 'noxsettings.toml' file
53+
if settings_file_path.exists():
54+
with settings_file_path.open(mode="rb") as fp:
55+
import tomli
56+
nox_settings = tomli.load(fp)
57+
venvbackend = nox_settings['venvbackend']
58+
envdir = nox_settings['envdir']
59+
assert venvbackend in venvbackend_values, f"File '{settings_file_path}' has {venvbackend=}, must be one of {','.join(venvbackend_values)}"
60+
nox.options.default_venv_backend = venvbackend
61+
if envdir is not None and len(envdir) > 0:
62+
nox.options.envdir = envdir
63+
64+
# @session # to only run on the current python interpreter
3365
# def lint(session: Session) -> None:
3466
# """Ensure the code is formatted as expected."""
3567
# session.install("ruff")
3668
# session.run("ruff", "--output-format=github", "--config=pyproject.toml", ".")
3769

38-
@session
70+
@session # to only run on the current python interpreter
3971
def check_poetry(session: Session) -> None:
4072
"""Check whether Poetry is correctly configured."""
4173
session.run("poetry", "check", "--no-interaction", external=True)

0 commit comments

Comments
 (0)