Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial support for installing Poetry dependency groups
  • Loading branch information
edgarrmondragon committed Sep 18, 2022
commit 7b7143a081e2ca3422342fcf73bdd592f0cd1195
6 changes: 5 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ def mypy(session: Session) -> None:
@session
@nox.parametrize(
"python,poetry",
[(python_versions[0], "1.0.10"), *((python, None) for python in python_versions)],
[
(python_versions[0], "1.2.0"),
(python_versions[0], "1.0.10"),
*((python, None) for python in python_versions),
],
)
def tests(session: Session, poetry: Optional[str]) -> None:
"""Run the test suite."""
Expand Down
41 changes: 35 additions & 6 deletions src/nox_poetry/poetry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Poetry interface."""
import os
import sys
from enum import Enum
from packaging.version import Version
from pathlib import Path
from typing import Any
from typing import Any, Tuple
from typing import Iterable
from typing import Iterator
from typing import List
Expand All @@ -11,6 +13,11 @@
import tomlkit
from nox.sessions import Session

from importlib import metadata

POETRY_VERSION = Version(metadata.version("poetry"))
POETRY_VERSION_1_2_0 = Version("1.2.0")


class CommandSkippedError(Exception):
"""The command was not executed by Nox."""
Expand Down Expand Up @@ -69,7 +76,13 @@ def config(self) -> Config:
self._config = Config(Path.cwd())
return self._config

def export(self) -> str:
def export(
self,
extras: bool = True,
without_hashes: bool = True,
include_groups: Tuple[str] = ("dev",),
exclude_groups: Tuple[str] = (),
) -> str:
"""Export the lock file to requirements format.

Returns:
Expand All @@ -78,13 +91,29 @@ def export(self) -> str:
Raises:
CommandSkippedError: The command `poetry export` was not executed.
"""
output = self.session.run_always(
args = [
"poetry",
"export",
"--format=requirements.txt",
"--dev",
*[f"--extras={extra}" for extra in self.config.extras],
"--without-hashes",
]

if without_hashes:
args.append("--without-hashes")

if extras:
args.extend(f"--extras={extra}" for extra in self.config.extras)

if POETRY_VERSION >= POETRY_VERSION_1_2_0:
if include_groups:
args.append(f"--with={','.join(include_groups)}")

if exclude_groups:
args.append(f"--without={','.join(exclude_groups)}")
else:
args.append("--dev")

output = self.session.run_always(
*args,
external=True,
silent=True,
stderr=None,
Expand Down
60 changes: 54 additions & 6 deletions src/nox_poetry/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def rewrite(arg: str, extras: Optional[str]) -> str:
self.session.run_always("pip", "uninstall", "--yes", package, silent=True)

try:
requirements = self.export_requirements()
requirements = self.export_requirements(filename="constraints.txt")
except CommandSkippedError:
return

Expand Down Expand Up @@ -173,7 +173,7 @@ def installroot(
"""
try:
package = self.build_package(distribution_format=distribution_format)
requirements = self.export_requirements()
requirements = self.export_requirements(filename="constraints.txt")
except CommandSkippedError:
return

Expand All @@ -196,7 +196,37 @@ def installroot(

self.session.install(f"--constraint={requirements}", package)

def export_requirements(self) -> Path:
def install_groups(self, groups: Tuple[str], *args, **kwargs) -> None:
"""Install all packages in the given Poetry dependency group into a Nox session
using Poetry.

Args:
group: The name of the dependency group to install.
args: Command-line arguments for ``pip install``.
kwargs: Keyword-arguments for ``session.install``. These are the same
as those for :meth:`nox.sessions.Session.run`.
"""

try:
requirements = self.export_requirements(
as_constraints=False,
extras=False,
groups=groups,
)
except CommandSkippedError:
return

self.install("-r", str(requirements), *args, **kwargs)

def export_requirements(
self,
*,
filename: str = "requirements.txt",
as_constraints: bool = True,
extras: bool = True,
without_hashes: bool = True,
groups: Tuple[str] = ("dev",),
) -> Path:
"""Export a requirements file from Poetry.

This function uses `poetry export <https://python-poetry.org/docs/cli/#export>`_
Expand All @@ -217,15 +247,21 @@ def export_requirements(self) -> Path:
tmpdir = Path(self.session._runner.envdir) / "tmp"
tmpdir.mkdir(exist_ok=True, parents=True)

path = tmpdir / "requirements.txt"
path = tmpdir / filename
hashfile = tmpdir / f"{path.name}.hash"

lockdata = Path("poetry.lock").read_bytes()
digest = hashlib.blake2b(lockdata).hexdigest()

if not hashfile.is_file() or hashfile.read_text() != digest:
constraints = to_constraints(self.poetry.export())
path.write_text(constraints)
contents = self.poetry.export(
include_groups=groups,
extras=extras,
without_hashes=without_hashes,
)
if as_constraints:
contents = to_constraints(contents)
path.write_text(contents)
hashfile.write_text(digest)

return path
Expand Down Expand Up @@ -290,3 +326,15 @@ def __init__(self, session: nox.Session) -> None:
def install(self, *args: str, **kwargs: Any) -> None:
"""Install packages into a Nox session using Poetry."""
return self.poetry.install(*args, **kwargs)

def install_groups(self, groups: Tuple[str], *args, **kwargs) -> None:
"""Install all packages in the given Poetry dependency group into a Nox session
using Poetry.

Args:
group: The name of the dependency group to install.
args: Command-line arguments for ``pip install``.
kwargs: Keyword-arguments for ``session.install``. These are the same
as those for :meth:`nox.sessions.Session.run`.
"""
return self.poetry.install_groups(groups, *args, **kwargs)