Skip to content

Commit

Permalink
Switch order of arguments to venv nox session and fix handling or c…
Browse files Browse the repository at this point in the history
…orner cases
  • Loading branch information
egparedes committed Jun 17, 2024
1 parent 6eef078 commit 496f44e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def load_from_frozen_requirements(filename: str) -> dict[str, str]:
REQUIREMENTS = load_from_frozen_requirements(ROOT_DIR / "requirements" / "dev.txt")


@nox.session
@nox.session(python="3.10")
def lint(session: nox.Session) -> None:
"""Run the linter (pre-commit)."""
session.install("pre-commit")
Expand All @@ -54,7 +54,12 @@ def tests(session: nox.Session) -> None:
@nox.session(python=["3.10", "3.11", "3.12"])
def venv(session: nox.Session) -> None:
"""
Sets up a Python development environment. Use as: `nox -s venv -- [dest_path] [req_preset]
Sets up a Python development environment. Use as: `nox -s venv-3.xx -- [req_preset] [dest_path]
req_preset: The requirements file to use as 'requirements/{req_preset}.txt'.
Default: 'dev'
dest_path (optional): The path to the virtualenv to create.
Default: '.venv-{3.xx}-{req_preset}'
This session will:
- Create a python virtualenv for the session
Expand All @@ -63,20 +68,25 @@ def venv(session: nox.Session) -> None:
- Invoke the python interpreter from the created project environment
to install the project and all it's development dependencies.
""" # noqa: W505 [doc-line-too-long]
venv_path = f"{DEFAULT_DEV_VENV_PATH}-{session.python}"
req_preset = "dev"
venv_path = None
virtualenv_args = []
if session.posargs:
venv_path, *more_pos_args = session.posargs
req_preset, *more_pos_args = session.posargs
if more_pos_args:
req_preset, _ = more_pos_args
venv_path, *_ = more_pos_args
if not venv_path:
venv_path = f"{DEFAULT_DEV_VENV_PATH}-{session.python}-{req_preset}"
venv_path = pathlib.Path(venv_path).resolve()

if not venv_path.exists():
print(f"Creating virtualenv at '{venv_path}' (options: {virtualenv_args})...")
session.install("virtualenv")
session.run("virtualenv", venv_path, silent=True)
else:
elif venv_path.exists():
assert (
venv_path.is_dir() and (venv_path / "bin" / f"python{session.python}").exists
), f"'{venv_path}' path already exists but is not a virtualenv with python{session.python}."
print(f"'{venv_path}' path already exists. Skipping virtualenv creation...")

python_path = venv_path / "bin" / "python"
Expand All @@ -97,7 +107,7 @@ def venv(session: nox.Session) -> None:
)


@nox.session
@nox.session(reuse_venv=True)
def requirements(session: nox.Session) -> None:
"""Freeze requirements files from project specification and synchronize versions across tools.""" # noqa: W505 [doc-line-too-long]
requirements_path = ROOT_DIR / "requirements"
Expand Down

0 comments on commit 496f44e

Please sign in to comment.