|
17 | 17 | python_versions_to_test = ["3.8", "3.9", "3.10", "3.11"] |
18 | 18 | nox.options.stop_on_first_error = True |
19 | 19 | nox.options.error_on_missing_interpreters = True |
| 20 | +nox.options.default_venv_backend = 'virtualenv' |
20 | 21 |
|
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 |
30 | 25 |
|
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 |
33 | 65 | # def lint(session: Session) -> None: |
34 | 66 | # """Ensure the code is formatted as expected.""" |
35 | 67 | # session.install("ruff") |
36 | 68 | # session.run("ruff", "--output-format=github", "--config=pyproject.toml", ".") |
37 | 69 |
|
38 | | -@session |
| 70 | +@session # to only run on the current python interpreter |
39 | 71 | def check_poetry(session: Session) -> None: |
40 | 72 | """Check whether Poetry is correctly configured.""" |
41 | 73 | session.run("poetry", "check", "--no-interaction", external=True) |
|
0 commit comments