Skip to content

Commit 02eb75d

Browse files
committed
pip: Add support for processing Poetry dependency section in pyproject
1 parent 7b66dbc commit 02eb75d

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

pip/flatpak-pip-generator.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,53 @@
110110
sys.exit('PyYAML modules is not installed. Run "pip install PyYAML"')
111111

112112

113+
def get_poetry_deps(pyproject_data: dict[str, Any]) -> list[str]:
114+
poetry_deps = pyproject_data.get("tool", {}).get("poetry", {}).get("dependencies")
115+
116+
if not poetry_deps:
117+
return []
118+
119+
def format_dependency_version(name: str, value: Any) -> str:
120+
sep, suffix = "@", ""
121+
dep_name = name
122+
123+
if isinstance(value, dict):
124+
if version := value.get("version"):
125+
sep, val = "==", version
126+
elif git_url := value.get("git"):
127+
val = f"git+{git_url}" if not git_url.startswith("git@") else git_url
128+
if rev := value.get("branch") or value.get("rev") or value.get("tag"):
129+
val += f"@{rev}"
130+
if subdir := value.get("subdirectory"):
131+
val += f"#subdirectory={subdir}"
132+
elif path := value.get("path"):
133+
dep_name, sep, val = "", "", path
134+
elif url := value.get("url"):
135+
dep_name, sep, val = "", "", url
136+
else:
137+
dep_name, sep, val = name, "", ""
138+
139+
if markers := value.get("markers"):
140+
suffix = f";{markers}"
141+
else:
142+
sep, val = "==", value
143+
144+
if val.startswith("^"):
145+
sep, val = ">=", val[1:]
146+
elif val.startswith("~"):
147+
sep, val = "~=", val[1:]
148+
elif "<" in val or ">" in val:
149+
sep, val = "", val.replace(" ", "")
150+
151+
return f"{dep_name}{sep}{val}{suffix}"
152+
153+
return sorted(
154+
format_dependency_version(dep, val)
155+
for dep, val in poetry_deps.items()
156+
if dep != "python"
157+
)
158+
159+
113160
def get_pypi_url(name: str, filename: str) -> str:
114161
url = f"https://pypi.org/pypi/{name}/json"
115162
print("Extracting download url for", name)
@@ -237,7 +284,11 @@ def fprint(string: str) -> None:
237284
pyproject_file = os.path.expanduser(opts.pyproject_file)
238285
with open(pyproject_file, "rb") as f:
239286
pyproject_data = toml_load(f)
240-
dependencies = pyproject_data.get("project", {}).get("dependencies", [])
287+
is_poetry = pyproject_data.get("tool", {}).get("poetry") is not None
288+
if is_poetry:
289+
dependencies = get_poetry_deps(pyproject_data)
290+
else:
291+
dependencies = pyproject_data.get("project", {}).get("dependencies", [])
241292
packages = list(requirements.parse("\n".join(dependencies)))
242293
with tempfile.NamedTemporaryFile(
243294
"w", delete=False, prefix="requirements."

0 commit comments

Comments
 (0)