Skip to content

Commit 222f82c

Browse files
authored
Merge pull request #229 from frenzymadness/poetry2reqs
Poetry2reqs
2 parents a6c2329 + 16f0566 commit 222f82c

File tree

13 files changed

+260
-14
lines changed

13 files changed

+260
-14
lines changed

micropipenv.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ def _poetry2pipfile_lock(
752752
if only_direct:
753753
if not no_default:
754754
for dependency_name, info in pyproject_poetry_section.get("dependencies", {}).items():
755-
default[dependency_name] = _translate_poetry_dependency(info)
755+
if dependency_name != "python":
756+
default[dependency_name] = _translate_poetry_dependency(info)
756757

757758
if not no_dev:
758759
for dependency_name, info in pyproject_poetry_section.get("dev-dependencies", {}).items():
@@ -771,8 +772,16 @@ def _poetry2pipfile_lock(
771772

772773
additional_markers = defaultdict(list)
773774
skip_all_markers = object()
775+
add_to_dev = list()
774776

775777
for entry in poetry_lock["package"]:
778+
779+
if entry["category"] not in ("dev", "main"):
780+
message = ("Unknown category for package '{}': '{}'. Supported categories are 'dev' and 'main'.").format(
781+
entry["name"], entry["category"]
782+
)
783+
raise PoetryError(message)
784+
776785
hashes = []
777786
for file_entry in poetry_lock["metadata"]["files"][entry["name"]]:
778787
hashes.append(file_entry["hash"])
@@ -826,6 +835,24 @@ def _poetry2pipfile_lock(
826835
else:
827836
additional_markers[normalize_package_name(dependency_name)].append(skip_all_markers)
828837

838+
# If package fits into both main and dev categories, poetry add it to the main one.
839+
# This might be a problem in the following scenario:
840+
# Let's say our project looks like this:
841+
# dependencies:
842+
# - A
843+
# dev-dependencies:
844+
# - B
845+
# - A (A is also a dependency of B)
846+
# Package A is therefore in the main category so if we use "--do-default" we get
847+
# only package B but no package A. But using requirements file requires to have
848+
# all dependencies with their hashes and pinned versions.
849+
# So, if a package is in dev and has a dependency in main, add the dependency also to dev or
850+
# if a package is already in "add_to_dev", add there also all its dependencies.
851+
if (
852+
entry["category"] == "dev" and dependency_name in pyproject_poetry_section.get("dependencies", ())
853+
) or entry["name"] in add_to_dev:
854+
add_to_dev.append(dependency_name)
855+
829856
for extra_name, extras_listed in entry.get("extras", {}).items():
830857
# Turn requirement specification into the actual requirement name.
831858
all_extra_dependencies = set(Requirement(r.split(" ", maxsplit=1)[0]).name for r in extras_listed)
@@ -839,14 +866,13 @@ def _poetry2pipfile_lock(
839866
if "extras" in requirement:
840867
requirement["extras"] = sorted(requirement["extras"])
841868

842-
if entry["category"] == "main":
843-
if not no_default:
844-
default[entry["name"]] = requirement
845-
elif entry["category"] == "dev":
846-
if not no_dev:
847-
develop[entry["name"]] = requirement
848-
else:
849-
raise PoetryError("Unknown category for package {}: {}".format(entry["name"], entry["category"]))
869+
if entry["category"] == "main" and not no_default:
870+
default[entry["name"]] = requirement
871+
872+
if (entry["category"] == "dev" and not no_dev) or (
873+
entry["name"] in add_to_dev and entry["name"] not in default
874+
):
875+
develop[entry["name"]] = requirement
850876

851877
for dependency_name, markers in additional_markers.items():
852878
# If a package depends on another package unconditionaly
@@ -1069,8 +1095,11 @@ def get_requirements_sections(
10691095

10701096
def _get_package_entry_str(
10711097
package_name, info, *, no_hashes=False, no_versions=False
1072-
): # type: (str, Dict[str, Any], bool, bool) -> str
1098+
): # type: (str, Union[Dict[str, Any], str], bool, bool) -> str
10731099
"""Print entry for the given package."""
1100+
if isinstance(info, str):
1101+
return package_name + info + "\n"
1102+
10741103
if "git" in info: # A special case for a VCS package
10751104
if info.get("editable", False):
10761105
result = "--editable git+{}".format(info["git"])

tests/data/parse/poetry_invalid_category/poetry.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "aaa"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Fridolin Pokorny <fridolin@redhat.com>"]
6+
7+
[tool.poetry.dependencies]
8+
daiquiri = "2.0.0"
9+
10+
[tool.poetry.dev-dependencies]
11+
flexmock = "^0.10.4"
12+
13+
[build-system]
14+
requires = ["poetry>=0.12"]
15+
build-backend = "poetry.masonry.api"

tests/data/requirements/poetry_complex/poetry.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[tool.poetry]
2+
name = "test"
3+
version = "1.0.0"
4+
description = ""
5+
authors = ["Your Name <you@example.com>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.8"
10+
python-dateutil = "2.8.1"
11+
12+
[tool.poetry.dev-dependencies]
13+
freezegun = "1.1.0"
14+
python-dateutil = "2.8.1"
15+
16+
[build-system]
17+
requires = ["poetry-core"]
18+
build-backend = "poetry.core.masonry.api"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
python-dateutil==2.8.1 \
2+
--hash=sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c \
3+
--hash=sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a
4+
six==1.16.0 \
5+
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 \
6+
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926
7+
freezegun==1.1.0 \
8+
--hash=sha256:2ae695f7eb96c62529f03a038461afe3c692db3465e215355e1bb4b0ab408712 \
9+
--hash=sha256:177f9dd59861d871e27a484c3332f35a6e3f5d14626f2bf91be37891f18927f3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Dev dependencies
3+
#
4+
freezegun==1.1.0 \
5+
--hash=sha256:2ae695f7eb96c62529f03a038461afe3c692db3465e215355e1bb4b0ab408712 \
6+
--hash=sha256:177f9dd59861d871e27a484c3332f35a6e3f5d14626f2bf91be37891f18927f3
7+
python-dateutil==2.8.1 \
8+
--hash=sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c \
9+
--hash=sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a
10+
six==1.16.0 \
11+
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 \
12+
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# Default dependencies
3+
#
4+
python-dateutil==2.8.1 \
5+
--hash=sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c \
6+
--hash=sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a
7+
six==1.16.0 \
8+
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 \
9+
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# Default dependencies
3+
#
4+
python-dateutil==2.8.1
5+
six==1.16.0
6+
#
7+
# Dev dependencies
8+
#
9+
freezegun==1.1.0
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Default dependencies
3+
#
4+
python-dateutil==2.8.1 \
5+
--hash=sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c \
6+
--hash=sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a
7+
six==1.16.0 \
8+
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 \
9+
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926
10+
#
11+
# Dev dependencies
12+
#
13+
freezegun==1.1.0 \
14+
--hash=sha256:2ae695f7eb96c62529f03a038461afe3c692db3465e215355e1bb4b0ab408712 \
15+
--hash=sha256:177f9dd59861d871e27a484c3332f35a6e3f5d14626f2bf91be37891f18927f3

0 commit comments

Comments
 (0)