Skip to content

Commit

Permalink
Add setuptools command to generate setup.py
Browse files Browse the repository at this point in the history
This change introduces `setuptools:setupfile` command that generates
a `setup.py` file for the project.

Resolves: python-poetry#761
  • Loading branch information
abn committed Dec 24, 2018
1 parent 3c26c8d commit 8a239a2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

from .commands.self import SelfCommand

from .commands.setuptools import SetupFileCommand

from .config import ApplicationConfig

from .commands.env import EnvCommand
Expand Down Expand Up @@ -94,6 +96,9 @@ def get_default_commands(self): # type: () -> list
# Env command
commands += [EnvCommand()]

# Setuptools commands
commands += [SetupFileCommand()]

# Self commands
commands += [SelfCommand()]

Expand Down
1 change: 1 addition & 0 deletions poetry/console/commands/setuptools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .setupfile import SetupFileCommand
26 changes: 26 additions & 0 deletions poetry/console/commands/setuptools/setupfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from poetry.console.commands.env_command import EnvCommand
from poetry.io import NullIO
from poetry.masonry.builders import SdistBuilder
from poetry.utils._compat import Path
from poetry.utils.env import NullEnv


class SetupFileCommand(EnvCommand):
"""
Generate a setuptools compatible <comment>setup.py</comment>
setuptools:setupfile
"""

def handle(self):
try:
setupfile = Path(self.poetry.file.parent, "setup.py")
self.line("<info>Writing to <comment>{}</comment></info>".format(setupfile))
builder = SdistBuilder(self.poetry, NullEnv(), NullIO())
setupfile.write_bytes(builder.build_setup())
except Exception as e:
self.line("<error>{}</error>".format(e))
self.line("<error>Failed to write <comment>setup.py</comment>.</error>")
return 1

return 0
Empty file.
36 changes: 36 additions & 0 deletions tests/console/commands/setuptools/test_setupfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import shutil
import tempfile

import pytest
from cleo.testers import CommandTester

from poetry.poetry import Poetry
from poetry.utils._compat import Path
from tests.test_poetry import fixtures_dir


@pytest.fixture
def tmp_dir():
dir_ = tempfile.mkdtemp(prefix="poetry_")
yield dir_
shutil.rmtree(dir_)


@pytest.fixture
def poetry(repo, tmp_dir):
project_dir = Path(tmp_dir, "sample_project")
shutil.copytree(str(fixtures_dir / "simple_project"), project_dir)
yield Poetry.create(project_dir)


def test_setupfile(app):
command = app.find("setuptools:setupfile")
setupfile = Path(app.poetry.file.parent, "setup.py")

tester = CommandTester(command)
tester.execute([("command", command.get_name())])

assert setupfile.is_file()

expected = "Writing to {}\n".format(setupfile)
assert tester.get_display(True) == expected

0 comments on commit 8a239a2

Please sign in to comment.