From 2301d0cd84b8736d680267fb6ed3607c85b999bc Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Mon, 24 Dec 2018 17:47:51 +0100 Subject: [PATCH] Add setuptools command to generate setup.py This change introduces `setuptools setupfile` command that generates a `setup.py` file for the project. Resolves: #761 --- docs/docs/cli.md | 13 +++++++ poetry/console/application.py | 6 +++ .../console/commands/setuptools/__init__.py | 1 + .../console/commands/setuptools/setupfile.py | 27 ++++++++++++++ .../console/commands/setuptools/setuptools.py | 15 ++++++++ tests/console/commands/setuptools/__init__.py | 0 .../commands/setuptools/test_setupfile.py | 37 +++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 poetry/console/commands/setuptools/__init__.py create mode 100644 poetry/console/commands/setuptools/setupfile.py create mode 100644 poetry/console/commands/setuptools/setuptools.py create mode 100644 tests/console/commands/setuptools/__init__.py create mode 100644 tests/console/commands/setuptools/test_setupfile.py diff --git a/docs/docs/cli.md b/docs/docs/cli.md index a9440a5f098..55dc01da8e9 100644 --- a/docs/docs/cli.md +++ b/docs/docs/cli.md @@ -484,3 +484,16 @@ poetry env remove test-O3eWbxRl-py3.7 !!!note If your remove the currently activated virtualenv, it will be automatically deactivated. + +## setuptools + +The `setuptools` command groups sub commands to management of setuptools assets. + +### setuptools setupfile + +The `setuptools setupfile` command generates a `setup.py` file to be used by + `setuptools`. + +```bash +poetry setuptools setupfile +``` diff --git a/poetry/console/application.py b/poetry/console/application.py index 774df4a7391..2b7c04d8358 100644 --- a/poetry/console/application.py +++ b/poetry/console/application.py @@ -3,6 +3,7 @@ from cleo import Application as BaseApplication from poetry import __version__ +from poetry.console.commands.setuptools.setuptools import SetuptoolsCommand from .commands import AboutCommand from .commands import AddCommand @@ -31,6 +32,8 @@ from .commands.self import SelfCommand +from .commands.setuptools import SetupFileCommand + from .config import ApplicationConfig from .commands.env import EnvCommand @@ -94,6 +97,9 @@ def get_default_commands(self): # type: () -> list # Env command commands += [EnvCommand()] + # Setuptools commands + commands += [SetuptoolsCommand()] + # Self commands commands += [SelfCommand()] diff --git a/poetry/console/commands/setuptools/__init__.py b/poetry/console/commands/setuptools/__init__.py new file mode 100644 index 00000000000..5dcfbd163d6 --- /dev/null +++ b/poetry/console/commands/setuptools/__init__.py @@ -0,0 +1 @@ +from .setupfile import SetupFileCommand diff --git a/poetry/console/commands/setuptools/setupfile.py b/poetry/console/commands/setuptools/setupfile.py new file mode 100644 index 00000000000..7d411d6ff95 --- /dev/null +++ b/poetry/console/commands/setuptools/setupfile.py @@ -0,0 +1,27 @@ +from clikit.io import NullIO + +from poetry.masonry.builders import SdistBuilder +from poetry.utils._compat import Path +from poetry.utils.env import NullEnv +from ..command import Command + + +class SetupFileCommand(Command): + """ + Generate a setuptools compatible setup.py + + setupfile + """ + + def handle(self): + try: + setupfile = Path(self.poetry.file.parent, "setup.py") + self.line("Writing to {}".format(setupfile)) + builder = SdistBuilder(self.poetry, NullEnv(), NullIO()) + setupfile.write_bytes(builder.build_setup()) + except Exception as e: + self.line("{}".format(e)) + self.line("Failed to write setup.py.") + return 1 + + return 0 diff --git a/poetry/console/commands/setuptools/setuptools.py b/poetry/console/commands/setuptools/setuptools.py new file mode 100644 index 00000000000..6517271386d --- /dev/null +++ b/poetry/console/commands/setuptools/setuptools.py @@ -0,0 +1,15 @@ +from ..command import Command +from poetry.console.commands.setuptools import SetupFileCommand + + +class SetuptoolsCommand(Command): + """ + Manage setuptools assets for this project. + + setuptools + """ + + commands = [SetupFileCommand()] + + def handle(self): # type: () -> int + return self.call("help", self._config.name) diff --git a/tests/console/commands/setuptools/__init__.py b/tests/console/commands/setuptools/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/console/commands/setuptools/test_setupfile.py b/tests/console/commands/setuptools/test_setupfile.py new file mode 100644 index 00000000000..53ce0f77e45 --- /dev/null +++ b/tests/console/commands/setuptools/test_setupfile.py @@ -0,0 +1,37 @@ +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"), str(project_dir)) + yield Poetry.create(project_dir) + shutil.rmtree(str(project_dir), ignore_errors=True) + + +def test_setupfile(app): + command = app.find("setuptools setupfile") + setupfile = Path(app.poetry.file.parent, "setup.py") + + tester = CommandTester(command) + tester.execute() + + assert setupfile.is_file() + + expected = "Writing to {}\n".format(setupfile) + assert expected == tester.io.fetch_output()