Skip to content

Commit 1154a3c

Browse files
committed
feat: use poetry to generate a setup.py file from pyproject.toml
1 parent 3ec86a6 commit 1154a3c

6 files changed

Lines changed: 40 additions & 14 deletions

File tree

clit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
CONFIG_FILENAME = os.path.join(CONFIG_DIR, "config.ini")
1717
CONFIG = ConfigParser()
1818
# http://stackoverflow.com/questions/19359556/configparser-reads-capital-keys-and-make-them-lower-case
19-
CONFIG.optionxform = str
19+
CONFIG.optionxform = str # type: ignore
2020
CONFIG.read(CONFIG_FILENAME)
2121

2222
LOGGER = logging.getLogger(__name__)

clit/dev.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from shutil import rmtree
55
from subprocess import call, check_output
6+
from textwrap import dedent
67
from typing import Tuple
78

89
import click
@@ -167,3 +168,28 @@ def full(part):
167168
def changelog():
168169
"""Preview the changelog."""
169170
shell(f"{PyPICommands.CHANGELOG} -u | less")
171+
172+
173+
@click.command()
174+
def poetry_setup_py():
175+
"""Use poetry to generate a setup.py file from pyproject.toml."""
176+
shell("poetry build")
177+
shell("tar -xvzf dist/*.gz --strip-components 1 */setup.py")
178+
shell("black setup.py")
179+
180+
setup_py_path: Path = Path.cwd() / "setup.py"
181+
lines = setup_py_path.read_text().split("\n")
182+
lines.insert(
183+
1,
184+
dedent(
185+
'''
186+
"""NOTICE: This file was generated automatically by the command: poetry-setup-py."""
187+
'''
188+
).strip(),
189+
)
190+
191+
# Add a hint so mypy ignores the setup() line
192+
lines[-2] += " # type: ignore"
193+
194+
setup_py_path.write_text("\n".join(lines))
195+
click.secho("setup.py generated!", fg="green")

clit/files.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def backup_full(ctx, dry_run: bool, kill: bool, pictures: bool):
151151
def shell(command_line, quiet=False, return_lines=False, **kwargs):
152152
"""Print and run a shell command."""
153153
if not quiet:
154-
print("$ {}".format(command_line))
154+
click.secho("$ ", fg="magenta", nl=False)
155+
click.secho(command_line, fg="yellow")
155156
if return_lines:
156157
kwargs.setdefault("stdout", PIPE)
157158

@@ -163,7 +164,7 @@ def shell(command_line, quiet=False, return_lines=False, **kwargs):
163164
return stdout.split("\n") if stdout else []
164165

165166

166-
def shell_find(command_line, **kwargs):
167+
def shell_find(command_line, **kwargs) -> List[str]:
167168
"""Run a find command using the shell, and return its output as a list."""
168169
if not command_line.startswith("find"):
169170
command_line = f"find {command_line}"

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515
import sys
1616
import os
17+
from typing import Dict
1718

1819
# If extensions (or modules to document with autodoc) are in another
1920
# directory, add these directories to sys.path here. If the directory is
@@ -193,7 +194,7 @@
193194

194195
# -- Options for LaTeX output ------------------------------------------
195196

196-
latex_elements = {
197+
latex_elements: Dict[str, str] = {
197198
# The paper size ('letterpaper' or 'a4paper').
198199
# 'papersize': 'letterpaper',
199200
# The font size ('10pt', '11pt' or '12pt').

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ classifiers = [
2020
]
2121

2222
# TODO This is still not working properly; the scripts are created but not added to the PATH. Temporary workaround:
23-
# Every time the scripts below change, setup.py has to be regenerated like described below.
24-
# Regenerate setup.py based on this .toml file with these commands:
25-
# poetry build && tar -xvz --strip-components 1 -f dist/*.gz */setup.py && rm -rf dist/
23+
# Every time the scripts below change, run:
24+
# poetry run generate-setup-py
2625
[tool.poetry.scripts]
2726
backup-full = "clit.files:backup_full"
2827
git-local-prune = "clit.git:prune_local_branches"
2928
git-vacuum = "clit.git:vacuum"
3029
pycharm-cli = "clit.files:pycharm_cli"
3130
pytest-run = "clit.files:pytest_run"
3231
pypi = "clit.dev:pypi"
32+
poetry-setup-py = "clit.dev:poetry_setup_py"
3333

3434
[tool.poetry.dependencies]
3535
python = "^3.6 || ^3.7"

setup.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
# -*- coding: utf-8 -*-
2-
"""
3-
This file was generated manually.
4-
5-
Check [tool.poetry.scripts] on pyproject.toml for instructions on how to regenerate it.
6-
"""
2+
"""NOTICE: This file was generated automatically by the command: poetry-setup-py."""
73
from distutils.core import setup
84

95
packages = ["clit"]
106

117
package_data = {"": ["*"]}
128

13-
install_requires = ["SQLAlchemy", "argcomplete", "click", "colorlog", "crayons", "plumbum", "prettyconf", "requests"]
9+
install_requires = ["SQLAlchemy", "argcomplete", "click", "colorlog", "plumbum", "prettyconf", "requests"]
1410

1511
entry_points = {
1612
"console_scripts": [
1713
"backup-full = clit.files:backup_full",
1814
"git-local-prune = clit.git:prune_local_branches",
1915
"git-vacuum = clit.git:vacuum",
16+
"poetry-setup-py = clit.dev:poetry_setup_py",
2017
"pycharm-cli = clit.files:pycharm_cli",
18+
"pypi = clit.dev:pypi",
2119
"pytest-run = clit.files:pytest_run",
2220
]
2321
}
@@ -38,4 +36,4 @@
3836
}
3937

4038

41-
setup(**setup_kwargs)
39+
setup(**setup_kwargs) # type: ignore

0 commit comments

Comments
 (0)