Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ poetry export -f requirements.txt > requirements.txt

!!!note

Only the `requirements.txt` format is currently supported.
Only the `requirements.txt` and `setup.py` format is currently supported.

### Options

Expand Down
14 changes: 10 additions & 4 deletions poetry/console/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ class ExportCommand(Command):
options = [
option("format", "f", "Format to export to.", flag=False),
option("output", "o", "The name of the output file.", flag=False),
option("without-hashes", None, "Exclude hashes from the exported file."),
option("dev", None, "Include development dependencies."),
option(
"without-hashes", None, "Exclude hashes from the exported file if used."
),
option("dev", None, "Include development dependencies if configurable."),
option(
"extras",
"E",
"Extra sets of dependencies to include.",
"Extra sets of dependencies to include if configurable.",
flag=False,
multiple=True,
),
option("with-credentials", None, "Include credentials for extra indices."),
option(
"with-credentials",
None,
"Include credentials for extra indices if format supports indices.",
),
]

def handle(self):
Expand Down
13 changes: 12 additions & 1 deletion poetry/utils/exporter.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from typing import Any
from typing import Union

from clikit.api.io import IO

from poetry.io.null_io import NullIO
from poetry.masonry.builders import SdistBuilder
from poetry.packages.directory_dependency import DirectoryDependency
from poetry.packages.file_dependency import FileDependency
from poetry.packages.url_dependency import URLDependency
from poetry.packages.vcs_dependency import VCSDependency
from poetry.poetry import Poetry
from poetry.utils._compat import Path
from poetry.utils._compat import decode
from poetry.utils.env import NullEnv


class Exporter(object):
"""
Exporter class to export a lock file to alternative formats.
"""

ACCEPTED_FORMATS = ("requirements.txt",)
ACCEPTED_FORMATS = ("requirements.txt", "setup.py")
ALLOWED_HASH_ALGORITHMS = ("sha256", "sha384", "sha512")

def __init__(self, poetry): # type: (Poetry) -> None
Expand Down Expand Up @@ -163,6 +167,13 @@ def _export_requirements_txt(

self._output(content, cwd, output)

def _export_setup_py(
self, cwd, output, *_, **__
): # type: (Path, Union[IO, str], Any, Any) -> None
builder = SdistBuilder(self._poetry, NullEnv(), NullIO())
content = decode(builder.build_setup())
self._output(content, cwd, output)

def _output(
self, content, cwd, output
): # type: (str, Path, Union[IO, str]) -> None
Expand Down
47 changes: 45 additions & 2 deletions tests/utils/test_exporter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import sys

import pytest
Expand All @@ -6,7 +7,7 @@
from poetry.poetry import Poetry
from poetry.repositories.auth import Auth
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.utils._compat import Path
from poetry.utils._compat import Path, encode, decode
from poetry.utils.exporter import Exporter


Expand Down Expand Up @@ -40,7 +41,7 @@ def locker():

@pytest.fixture
def poetry(fixture_dir, locker):
p = Poetry.create(fixture_dir("sample_project"))
p = Poetry.create(fixture_dir("simple_project"))
p._locker = locker

return p
Expand Down Expand Up @@ -799,3 +800,45 @@ def test_exporter_exports_requirements_txt_to_standard_output(tmp_dir, poetry, c
"""

assert out == expected


@pytest.mark.skipif(
sys.version_info < (3, 4), reason="python2.7 fails due to unicode issues"
)
def test_exporter_can_export_setup_py_with_standard_packages(tmp_dir, poetry):
exporter = Exporter(poetry)

exporter.export("setup.py", Path(tmp_dir), "setup.py")

with (Path(tmp_dir) / "setup.py").open(encoding="utf-8") as f:
content = f.read()

expected = """# -*- coding: utf-8 -*-
from distutils.core import setup

packages = \\
['simple_project']

package_data = \\
{'': ['*']}

setup_kwargs = {
'name': 'simple-project',
'version': '1.2.3',
'description': 'Some description.',
'long_description': 'My Package\\n==========\\n',
'author': 'Sébastien Eustace',
'author_email': 'sebastien@eustace.io',
'maintainer': None,
'maintainer_email': None,
'url': 'https://poetry.eustace.io',
'packages': packages,
'package_data': package_data,
'python_requires': '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
}


setup(**setup_kwargs)
"""

assert expected == content