diff --git a/example/README.md b/example/README.md index e9c72e00..da1e3554 100644 --- a/example/README.md +++ b/example/README.md @@ -44,17 +44,20 @@ This would be the same as running `unidep merge --name myenv --verbose`: 🔍 Scanning in `.` at depth 0 🔍 Scanning in `hatch_project` at depth 1 🔍 Found `requirements.yaml` at `hatch_project/requirements.yaml` +🔍 Scanning in `poetry_project` at depth 1 +🔍 Found `requirements.yaml` at `poetry_project/requirements.yaml` 🔍 Scanning in `setuptools_project` at depth 1 🔍 Found `requirements.yaml` at `setuptools_project/requirements.yaml` 🔍 Scanning in `setup_py_project` at depth 1 🔍 Found `requirements.yaml` at `setup_py_project/requirements.yaml` 📄 Parsing `hatch_project/requirements.yaml` +📄 Parsing `poetry_project/requirements.yaml` 📄 Parsing `setup_py_project/requirements.yaml` 📄 Parsing include `../setuptools_project` 📄 Parsing `setuptools_project/requirements.yaml` 📝 Generating environment file at `environment.yaml` 📝 Environment file generated successfully. -✅ Generated environment file at `environment.yaml` from `hatch_project/requirements.yaml`, `setup_py_project/requirements.yaml`, `setuptools_project/requirements.yaml` +✅ Generated environment file at `environment.yaml` from `hatch_project/requirements.yaml`, `poetry_project/requirements.yaml`, `setup_py_project/requirements.yaml`, `setuptools_project/requirements.yaml` ``` diff --git a/example/poetry_project/poetry_project.py b/example/poetry_project/poetry_project.py new file mode 100644 index 00000000..7d4290a1 --- /dev/null +++ b/example/poetry_project/poetry_project.py @@ -0,0 +1 @@ +x = 1 diff --git a/example/poetry_project/pyproject.toml b/example/poetry_project/pyproject.toml new file mode 100644 index 00000000..21b36287 --- /dev/null +++ b/example/poetry_project/pyproject.toml @@ -0,0 +1,13 @@ +[build-system] +requires = ["poetry-core>=1.0.0", "unidep @ file://localhost/Users/basnijholt/Code/unidep"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry] +name = "poetry_project" +version = "0.1.0" +description = "Example poetry_project for `unidep`." +authors = ["Bas Nijholt "] + +# `dependencies` is not needed because it is automatically +# populated by `unidep` with the dependencies from the `requirements.yaml` +# [tool.poetry.dependencies] diff --git a/example/poetry_project/requirements.yaml b/example/poetry_project/requirements.yaml new file mode 100644 index 00000000..bbf0b251 --- /dev/null +++ b/example/poetry_project/requirements.yaml @@ -0,0 +1,9 @@ +name: poetry_project +channels: + - conda-forge +dependencies: + - numpy + - pandas + - wexpect # [win] + - pexpect # [unix] + - pip: unidep diff --git a/pyproject.toml b/pyproject.toml index 7a7a73e7..0447bb70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,9 @@ unidep = "unidep:_setuptools_integration._setuptools_finalizer" [project.entry-points.hatch] unidep = "unidep._hatch_integration" +[tool.poetry.plugins."poetry.plugin"] +poetry-dynamic-versioning = "unidep._poetry_integration:UniDepPlugin" + [project.entry-points.pytest11] affected = "unidep._pytest_plugin" diff --git a/unidep/_poetry_integration.py b/unidep/_poetry_integration.py new file mode 100644 index 00000000..f864b255 --- /dev/null +++ b/unidep/_poetry_integration.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +from pathlib import Path + +from cleo.io.io import IO +from poetry.plugins.plugin import Plugin +from poetry.poetry import Poetry + +from unidep._setuptools_integration import get_python_dependencies +from unidep.utils import identify_current_platform + +__all__ = ["UniDepPlugin"] + +# See example: https://github.com/mtkennerly/poetry-dynamic-versioning + + +class UniDepPlugin(Plugin): + def activate(self, poetry: Poetry, io: IO): + io.write_line("Setting dependencies with UniDep") + project_root = Path().resolve() + requirements_file = project_root / "requirements.yaml" + if "dependencies" not in metadata.get("dynamic", []): + return + if not requirements_file.exists(): + return + if "dependencies" in metadata: + error_msg = ( + "You have a requirements.yaml file in your project root," + " but you are also using [project.dependencies]." + " Please choose either requirements.yaml or" + " [project.dependencies], but not both." + ) + raise RuntimeError(error_msg) + + poetry.package.dependencies = get_python_dependencies( + requirements_file, + platforms=[identify_current_platform()], + raises_if_missing=False, + )