Skip to content

Commit 708ced2

Browse files
committed
Added an option to export to requirements.txt without markers
1 parent 875cda2 commit 708ced2

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/poetry_plugin_export/command.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class ExportCommand(GroupCommand):
2121
description = "Exports the lock file to alternative formats."
2222

2323
options = [ # noqa: RUF012
24+
option("without-markers",
25+
None,
26+
"Include markers in the exported file (default - True).",
27+
flag=True),
2428
option(
2529
"format",
2630
"f",
@@ -146,13 +150,18 @@ def handle(self) -> int:
146150
)
147151
return 1
148152

153+
without_markers = False
154+
if self.option("without-markers"):
155+
without_markers = self.option("without-markers")
156+
149157
groups = (
150158
self.poetry.package.dependency_group_names(include_optional=True)
151159
if self.option("all-groups")
152160
else self.activated_groups
153161
)
154162

155163
exporter = Exporter(self.poetry, self.io)
164+
exporter.with_markers(not without_markers)
156165
exporter.only_groups(list(groups))
157166
exporter.with_extras(list(extras))
158167
exporter.with_hashes(not self.option("without-hashes"))

src/poetry_plugin_export/exporter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self, poetry: Poetry, io: IO) -> None:
4545
self._with_hashes = True
4646
self._with_credentials = False
4747
self._with_urls = True
48+
self._with_markers = True
4849
self._extras: Collection[NormalizedName] = ()
4950
self._groups: Iterable[str] = [MAIN_GROUP]
5051

@@ -72,6 +73,12 @@ def with_hashes(self, with_hashes: bool = True) -> Exporter:
7273

7374
return self
7475

76+
def with_markers(self, with_markers: bool = True) -> Exporter:
77+
# This is a no-op for now, but we can implement it in the future
78+
# if needed.
79+
self._with_markers = with_markers
80+
return self
81+
7582
def with_credentials(self, with_credentials: bool = True) -> Exporter:
7683
self._with_credentials = with_credentials
7784

@@ -153,7 +160,7 @@ def _export_generic_txt(
153160

154161
if not is_direct_remote_reference and ";" in requirement:
155162
markers = requirement.split(";", 1)[1].strip()
156-
if markers:
163+
if markers and self._with_markers:
157164
line += f" ; {markers}"
158165

159166
if (

tests/test_exporter.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,64 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers(
519519

520520
assert expected == {}
521521

522+
@pytest.mark.parametrize("lock_version", ["2.1"])
523+
def test_exporter_with_no_markers_flag(tmp_path: Path, poetry: Poetry, lock_version: str) -> None:
524+
lock_data = {
525+
"package": [
526+
{
527+
"name": "greenlet",
528+
"version": "3.2.1",
529+
"optional": False,
530+
"python-versions": ">=3.9",
531+
"markers": 'python_version < "3.14" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32")',
532+
},
533+
{
534+
"name": "typing-extensions",
535+
"version": "4.13.2",
536+
"optional": False,
537+
"python-versions": ">=3.8",
538+
},
539+
{
540+
"name": "sqlalchemy",
541+
"version": "2.0.40",
542+
"optional": False,
543+
"python-versions": ">=3.7",
544+
"dependencies": {
545+
"greenlet": {
546+
"version": ">=1",
547+
"markers": 'python_version < "3.14" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32")'
548+
},
549+
"typing-extensions": ">=4.6.0"
550+
},
551+
}
552+
],
553+
"metadata": {
554+
"lock-version": lock_version,
555+
"files": {
556+
"greenlet": [],
557+
"typing-extensions": [],
558+
"sqlalchemy": []
559+
}
560+
},
561+
}
562+
563+
fix_lock_data(lock_data)
564+
poetry.locker.mock_lock_data(lock_data) # type: ignore[attr-defined]
565+
566+
exporter = Exporter(poetry, NullIO())
567+
exporter.with_markers(False)
568+
exporter.export("requirements.txt", tmp_path, "requirements.txt")
569+
570+
with open(tmp_path / "requirements.txt") as f:
571+
lines = set(f.read().strip().splitlines())
572+
573+
expected = {
574+
'greenlet==3.2.1',
575+
'sqlalchemy==2.0.40',
576+
'typing-extensions==4.13.2',
577+
}
578+
579+
assert lines == expected
522580

523581
@pytest.mark.parametrize(
524582
["dev", "lines"],

0 commit comments

Comments
 (0)