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 25, 2018
1 parent 3c26c8d commit 2301d0c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 6 additions & 0 deletions poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -31,6 +32,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 +97,9 @@ def get_default_commands(self): # type: () -> list
# Env command
commands += [EnvCommand()]

# Setuptools commands
commands += [SetuptoolsCommand()]

# 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
27 changes: 27 additions & 0 deletions poetry/console/commands/setuptools/setupfile.py
Original file line number Diff line number Diff line change
@@ -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 <comment>setup.py</comment>
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
15 changes: 15 additions & 0 deletions poetry/console/commands/setuptools/setuptools.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
37 changes: 37 additions & 0 deletions tests/console/commands/setuptools/test_setupfile.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2301d0c

Please sign in to comment.