spinup-py 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spinup_py/__init__.py +0 -0
- spinup_py/cli.py +72 -0
- spinup_py/prompts.py +201 -0
- spinup_py/scaffold.py +161 -0
- spinup_py/types.py +116 -0
- spinup_py/update.py +486 -0
- spinup_py-0.2.0.dist-info/METADATA +150 -0
- spinup_py-0.2.0.dist-info/RECORD +12 -0
- spinup_py-0.2.0.dist-info/WHEEL +5 -0
- spinup_py-0.2.0.dist-info/entry_points.txt +2 -0
- spinup_py-0.2.0.dist-info/licenses/LICENSE +21 -0
- spinup_py-0.2.0.dist-info/top_level.txt +1 -0
spinup_py/__init__.py
ADDED
|
File without changes
|
spinup_py/cli.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import typer
|
|
2
|
+
|
|
3
|
+
app = typer.Typer(
|
|
4
|
+
name="spinup-py",
|
|
5
|
+
help="Scaffold and maintain production-ready Python projects.",
|
|
6
|
+
no_args_is_help=False,
|
|
7
|
+
add_completion=False,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@app.callback(
|
|
12
|
+
invoke_without_command=True,
|
|
13
|
+
# Allow options after the positional, e.g. `spinup-py my-proj --non-interactive`.
|
|
14
|
+
context_settings={"allow_interspersed_args": True},
|
|
15
|
+
)
|
|
16
|
+
def main(
|
|
17
|
+
ctx: typer.Context,
|
|
18
|
+
project_name: str | None = typer.Argument(None, help="Name of the new project"),
|
|
19
|
+
update: str | None = typer.Option(
|
|
20
|
+
None,
|
|
21
|
+
"--update",
|
|
22
|
+
"-u",
|
|
23
|
+
metavar="DIR",
|
|
24
|
+
help="Retrofit an existing project at DIR (default: current directory)",
|
|
25
|
+
),
|
|
26
|
+
version: bool = typer.Option(False, "--version", "-v", help="Show version and exit"),
|
|
27
|
+
non_interactive: bool = typer.Option(False, "--non-interactive", "-y", help="Scaffold with defaults, no prompts"),
|
|
28
|
+
) -> None:
|
|
29
|
+
if ctx.invoked_subcommand is not None:
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
if version:
|
|
33
|
+
try:
|
|
34
|
+
from importlib.metadata import PackageNotFoundError
|
|
35
|
+
from importlib.metadata import version as pkg_version
|
|
36
|
+
|
|
37
|
+
typer.echo(pkg_version("spinup-py"))
|
|
38
|
+
except PackageNotFoundError:
|
|
39
|
+
typer.echo("0.1.0")
|
|
40
|
+
raise typer.Exit()
|
|
41
|
+
|
|
42
|
+
if update is not None:
|
|
43
|
+
from spinup_py.update import update_project
|
|
44
|
+
|
|
45
|
+
update_project(update or ".")
|
|
46
|
+
elif project_name is not None:
|
|
47
|
+
from spinup_py.scaffold import run_new
|
|
48
|
+
|
|
49
|
+
run_new(project_name, non_interactive=non_interactive)
|
|
50
|
+
else:
|
|
51
|
+
typer.echo(ctx.get_help())
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@app.command("new")
|
|
55
|
+
def new_cmd(
|
|
56
|
+
project_name: str | None = typer.Argument(None, help="Project name (kebab-case)"),
|
|
57
|
+
non_interactive: bool = typer.Option(False, "--non-interactive", "-y", help="Scaffold with defaults, no prompts"),
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Scaffold a new Python project through guided prompts."""
|
|
60
|
+
from spinup_py.scaffold import run_new
|
|
61
|
+
|
|
62
|
+
run_new(project_name, non_interactive=non_interactive)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@app.command("update")
|
|
66
|
+
def update_cmd(
|
|
67
|
+
target_dir: str = typer.Argument(".", help="Path to the project to retrofit"),
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Retrofit an existing Python project with spinup-py tooling."""
|
|
70
|
+
from spinup_py.update import update_project
|
|
71
|
+
|
|
72
|
+
update_project(target_dir)
|
spinup_py/prompts.py
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import questionary
|
|
5
|
+
import tomlkit
|
|
6
|
+
from pydantic import ValidationError
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
from rich.table import Table
|
|
9
|
+
|
|
10
|
+
from spinup_py.types import ProjectConfig, UserDefaults
|
|
11
|
+
|
|
12
|
+
RC_PATH = Path.home() / ".spinup-pyrc.toml"
|
|
13
|
+
LEGACY_RC_PATH = Path.home() / ".create-py-projectrc.toml"
|
|
14
|
+
console = Console()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _rc_read_path() -> Path | None:
|
|
18
|
+
"""Path to read defaults from: new RC if present, else legacy, else None."""
|
|
19
|
+
if RC_PATH.exists():
|
|
20
|
+
return RC_PATH
|
|
21
|
+
if LEGACY_RC_PATH.exists():
|
|
22
|
+
return LEGACY_RC_PATH
|
|
23
|
+
return None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def load_user_defaults() -> UserDefaults:
|
|
27
|
+
read_path = _rc_read_path()
|
|
28
|
+
if read_path is not None:
|
|
29
|
+
try:
|
|
30
|
+
data = tomlkit.parse(read_path.read_text()).get("defaults", {})
|
|
31
|
+
return UserDefaults(**data)
|
|
32
|
+
except (OSError, tomlkit.exceptions.TOMLKitError, ValidationError):
|
|
33
|
+
# ValidationError (pydantic) does NOT inherit from ValueError, so it
|
|
34
|
+
# must be caught explicitly or a malformed RC file crashes the CLI.
|
|
35
|
+
console.print(f"[yellow]Warning: could not parse {read_path}, using defaults[/yellow]")
|
|
36
|
+
return UserDefaults()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def save_user_defaults(d: UserDefaults) -> None:
|
|
40
|
+
read_path = _rc_read_path()
|
|
41
|
+
if read_path is not None:
|
|
42
|
+
try:
|
|
43
|
+
doc = tomlkit.parse(read_path.read_text())
|
|
44
|
+
except (OSError, tomlkit.exceptions.TOMLKitError) as exc:
|
|
45
|
+
console.print(
|
|
46
|
+
f"[yellow]Warning: could not parse {read_path} ({exc}); " f"non-defaults sections will be lost[/yellow]"
|
|
47
|
+
)
|
|
48
|
+
doc = tomlkit.document()
|
|
49
|
+
else:
|
|
50
|
+
doc = tomlkit.document()
|
|
51
|
+
tbl = tomlkit.table()
|
|
52
|
+
tbl["author"] = d.author
|
|
53
|
+
tbl["email"] = d.email
|
|
54
|
+
tbl["github_handle"] = d.github_handle
|
|
55
|
+
tbl["python_version"] = d.python_version
|
|
56
|
+
doc["defaults"] = tbl
|
|
57
|
+
try:
|
|
58
|
+
RC_PATH.write_text(tomlkit.dumps(doc))
|
|
59
|
+
except OSError:
|
|
60
|
+
console.print(f"[yellow]Warning: could not save defaults to {RC_PATH}[/yellow]")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def build_default_config(project_name: str) -> ProjectConfig:
|
|
64
|
+
"""Build a ProjectConfig from saved/built-in defaults with no prompts."""
|
|
65
|
+
defaults = load_user_defaults()
|
|
66
|
+
return ProjectConfig.create(
|
|
67
|
+
project_name=project_name,
|
|
68
|
+
description="",
|
|
69
|
+
author=defaults.author,
|
|
70
|
+
email=defaults.email,
|
|
71
|
+
github_handle=defaults.github_handle,
|
|
72
|
+
python_version=defaults.python_version,
|
|
73
|
+
include_github_actions=True,
|
|
74
|
+
devcontainer=True,
|
|
75
|
+
include_docs=False,
|
|
76
|
+
codecov=False,
|
|
77
|
+
dockerfile=False,
|
|
78
|
+
deptry=True,
|
|
79
|
+
publish_to_pypi=False,
|
|
80
|
+
open_source_license="MIT license",
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def run_prompts(project_name_arg: str | None = None) -> ProjectConfig:
|
|
85
|
+
console.rule("[bold blue]spinup-py[/bold blue]")
|
|
86
|
+
defaults = load_user_defaults()
|
|
87
|
+
|
|
88
|
+
if project_name_arg:
|
|
89
|
+
project_name = project_name_arg
|
|
90
|
+
else:
|
|
91
|
+
project_name = questionary.text(
|
|
92
|
+
"Project name (kebab-case):",
|
|
93
|
+
validate=lambda v: bool(v)
|
|
94
|
+
and all(c.islower() or c.isdigit() or c == "-" for c in v)
|
|
95
|
+
or "Use lowercase letters, digits, and hyphens only",
|
|
96
|
+
).ask()
|
|
97
|
+
if project_name is None:
|
|
98
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
99
|
+
sys.exit(0)
|
|
100
|
+
|
|
101
|
+
description = questionary.text("Short description:").ask()
|
|
102
|
+
if description is None:
|
|
103
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
104
|
+
sys.exit(0)
|
|
105
|
+
|
|
106
|
+
author = questionary.text("Author name:", default=defaults.author).ask()
|
|
107
|
+
if author is None:
|
|
108
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
109
|
+
sys.exit(0)
|
|
110
|
+
author = author or defaults.author
|
|
111
|
+
email = questionary.text("Email:", default=defaults.email).ask()
|
|
112
|
+
if email is None:
|
|
113
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
114
|
+
sys.exit(0)
|
|
115
|
+
email = email or defaults.email
|
|
116
|
+
github_handle = questionary.text("GitHub handle:", default=defaults.github_handle).ask()
|
|
117
|
+
if github_handle is None:
|
|
118
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
119
|
+
sys.exit(0)
|
|
120
|
+
github_handle = github_handle or defaults.github_handle
|
|
121
|
+
python_version = questionary.select(
|
|
122
|
+
"Python version:",
|
|
123
|
+
choices=["3.12", "3.11", "3.10"],
|
|
124
|
+
default=defaults.python_version if defaults.python_version in ["3.12", "3.11", "3.10"] else "3.12",
|
|
125
|
+
).ask()
|
|
126
|
+
if python_version is None:
|
|
127
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
128
|
+
sys.exit(0)
|
|
129
|
+
|
|
130
|
+
features: list[str] | None = questionary.checkbox(
|
|
131
|
+
"Features to include:",
|
|
132
|
+
choices=[
|
|
133
|
+
questionary.Choice("GitHub Actions CI", value="github_actions", checked=True),
|
|
134
|
+
questionary.Choice("Devcontainer", value="devcontainer", checked=True),
|
|
135
|
+
questionary.Choice("Docusaurus docs site", value="include_docs"),
|
|
136
|
+
questionary.Choice("Codecov coverage", value="codecov"),
|
|
137
|
+
questionary.Choice("Dockerfile", value="dockerfile"),
|
|
138
|
+
questionary.Choice("deptry (dependency audit)", value="deptry", checked=True),
|
|
139
|
+
questionary.Choice("Publish to PyPI", value="publish_to_pypi"),
|
|
140
|
+
],
|
|
141
|
+
).ask()
|
|
142
|
+
if features is None:
|
|
143
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
144
|
+
sys.exit(0)
|
|
145
|
+
|
|
146
|
+
license_choice = questionary.select(
|
|
147
|
+
"License:",
|
|
148
|
+
choices=[
|
|
149
|
+
"MIT license",
|
|
150
|
+
"BSD license",
|
|
151
|
+
"ISC license",
|
|
152
|
+
"Apache Software License 2.0",
|
|
153
|
+
"GNU General Public License v3",
|
|
154
|
+
"Not open source",
|
|
155
|
+
],
|
|
156
|
+
default="MIT license",
|
|
157
|
+
).ask()
|
|
158
|
+
if license_choice is None:
|
|
159
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
160
|
+
sys.exit(0)
|
|
161
|
+
|
|
162
|
+
config = ProjectConfig.create(
|
|
163
|
+
project_name=project_name,
|
|
164
|
+
description=description,
|
|
165
|
+
author=author,
|
|
166
|
+
email=email,
|
|
167
|
+
github_handle=github_handle,
|
|
168
|
+
python_version=python_version,
|
|
169
|
+
include_github_actions="github_actions" in features,
|
|
170
|
+
devcontainer="devcontainer" in features,
|
|
171
|
+
include_docs="include_docs" in features,
|
|
172
|
+
codecov="codecov" in features,
|
|
173
|
+
dockerfile="dockerfile" in features,
|
|
174
|
+
deptry="deptry" in features,
|
|
175
|
+
publish_to_pypi="publish_to_pypi" in features,
|
|
176
|
+
open_source_license=license_choice,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
table = Table(title="Project Configuration", show_header=False, box=None, padding=(0, 2))
|
|
180
|
+
table.add_column("Key", style="cyan")
|
|
181
|
+
table.add_column("Value", style="white")
|
|
182
|
+
for k, v in config.to_cookiecutter_dict().items():
|
|
183
|
+
table.add_row(k, str(v))
|
|
184
|
+
console.print()
|
|
185
|
+
console.print(table)
|
|
186
|
+
console.print()
|
|
187
|
+
|
|
188
|
+
confirm = questionary.confirm("Create project?", default=True).ask()
|
|
189
|
+
if confirm is None or not confirm:
|
|
190
|
+
console.print("[yellow]Aborted.[/yellow]")
|
|
191
|
+
sys.exit(0)
|
|
192
|
+
|
|
193
|
+
save_user_defaults(
|
|
194
|
+
UserDefaults(
|
|
195
|
+
author=author,
|
|
196
|
+
email=email,
|
|
197
|
+
github_handle=github_handle,
|
|
198
|
+
python_version=python_version,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
return config
|
spinup_py/scaffold.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import questionary
|
|
8
|
+
from cookiecutter.exceptions import ( # type: ignore[import-untyped]
|
|
9
|
+
RepositoryCloneFailed,
|
|
10
|
+
RepositoryNotFound,
|
|
11
|
+
)
|
|
12
|
+
from cookiecutter.main import cookiecutter # type: ignore[import-untyped]
|
|
13
|
+
from rich.console import Console
|
|
14
|
+
|
|
15
|
+
from spinup_py.types import ProjectConfig
|
|
16
|
+
|
|
17
|
+
console = Console()
|
|
18
|
+
|
|
19
|
+
REMOTE_TEMPLATE = "gh:joeblackwaslike/spinup-py"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _template_ref() -> str:
|
|
23
|
+
"""Resolve the cookiecutter template reference.
|
|
24
|
+
|
|
25
|
+
1. An explicit override via the SPINUP_PY_TEMPLATE env var (used by CI smoke
|
|
26
|
+
tests and power users) — a local path or any cookiecutter-accepted ref.
|
|
27
|
+
2. The local repository root when running from a source checkout / editable
|
|
28
|
+
install (cookiecutter.json present next to the installed package).
|
|
29
|
+
3. Otherwise the published template on GitHub, which cookiecutter clones.
|
|
30
|
+
"""
|
|
31
|
+
override = os.environ.get("SPINUP_PY_TEMPLATE")
|
|
32
|
+
if override:
|
|
33
|
+
return override
|
|
34
|
+
root = Path(__file__).parent.parent.parent
|
|
35
|
+
if (root / "cookiecutter.json").exists():
|
|
36
|
+
return str(root)
|
|
37
|
+
return REMOTE_TEMPLATE
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def scaffold(dest_dir: str, config: ProjectConfig, non_interactive: bool = False) -> None:
|
|
41
|
+
"""Scaffold a new project from the template.
|
|
42
|
+
|
|
43
|
+
The project is always created at ``<parent of dest_dir>/<config.project_name>``:
|
|
44
|
+
only the *parent* of ``dest_dir`` is used as cookiecutter's output directory,
|
|
45
|
+
and the final directory name comes from ``config.project_name``. Callers must
|
|
46
|
+
therefore pass a ``dest_dir`` whose basename equals ``config.project_name``
|
|
47
|
+
(``run_new`` does this); otherwise the basename would be silently ignored.
|
|
48
|
+
"""
|
|
49
|
+
dest = Path(dest_dir).resolve()
|
|
50
|
+
if dest.name != config.project_name:
|
|
51
|
+
console.print(
|
|
52
|
+
f"[red]dest_dir basename ({dest.name!r}) must match " f"project_name ({config.project_name!r}).[/red]"
|
|
53
|
+
)
|
|
54
|
+
raise SystemExit(1)
|
|
55
|
+
template_ref = _template_ref()
|
|
56
|
+
|
|
57
|
+
console.print(f"\n[bold]Scaffolding [cyan]{config.project_name}[/cyan]...[/bold]")
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
cookiecutter(
|
|
61
|
+
template_ref,
|
|
62
|
+
no_input=True,
|
|
63
|
+
extra_context=config.to_cookiecutter_dict(),
|
|
64
|
+
output_dir=str(dest.parent),
|
|
65
|
+
)
|
|
66
|
+
except (RepositoryNotFound, RepositoryCloneFailed) as exc:
|
|
67
|
+
console.print(f"[red]Could not fetch the project template ({template_ref}): {exc}[/red]")
|
|
68
|
+
console.print(
|
|
69
|
+
"[dim]If you are offline or the template repo is unavailable, point "
|
|
70
|
+
"SPINUP_PY_TEMPLATE at a local cookiecutter template directory.[/dim]"
|
|
71
|
+
)
|
|
72
|
+
raise SystemExit(1) from exc
|
|
73
|
+
|
|
74
|
+
project_dir = dest.parent / config.project_name
|
|
75
|
+
|
|
76
|
+
# Git
|
|
77
|
+
console.print("[dim]Initializing git...[/dim]")
|
|
78
|
+
try:
|
|
79
|
+
_run(["git", "init"], cwd=project_dir)
|
|
80
|
+
_run(["git", "add", "-A"], cwd=project_dir)
|
|
81
|
+
_run(
|
|
82
|
+
["git", "commit", "-m", "chore: initial scaffold from spinup-py"],
|
|
83
|
+
cwd=project_dir,
|
|
84
|
+
)
|
|
85
|
+
except (_CommandError, OSError) as exc:
|
|
86
|
+
# OSError covers FileNotFoundError when git is not installed/on PATH,
|
|
87
|
+
# so we still clean up and exit cleanly instead of crashing.
|
|
88
|
+
console.print(f"[red]{exc}[/red]")
|
|
89
|
+
console.print(f"[yellow]Cleaning up {project_dir}[/yellow]")
|
|
90
|
+
shutil.rmtree(project_dir, ignore_errors=True)
|
|
91
|
+
sys.exit(1)
|
|
92
|
+
|
|
93
|
+
# GitHub
|
|
94
|
+
if not non_interactive:
|
|
95
|
+
push_choice = questionary.select("Push to GitHub?", choices=["public", "private", "skip"], default="skip").ask()
|
|
96
|
+
if push_choice in ("public", "private"):
|
|
97
|
+
console.print("[dim]Creating GitHub repo...[/dim]")
|
|
98
|
+
try:
|
|
99
|
+
subprocess.run(
|
|
100
|
+
[
|
|
101
|
+
"gh",
|
|
102
|
+
"repo",
|
|
103
|
+
"create",
|
|
104
|
+
f"{config.github_handle}/{config.project_name}",
|
|
105
|
+
f"--{push_choice}",
|
|
106
|
+
"--source=.",
|
|
107
|
+
"--remote=origin",
|
|
108
|
+
"--push",
|
|
109
|
+
],
|
|
110
|
+
cwd=project_dir,
|
|
111
|
+
check=True,
|
|
112
|
+
)
|
|
113
|
+
except (subprocess.CalledProcessError, FileNotFoundError) as exc:
|
|
114
|
+
console.print(f"[yellow]GitHub repo creation failed: {exc}[/yellow]")
|
|
115
|
+
console.print("[dim]You can push manually later with: gh repo create[/dim]")
|
|
116
|
+
|
|
117
|
+
# Beads
|
|
118
|
+
try:
|
|
119
|
+
result = subprocess.run(
|
|
120
|
+
["bd", "init", "--skip-agents", "--non-interactive"],
|
|
121
|
+
cwd=project_dir,
|
|
122
|
+
capture_output=True,
|
|
123
|
+
)
|
|
124
|
+
if result.returncode != 0:
|
|
125
|
+
console.print("[dim yellow]bd init failed — skipping Beads init[/dim yellow]")
|
|
126
|
+
except OSError:
|
|
127
|
+
console.print("[dim yellow]bd not found or not executable — skipping Beads init[/dim yellow]")
|
|
128
|
+
|
|
129
|
+
# Serena
|
|
130
|
+
serena_dir = project_dir / ".serena"
|
|
131
|
+
serena_dir.mkdir(exist_ok=True)
|
|
132
|
+
(serena_dir / "project.yml").write_text(f"project_name: {config.project_name}\nlanguage: python\n")
|
|
133
|
+
|
|
134
|
+
console.print(f"\n[bold green]✓ Project created at {project_dir}[/bold green]")
|
|
135
|
+
console.print(f"[dim]Next: cd {config.project_name} && uv sync && uv run pre-commit install[/dim]")
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def run_new(project_name: str | None, non_interactive: bool = False) -> None:
|
|
139
|
+
if non_interactive:
|
|
140
|
+
if not project_name:
|
|
141
|
+
console.print("[red]--non-interactive requires a project name[/red]")
|
|
142
|
+
raise SystemExit(2)
|
|
143
|
+
from spinup_py.prompts import build_default_config
|
|
144
|
+
|
|
145
|
+
config = build_default_config(project_name)
|
|
146
|
+
else:
|
|
147
|
+
from spinup_py.prompts import run_prompts
|
|
148
|
+
|
|
149
|
+
config = run_prompts(project_name)
|
|
150
|
+
dest_dir = str(Path.cwd() / config.project_name)
|
|
151
|
+
scaffold(dest_dir, config, non_interactive=non_interactive)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class _CommandError(Exception):
|
|
155
|
+
pass
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _run(cmd: list[str], cwd: Path) -> None:
|
|
159
|
+
result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True)
|
|
160
|
+
if result.returncode != 0:
|
|
161
|
+
raise _CommandError(f"Command failed: {' '.join(cmd)}\n{result.stderr}")
|
spinup_py/types.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Any, Literal
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel, ConfigDict, field_validator, model_validator
|
|
5
|
+
|
|
6
|
+
PythonVersion = Literal["3.12", "3.11", "3.10"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UserDefaults(BaseModel):
|
|
10
|
+
author: str = ""
|
|
11
|
+
email: str = ""
|
|
12
|
+
github_handle: str = ""
|
|
13
|
+
python_version: PythonVersion = "3.12"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProjectConfig(BaseModel):
|
|
17
|
+
model_config = ConfigDict(frozen=True)
|
|
18
|
+
|
|
19
|
+
project_name: str
|
|
20
|
+
project_slug: str = ""
|
|
21
|
+
description: str
|
|
22
|
+
author: str
|
|
23
|
+
email: str
|
|
24
|
+
github_handle: str
|
|
25
|
+
python_version: PythonVersion = "3.12"
|
|
26
|
+
include_github_actions: bool = True
|
|
27
|
+
publish_to_pypi: bool = False
|
|
28
|
+
deptry: bool = True
|
|
29
|
+
include_docs: bool = False
|
|
30
|
+
codecov: bool = False
|
|
31
|
+
dockerfile: bool = False
|
|
32
|
+
devcontainer: bool = True
|
|
33
|
+
open_source_license: Literal[
|
|
34
|
+
"MIT license",
|
|
35
|
+
"BSD license",
|
|
36
|
+
"ISC license",
|
|
37
|
+
"Apache Software License 2.0",
|
|
38
|
+
"GNU General Public License v3",
|
|
39
|
+
"Not open source",
|
|
40
|
+
] = "MIT license"
|
|
41
|
+
|
|
42
|
+
@field_validator("project_name")
|
|
43
|
+
@classmethod
|
|
44
|
+
def _validate_project_name(cls, v: str) -> str:
|
|
45
|
+
if not re.match(r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?$", v):
|
|
46
|
+
msg = "project_name must be kebab-case (lowercase letters, digits, hyphens; no leading/trailing hyphens)"
|
|
47
|
+
raise ValueError(msg)
|
|
48
|
+
return v
|
|
49
|
+
|
|
50
|
+
@model_validator(mode="before")
|
|
51
|
+
@classmethod
|
|
52
|
+
def _derive_slug(cls, data: Any) -> Any:
|
|
53
|
+
# Derive before construction so the slug stays consistent with
|
|
54
|
+
# project_name even though the model is frozen (no post-init mutation).
|
|
55
|
+
if isinstance(data, dict) and not data.get("project_slug"):
|
|
56
|
+
name = str(data.get("project_name", ""))
|
|
57
|
+
data["project_slug"] = name.lower().replace("-", "_")
|
|
58
|
+
return data
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def create(
|
|
62
|
+
cls,
|
|
63
|
+
*,
|
|
64
|
+
project_name: str,
|
|
65
|
+
description: str,
|
|
66
|
+
author: str,
|
|
67
|
+
email: str,
|
|
68
|
+
github_handle: str,
|
|
69
|
+
python_version: PythonVersion = "3.12",
|
|
70
|
+
include_github_actions: bool = True,
|
|
71
|
+
publish_to_pypi: bool = False,
|
|
72
|
+
deptry: bool = True,
|
|
73
|
+
include_docs: bool = False,
|
|
74
|
+
codecov: bool = False,
|
|
75
|
+
dockerfile: bool = False,
|
|
76
|
+
devcontainer: bool = True,
|
|
77
|
+
open_source_license: str = "MIT license",
|
|
78
|
+
) -> "ProjectConfig":
|
|
79
|
+
return cls(
|
|
80
|
+
project_name=project_name,
|
|
81
|
+
description=description,
|
|
82
|
+
author=author,
|
|
83
|
+
email=email,
|
|
84
|
+
github_handle=github_handle,
|
|
85
|
+
python_version=python_version,
|
|
86
|
+
include_github_actions=include_github_actions,
|
|
87
|
+
publish_to_pypi=publish_to_pypi,
|
|
88
|
+
deptry=deptry,
|
|
89
|
+
include_docs=include_docs,
|
|
90
|
+
codecov=codecov,
|
|
91
|
+
dockerfile=dockerfile,
|
|
92
|
+
devcontainer=devcontainer,
|
|
93
|
+
open_source_license=open_source_license, # type: ignore[arg-type]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
def to_cookiecutter_dict(self) -> dict[str, str]:
|
|
97
|
+
def yn(v: bool) -> str:
|
|
98
|
+
return "y" if v else "n"
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
"author": self.author,
|
|
102
|
+
"email": self.email,
|
|
103
|
+
"github_handle": self.github_handle,
|
|
104
|
+
"project_name": self.project_name,
|
|
105
|
+
"project_slug": self.project_slug,
|
|
106
|
+
"description": self.description,
|
|
107
|
+
"python_version": self.python_version,
|
|
108
|
+
"include_github_actions": yn(self.include_github_actions),
|
|
109
|
+
"publish_to_pypi": yn(self.publish_to_pypi),
|
|
110
|
+
"deptry": yn(self.deptry),
|
|
111
|
+
"include_docs": yn(self.include_docs),
|
|
112
|
+
"codecov": yn(self.codecov),
|
|
113
|
+
"dockerfile": yn(self.dockerfile),
|
|
114
|
+
"devcontainer": yn(self.devcontainer),
|
|
115
|
+
"open_source_license": self.open_source_license,
|
|
116
|
+
}
|
spinup_py/update.py
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import shutil
|
|
3
|
+
import subprocess
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import questionary
|
|
9
|
+
import tomlkit
|
|
10
|
+
from rich.console import Console
|
|
11
|
+
from rich.table import Table
|
|
12
|
+
from tomlkit.items import Array
|
|
13
|
+
|
|
14
|
+
console = Console()
|
|
15
|
+
|
|
16
|
+
# Template source: {{cookiecutter.project_name}}/ relative to the project root
|
|
17
|
+
_TEMPLATE_ROOT = Path(__file__).parent.parent.parent / "{{cookiecutter.project_name}}"
|
|
18
|
+
|
|
19
|
+
# ── TOML blocks injected into pyproject.toml ─────────────────────────────────
|
|
20
|
+
|
|
21
|
+
_RUFF_TOML = """\
|
|
22
|
+
[tool.ruff]
|
|
23
|
+
target-version = "py312"
|
|
24
|
+
line-length = 100
|
|
25
|
+
fix = true
|
|
26
|
+
|
|
27
|
+
[tool.ruff.lint]
|
|
28
|
+
select = [
|
|
29
|
+
"A", # flake8-builtins
|
|
30
|
+
"B", # flake8-bugbear
|
|
31
|
+
"C4", # flake8-comprehensions
|
|
32
|
+
"C90", # maccabe
|
|
33
|
+
"COM", # flake8-commas
|
|
34
|
+
"D", # pydocstyle
|
|
35
|
+
"DTZ", # flake8-datetimez
|
|
36
|
+
"E", # pycodestyle
|
|
37
|
+
"ERA", # flake8-eradicate
|
|
38
|
+
"EXE", # flake8-executable
|
|
39
|
+
"F", # pyflakes
|
|
40
|
+
"FBT", # flake8-boolean-trap
|
|
41
|
+
"FLY", # pyflint
|
|
42
|
+
"FURB", # refurb
|
|
43
|
+
"G", # flake8-logging-format
|
|
44
|
+
"I", # isort
|
|
45
|
+
"ICN", # flake8-import-conventions
|
|
46
|
+
"ISC", # flake8-implicit-str-concat
|
|
47
|
+
"LOG", # flake8-logging
|
|
48
|
+
"N", # pep8-naming
|
|
49
|
+
"PERF", # perflint
|
|
50
|
+
"PIE", # flake8-pie
|
|
51
|
+
"PL", # pylint
|
|
52
|
+
"PT", # flake8-pytest-style
|
|
53
|
+
"PTH", # flake8-use-pathlib
|
|
54
|
+
"Q", # flake8-quotes
|
|
55
|
+
"RET", # flake8-return
|
|
56
|
+
"RSE", # flake8-raise
|
|
57
|
+
"RUF", # ruff
|
|
58
|
+
"S", # flake8-bandit
|
|
59
|
+
"SIM", # flake8-simpify
|
|
60
|
+
"SLF", # flake8-self
|
|
61
|
+
"SLOT", # flake8-slots
|
|
62
|
+
"T100", # flake8-debugger
|
|
63
|
+
"TRY", # tryceratops
|
|
64
|
+
"UP", # pyupgrade
|
|
65
|
+
"W", # pycodestyle
|
|
66
|
+
"YTT", # flake8-2020
|
|
67
|
+
]
|
|
68
|
+
ignore = [
|
|
69
|
+
"A005", "COM812", "D100", "D104", "D106", "D203", "D212", "D401",
|
|
70
|
+
"D404", "D405", "E501", "E731", "ISC001", "ISC003", "PLR09",
|
|
71
|
+
"PLR2004", "PLR6301", "TRY003",
|
|
72
|
+
]
|
|
73
|
+
external = ["WPS"]
|
|
74
|
+
|
|
75
|
+
[tool.ruff.lint.pydocstyle]
|
|
76
|
+
convention = "google"
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint.flake8-quotes]
|
|
79
|
+
inline-quotes = "double"
|
|
80
|
+
|
|
81
|
+
[tool.ruff.lint.mccabe]
|
|
82
|
+
max-complexity = 24
|
|
83
|
+
|
|
84
|
+
[tool.ruff.lint.per-file-ignores]
|
|
85
|
+
"__init__.py" = ["F401", "F403"]
|
|
86
|
+
"tests/*.py" = ["S101", "S105", "S404", "S603", "S607", "D103"]
|
|
87
|
+
|
|
88
|
+
[tool.ruff.format]
|
|
89
|
+
preview = true
|
|
90
|
+
quote-style = "double"
|
|
91
|
+
indent-style = "space"
|
|
92
|
+
docstring-code-format = false
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
_MYPY_TOML = """\
|
|
96
|
+
[tool.mypy]
|
|
97
|
+
files = ["src"]
|
|
98
|
+
plugins = ["pydantic.mypy"]
|
|
99
|
+
strict = true
|
|
100
|
+
disallow_untyped_defs = true
|
|
101
|
+
disallow_any_unimported = true
|
|
102
|
+
no_implicit_optional = true
|
|
103
|
+
check_untyped_defs = true
|
|
104
|
+
warn_return_any = true
|
|
105
|
+
warn_unused_ignores = true
|
|
106
|
+
show_error_codes = true
|
|
107
|
+
warn_unreachable = true
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
_PYTEST_TOML = """\
|
|
111
|
+
[tool.pytest.ini_options]
|
|
112
|
+
pythonpath = ["src"]
|
|
113
|
+
testpaths = ["tests"]
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
_COVERAGE_TOML = """\
|
|
117
|
+
[tool.coverage.run]
|
|
118
|
+
branch = true
|
|
119
|
+
source = ["src"]
|
|
120
|
+
|
|
121
|
+
[tool.coverage.report]
|
|
122
|
+
skip_empty = true
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
# ── Helpers ───────────────────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@dataclass
|
|
129
|
+
class UpdateOption:
|
|
130
|
+
value: str
|
|
131
|
+
label: str
|
|
132
|
+
hint: str
|
|
133
|
+
apply: Callable[[Path], None]
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _get_project_name(target: Path) -> str:
|
|
137
|
+
pyproject = target / "pyproject.toml"
|
|
138
|
+
if pyproject.exists():
|
|
139
|
+
try:
|
|
140
|
+
doc = tomlkit.parse(pyproject.read_text())
|
|
141
|
+
return str(doc["project"]["name"])
|
|
142
|
+
except KeyError:
|
|
143
|
+
console.print(f"[dim]No [project].name in {pyproject}; falling back to directory name[/dim]")
|
|
144
|
+
except (tomlkit.exceptions.TOMLKitError, OSError) as exc:
|
|
145
|
+
console.print(f"[yellow]Warning: could not read project name from {pyproject}: {exc}[/yellow]")
|
|
146
|
+
return target.name
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _has_toml_section(pyproject_path: Path, *keys: str) -> bool:
|
|
150
|
+
if not pyproject_path.exists():
|
|
151
|
+
return False
|
|
152
|
+
try:
|
|
153
|
+
node: object = tomlkit.parse(pyproject_path.read_text())
|
|
154
|
+
for key in keys:
|
|
155
|
+
node = node[key] # type: ignore[index]
|
|
156
|
+
return True
|
|
157
|
+
except KeyError:
|
|
158
|
+
return False
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _append_toml(pyproject_path: Path, content: str) -> None:
|
|
162
|
+
text = pyproject_path.read_text()
|
|
163
|
+
pyproject_path.write_text(text.rstrip() + "\n\n" + content.strip() + "\n")
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _copy_dir(src_rel: str, target: Path, project_name: str) -> None:
|
|
167
|
+
src = _TEMPLATE_ROOT / src_rel
|
|
168
|
+
dst = target / src_rel
|
|
169
|
+
if not src.exists():
|
|
170
|
+
console.print(f"[yellow]Template source not found: {src}[/yellow]")
|
|
171
|
+
return
|
|
172
|
+
if dst.exists():
|
|
173
|
+
answer = questionary.confirm(
|
|
174
|
+
f"Update existing {dst.name}/ directory? (template files overwritten, your other files kept)",
|
|
175
|
+
default=False,
|
|
176
|
+
).ask()
|
|
177
|
+
if answer is None: # Ctrl+C / ESC — treat as an explicit cancellation.
|
|
178
|
+
console.print("[yellow]Cancelled.[/yellow]")
|
|
179
|
+
raise SystemExit(1)
|
|
180
|
+
if not answer:
|
|
181
|
+
console.print(f"[yellow]Skipping {src_rel}[/yellow]")
|
|
182
|
+
return
|
|
183
|
+
# Merge into the destination rather than deleting it, so files the user
|
|
184
|
+
# added alongside the template (e.g. extra docs pages) are preserved.
|
|
185
|
+
shutil.copytree(src, dst, dirs_exist_ok=True)
|
|
186
|
+
_substitute_vars(dst, project_name)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _copy_file(src_rel: str, target: Path, project_name: str) -> None:
|
|
190
|
+
src = _TEMPLATE_ROOT / src_rel
|
|
191
|
+
dst = target / src_rel
|
|
192
|
+
if not src.exists():
|
|
193
|
+
console.print(f"[yellow]Template source not found: {src}[/yellow]")
|
|
194
|
+
return
|
|
195
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
196
|
+
text = _render_template(src.read_text(encoding="utf-8"), project_name)
|
|
197
|
+
dst.write_text(text, encoding="utf-8")
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _render_template(text: str, project_name: str) -> str:
|
|
201
|
+
text = text.replace("{{cookiecutter.project_name}}", project_name)
|
|
202
|
+
text = text.replace("{{cookiecutter.project_slug}}", project_name.replace("-", "_"))
|
|
203
|
+
# Strip remaining unresolved cookiecutter expressions
|
|
204
|
+
text = re.sub(r"\{\{cookiecutter\.[^}]+\}\}", "", text)
|
|
205
|
+
return text
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _substitute_vars(directory: Path, project_name: str) -> None:
|
|
209
|
+
for f in directory.rglob("*"):
|
|
210
|
+
if f.is_file():
|
|
211
|
+
try:
|
|
212
|
+
f.write_text(_render_template(f.read_text(encoding="utf-8"), project_name))
|
|
213
|
+
except UnicodeDecodeError:
|
|
214
|
+
# Binary file (image, compiled artifact) — no template tokens to
|
|
215
|
+
# substitute. Logged so skipped files are traceable.
|
|
216
|
+
console.print(f"[dim]Skipping non-text file: {f}[/dim]")
|
|
217
|
+
except PermissionError as exc:
|
|
218
|
+
console.print(f"[yellow]Warning: could not update {f}: {exc}[/yellow]")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _has_dev_dependency(pyproject_path: Path, name: str) -> bool:
|
|
222
|
+
"""Return True if ``name`` is listed in ``dependency-groups.dev``.
|
|
223
|
+
|
|
224
|
+
Matches by package name (start of the requirement string) so it is not
|
|
225
|
+
fooled by the name appearing in a comment or an unrelated section.
|
|
226
|
+
"""
|
|
227
|
+
try:
|
|
228
|
+
doc = tomlkit.parse(pyproject_path.read_text())
|
|
229
|
+
dev_deps = doc["dependency-groups"]["dev"]
|
|
230
|
+
except KeyError:
|
|
231
|
+
return False
|
|
232
|
+
pattern = re.compile(rf"^{re.escape(name)}(?:[<>=~!\[;\s]|$)", re.IGNORECASE)
|
|
233
|
+
return any(pattern.match(str(dep).strip()) for dep in dev_deps)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _add_deptry(pyproject_path: Path) -> None:
|
|
237
|
+
doc = tomlkit.parse(pyproject_path.read_text())
|
|
238
|
+
try:
|
|
239
|
+
dep_groups = doc["dependency-groups"]
|
|
240
|
+
except KeyError:
|
|
241
|
+
_append_toml(pyproject_path, '[dependency-groups]\ndev = ["deptry>=0.23.0"]\n')
|
|
242
|
+
return
|
|
243
|
+
dev_deps = dep_groups.get("dev")
|
|
244
|
+
if not isinstance(dev_deps, Array):
|
|
245
|
+
# Missing, scalar, or inline-table `dev` — replace with a proper array
|
|
246
|
+
# rather than calling .append() on a non-array and crashing.
|
|
247
|
+
dep_groups["dev"] = ["deptry>=0.23.0"]
|
|
248
|
+
elif not any("deptry" in str(dep) for dep in dev_deps):
|
|
249
|
+
dev_deps.append("deptry>=0.23.0")
|
|
250
|
+
pyproject_path.write_text(tomlkit.dumps(doc))
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _run_beads(target: Path) -> None:
|
|
254
|
+
try:
|
|
255
|
+
result = subprocess.run(
|
|
256
|
+
["bd", "init", "--skip-agents", "--non-interactive"],
|
|
257
|
+
cwd=target,
|
|
258
|
+
capture_output=True,
|
|
259
|
+
check=False,
|
|
260
|
+
)
|
|
261
|
+
if result.returncode != 0:
|
|
262
|
+
console.print("[yellow]bd init failed — check beads CLI installation[/yellow]")
|
|
263
|
+
except OSError:
|
|
264
|
+
console.print("[yellow]bd not found or not executable — install the beads CLI to use this feature[/yellow]")
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def _init_serena(target: Path, project_name: str) -> None:
|
|
268
|
+
serena_dir = target / ".serena"
|
|
269
|
+
serena_dir.mkdir(exist_ok=True)
|
|
270
|
+
(serena_dir / "project.yml").write_text(f"project_name: {project_name}\nlanguage: python\n")
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
# ── Detection ─────────────────────────────────────────────────────────────────
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _write_file(path: Path, content: str) -> None:
|
|
277
|
+
path.write_text(content)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def detect_available_updates(target: Path) -> list[UpdateOption]:
|
|
281
|
+
project_name = _get_project_name(target)
|
|
282
|
+
pyproject = target / "pyproject.toml"
|
|
283
|
+
options: list[UpdateOption] = []
|
|
284
|
+
|
|
285
|
+
if not (target / ".devcontainer").exists():
|
|
286
|
+
options.append(
|
|
287
|
+
UpdateOption(
|
|
288
|
+
value="devcontainer",
|
|
289
|
+
label="Add .devcontainer/",
|
|
290
|
+
hint="Full Claude Code dev environment (custom image, mounts, API keys)",
|
|
291
|
+
apply=lambda t: _copy_dir(".devcontainer", t, project_name),
|
|
292
|
+
)
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
if not (target / "AGENTS.md").exists():
|
|
296
|
+
options.append(
|
|
297
|
+
UpdateOption(
|
|
298
|
+
value="agents_md",
|
|
299
|
+
label="Add AGENTS.md",
|
|
300
|
+
hint="Agent instruction file (Codex, Gemini, Cursor, Copilot)",
|
|
301
|
+
apply=lambda t: _copy_file("AGENTS.md", t, project_name),
|
|
302
|
+
)
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
if not (target / "CLAUDE.md").exists():
|
|
306
|
+
options.append(
|
|
307
|
+
UpdateOption(
|
|
308
|
+
value="claude_md",
|
|
309
|
+
label="Add CLAUDE.md",
|
|
310
|
+
hint="Single-line @AGENTS.md import for Claude Code",
|
|
311
|
+
apply=lambda t: _write_file(t / "CLAUDE.md", "@AGENTS.md\n"),
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
if not (target / ".github" / "workflows").exists():
|
|
316
|
+
options.append(
|
|
317
|
+
UpdateOption(
|
|
318
|
+
value="github_actions",
|
|
319
|
+
label="Add GitHub Actions CI (.github/workflows/)",
|
|
320
|
+
hint="Lint, typecheck, test, build",
|
|
321
|
+
apply=lambda t: _copy_dir(".github", t, project_name),
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
elif not (target / ".github" / "workflows" / "on-release-main.yml").exists():
|
|
325
|
+
options.append(
|
|
326
|
+
UpdateOption(
|
|
327
|
+
value="pypi_publish",
|
|
328
|
+
label="Add PyPI publish workflow",
|
|
329
|
+
hint=".github/workflows/on-release-main.yml",
|
|
330
|
+
apply=lambda t: _copy_file(".github/workflows/on-release-main.yml", t, project_name),
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
if pyproject.exists() and not _has_toml_section(pyproject, "tool", "ruff"):
|
|
335
|
+
options.append(
|
|
336
|
+
UpdateOption(
|
|
337
|
+
value="ruff",
|
|
338
|
+
label="Add ruff config to pyproject.toml",
|
|
339
|
+
hint="Full linting ruleset with WPS support, google docstrings",
|
|
340
|
+
apply=lambda t: _append_toml(t / "pyproject.toml", _RUFF_TOML),
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
if pyproject.exists() and not _has_toml_section(pyproject, "tool", "mypy"):
|
|
345
|
+
options.append(
|
|
346
|
+
UpdateOption(
|
|
347
|
+
value="mypy",
|
|
348
|
+
label="Add mypy config to pyproject.toml",
|
|
349
|
+
hint="Strict typing with pydantic plugin",
|
|
350
|
+
apply=lambda t: _append_toml(t / "pyproject.toml", _MYPY_TOML),
|
|
351
|
+
)
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
if pyproject.exists() and not _has_toml_section(pyproject, "tool", "pytest"):
|
|
355
|
+
options.append(
|
|
356
|
+
UpdateOption(
|
|
357
|
+
value="pytest",
|
|
358
|
+
label="Add pytest config to pyproject.toml",
|
|
359
|
+
hint='pythonpath = ["src"], testpaths = ["tests"]',
|
|
360
|
+
apply=lambda t: _append_toml(t / "pyproject.toml", _PYTEST_TOML),
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
if pyproject.exists() and not _has_toml_section(pyproject, "tool", "coverage"):
|
|
365
|
+
options.append(
|
|
366
|
+
UpdateOption(
|
|
367
|
+
value="coverage",
|
|
368
|
+
label="Add coverage config to pyproject.toml",
|
|
369
|
+
hint="Branch coverage, src source",
|
|
370
|
+
apply=lambda t: _append_toml(t / "pyproject.toml", _COVERAGE_TOML),
|
|
371
|
+
)
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
if not (target / ".pre-commit-config.yaml").exists():
|
|
375
|
+
options.append(
|
|
376
|
+
UpdateOption(
|
|
377
|
+
value="pre_commit",
|
|
378
|
+
label="Add .pre-commit-config.yaml",
|
|
379
|
+
hint="ruff + WPS + prettier hooks",
|
|
380
|
+
apply=lambda t: _copy_file(".pre-commit-config.yaml", t, project_name),
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
if pyproject.exists() and not _has_dev_dependency(pyproject, "deptry"):
|
|
385
|
+
options.append(
|
|
386
|
+
UpdateOption(
|
|
387
|
+
value="deptry",
|
|
388
|
+
label="Add deptry to dev dependencies",
|
|
389
|
+
hint="Detects unused / missing / misplaced dependencies",
|
|
390
|
+
apply=lambda t: _add_deptry(t / "pyproject.toml"),
|
|
391
|
+
)
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
if not (target / "docs").exists():
|
|
395
|
+
options.append(
|
|
396
|
+
UpdateOption(
|
|
397
|
+
value="docs",
|
|
398
|
+
label="Add Docusaurus docs site (docs/)",
|
|
399
|
+
hint="Copy Docusaurus scaffold from template",
|
|
400
|
+
apply=lambda t: _copy_dir("docs", t, project_name),
|
|
401
|
+
)
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
if not (target / "Dockerfile").exists():
|
|
405
|
+
|
|
406
|
+
def _add_dockerfile(t: Path) -> None:
|
|
407
|
+
# The hardened Dockerfile relies on .dockerignore to keep secrets and
|
|
408
|
+
# local dirs out of `COPY . /app`, so ship both together.
|
|
409
|
+
_copy_file("Dockerfile", t, project_name)
|
|
410
|
+
_copy_file(".dockerignore", t, project_name)
|
|
411
|
+
|
|
412
|
+
options.append(
|
|
413
|
+
UpdateOption(
|
|
414
|
+
value="dockerfile",
|
|
415
|
+
label="Add Dockerfile",
|
|
416
|
+
hint="Multi-stage Python build",
|
|
417
|
+
apply=_add_dockerfile,
|
|
418
|
+
)
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
if not (target / ".beads").exists():
|
|
422
|
+
options.append(
|
|
423
|
+
UpdateOption(
|
|
424
|
+
value="beads",
|
|
425
|
+
label="Initialize Beads task manager",
|
|
426
|
+
hint="Runs: bd init --skip-agents",
|
|
427
|
+
apply=lambda t: _run_beads(t),
|
|
428
|
+
)
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
if not (target / ".serena" / "project.yml").exists():
|
|
432
|
+
options.append(
|
|
433
|
+
UpdateOption(
|
|
434
|
+
value="serena",
|
|
435
|
+
label="Initialize Serena project config",
|
|
436
|
+
hint="Creates .serena/project.yml",
|
|
437
|
+
apply=lambda t: _init_serena(t, project_name),
|
|
438
|
+
)
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
return options
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
# ── Entry point ───────────────────────────────────────────────────────────────
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def update_project(target_dir: str) -> None:
|
|
448
|
+
target = Path(target_dir).resolve()
|
|
449
|
+
if not target.exists():
|
|
450
|
+
console.print(f"[red]Directory not found: {target}[/red]")
|
|
451
|
+
raise SystemExit(1)
|
|
452
|
+
|
|
453
|
+
console.rule("[bold blue]spinup-py update[/bold blue]")
|
|
454
|
+
console.print(f"[dim]Target: {target}[/dim]\n")
|
|
455
|
+
|
|
456
|
+
available = detect_available_updates(target)
|
|
457
|
+
if not available:
|
|
458
|
+
console.print("[green]✓ Project is already up to date![/green]")
|
|
459
|
+
return
|
|
460
|
+
|
|
461
|
+
choices = [questionary.Choice(f"{o.label} [dim]{o.hint}[/dim]", value=o.value, checked=True) for o in available]
|
|
462
|
+
selected_values: list[str] = questionary.checkbox("Select updates to apply:", choices=choices).ask()
|
|
463
|
+
|
|
464
|
+
if not selected_values:
|
|
465
|
+
console.print("[yellow]No updates selected.[/yellow]")
|
|
466
|
+
return
|
|
467
|
+
|
|
468
|
+
selected = [o for o in available if o.value in selected_values]
|
|
469
|
+
|
|
470
|
+
table = Table(show_header=False, box=None, padding=(0, 2))
|
|
471
|
+
table.add_column(style="bold")
|
|
472
|
+
table.add_column(style="dim")
|
|
473
|
+
|
|
474
|
+
for opt in selected:
|
|
475
|
+
try:
|
|
476
|
+
opt.apply(target)
|
|
477
|
+
table.add_row("[green]✓[/green]", opt.label)
|
|
478
|
+
# Only swallow expected runtime failures (file IO, TOML, subprocess);
|
|
479
|
+
# programming errors (AttributeError, TypeError, ...) propagate so a
|
|
480
|
+
# broken apply() shows a traceback instead of a silent "failed" row.
|
|
481
|
+
except (OSError, tomlkit.exceptions.TOMLKitError, subprocess.SubprocessError) as exc:
|
|
482
|
+
table.add_row("[red]✗[/red]", f"{opt.label} — {exc}")
|
|
483
|
+
|
|
484
|
+
console.print()
|
|
485
|
+
console.print(table)
|
|
486
|
+
console.print()
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spinup-py
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Scaffold and maintain production-ready Python projects with a rich CLI and Claude Code plugin.
|
|
5
|
+
Author-email: Joe Black <me@joeblack.nyc>
|
|
6
|
+
Project-URL: Repository, https://github.com/joeblackwaslike/spinup-py
|
|
7
|
+
Project-URL: Documentation, https://joeblackwaslike.github.io/spinup-py/
|
|
8
|
+
Project-URL: Homepage, https://joeblackwaslike.github.io/spinup-py/
|
|
9
|
+
Keywords: python,scaffold,cli,template,uv,cookiecutter
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
21
|
+
Requires-Python: <4.0,>=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: cookiecutter>=2.1.1
|
|
25
|
+
Requires-Dist: typer[all]>=0.12
|
|
26
|
+
Requires-Dist: questionary>=2.0
|
|
27
|
+
Requires-Dist: pydantic>=2.0
|
|
28
|
+
Requires-Dist: tomlkit>=0.12
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
<p align="center">
|
|
32
|
+
<img width="600" src="https://raw.githubusercontent.com/joeblackwaslike/spinup-py/main/docs/static/cookiecutter.svg">
|
|
33
|
+
</p style = "margin-bottom: 2rem;">
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
[](https://github.com/joeblackwaslike/spinup-py/actions/workflows/main.yml?query=branch%3Amain)
|
|
38
|
+
[](https://github.com/joeblackwaslike/spinup-py/blob/main/pyproject.toml)
|
|
39
|
+
[](https://joeblackwaslike.github.io/spinup-py/)
|
|
40
|
+
[](https://img.shields.io/github/license/joeblackwaslike/spinup-py)
|
|
41
|
+
|
|
42
|
+
This is a modern Cookiecutter template that can be used to initiate a Python project with all the necessary tools for development, testing, and deployment. It supports the following features:
|
|
43
|
+
|
|
44
|
+
- [uv](https://docs.astral.sh/uv/) for dependency management
|
|
45
|
+
- Supports [src layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/)
|
|
46
|
+
- CI/CD with [GitHub Actions](https://github.com/features/actions)
|
|
47
|
+
- Pre-commit hooks with [pre-commit](https://pre-commit.com/)
|
|
48
|
+
- Code quality with [ruff](https://github.com/charliermarsh/ruff), [mypy](https://mypy.readthedocs.io/en/stable/), [wemake-python-styleguide](https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/setup.html), [deptry](https://github.com/joeblackwaslike/deptry/) and [prettier](https://prettier.io/)
|
|
49
|
+
- Publishing to [PyPI](https://pypi.org) by creating a new release on GitHub
|
|
50
|
+
- Testing and coverage with [pytest](https://docs.pytest.org/en/7.1.x/) and [codecov](https://about.codecov.io/)
|
|
51
|
+
- Documentation with [Docusaurus](https://docusaurus.io/)
|
|
52
|
+
- Compatibility testing for multiple versions of Python with [tox-uv](https://github.com/tox-dev/tox-uv)
|
|
53
|
+
- Containerization with [Docker](https://www.docker.com/)
|
|
54
|
+
- Development environment with [VSCode devcontainers](https://code.visualstudio.com/docs/devcontainers/containers)
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
<p align="center">
|
|
59
|
+
<a href="https://joeblackwaslike.github.io/spinup-py/">Documentation</a> - <a href="https://github.com/joeblackwaslike/spinup-py-example">Example</a>
|
|
60
|
+
</p>
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Quickstart
|
|
65
|
+
|
|
66
|
+
[](https://discord.com/channels/1486035859747897414/1509515362012626944) [](https://discord.gg/Fjc9zYHZyV)
|
|
67
|
+
|
|
68
|
+
Navigate to the directory where you want your new project and run:
|
|
69
|
+
|
|
70
|
+
```shell
|
|
71
|
+
uvx spinup-py my-project
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Or install globally first:
|
|
75
|
+
|
|
76
|
+
```shell
|
|
77
|
+
uv tool install spinup-py
|
|
78
|
+
spinup-py my-project
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Follow the guided prompts to configure your project. Once complete, a new directory `my-project/` will be created with everything set up.
|
|
82
|
+
|
|
83
|
+
To retrofit an existing project with the latest tooling:
|
|
84
|
+
|
|
85
|
+
```shell
|
|
86
|
+
spinup-py --update /path/to/existing-project
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
> `spinup-py` is the Python sibling of [`spinup-ts`](https://www.npmjs.com/package/spinup-ts), the equivalent TypeScript project scaffolder.
|
|
90
|
+
|
|
91
|
+
### Install from source
|
|
92
|
+
|
|
93
|
+
For local or development use (e.g. running it as a Claude Code plugin), install the checkout directly with an editable install:
|
|
94
|
+
|
|
95
|
+
```shell
|
|
96
|
+
uv tool install --editable .
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Usage
|
|
100
|
+
|
|
101
|
+
```shell
|
|
102
|
+
# Scaffold a new project (bare form — positional name)
|
|
103
|
+
spinup-py my-project
|
|
104
|
+
|
|
105
|
+
# Same thing via the explicit subcommand
|
|
106
|
+
spinup-py new my-project
|
|
107
|
+
|
|
108
|
+
# Retrofit an existing project with the current tooling
|
|
109
|
+
spinup-py update /path/to/existing-project
|
|
110
|
+
|
|
111
|
+
# Or via the root flag (equivalent to `update`)
|
|
112
|
+
spinup-py --update /path/to/existing-project
|
|
113
|
+
|
|
114
|
+
# Print the installed version
|
|
115
|
+
spinup-py --version
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Flags
|
|
119
|
+
|
|
120
|
+
| Flag / command | Description |
|
|
121
|
+
| -------------------------- | ---------------------------------------------------------------------------- |
|
|
122
|
+
| `spinup-py <name>` | Scaffold a new project with the given name (runs the guided prompts). |
|
|
123
|
+
| `spinup-py new [<name>]` | Explicit subcommand for scaffolding a new project. |
|
|
124
|
+
| `spinup-py update [<dir>]` | Retrofit an existing project at `<dir>` (defaults to the current directory). |
|
|
125
|
+
| `--update`, `-u <dir>` | Root-level equivalent of `update` — retrofit an existing project at `<dir>`. |
|
|
126
|
+
| `--non-interactive`, `-y` | Scaffold with sensible defaults and no prompts. |
|
|
127
|
+
| `--version`, `-v` | Print the installed version and exit. |
|
|
128
|
+
|
|
129
|
+
### Offline / custom template
|
|
130
|
+
|
|
131
|
+
By default the published CLI fetches its Cookiecutter template from GitHub (`gh:joeblackwaslike/spinup-py`). To use a local or custom template instead — handy when working offline or from a fork — set the `SPINUP_PY_TEMPLATE` environment variable to any Cookiecutter reference (a local path, a different GitHub repo, etc.):
|
|
132
|
+
|
|
133
|
+
```shell
|
|
134
|
+
SPINUP_PY_TEMPLATE=/path/to/template spinup-py my-project
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Acknowledgements & Lineage
|
|
138
|
+
|
|
139
|
+
- This project is partially based on [Florian Maas's](https://github.com/fpgmaas) great
|
|
140
|
+
[cookiecutter-uv](https://github.com/fpgmaas/cookiecutter-uv)
|
|
141
|
+
repository. Many additional improvements have been added from all the projects I've worked on over the last 12 years or so.
|
|
142
|
+
- Florian also credits [Audrey Feldroy's](https://github.com/audreyfeldroy) great [cookiecutter-pypackage](https://github.com/audreyfeldroy/cookiecutter-pypackage) repository.
|
|
143
|
+
|
|
144
|
+
## Feedback and contributing
|
|
145
|
+
|
|
146
|
+
My mission with this is to build the best project starter for python projects that has everything you can want, the ability to customize and/or opt out of most everything, and save you the most time!
|
|
147
|
+
|
|
148
|
+
I am only one person with very specific preferences. If you have any feedback you can email me at `me@joeblack.nyc`. If you find any bugs, mistakes, missed opportunities, or would like to contribute new functionality in any way, I would love to hear about it in an issue, feel free to email me the issue to get my attention.
|
|
149
|
+
|
|
150
|
+
This project is under continuous iterative improvements as I find bugs, and better and more effective ways to do things. But I can only do so much on my own, so please open an issue if you want to help/contribute and we can iterate and discuss how we can make this happen. If you find an outright bug or mistake, just open a PR and describe all the details there.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
spinup_py/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
spinup_py/cli.py,sha256=bAy-5OItv0orgNibvtxd6MuXk-VPDM96SG6A5rn53dA,2287
|
|
3
|
+
spinup_py/prompts.py,sha256=fgKj5zRZvtiXntpX60W6q3CbJ89_xRP9qAMK0a9Nq8c,7056
|
|
4
|
+
spinup_py/scaffold.py,sha256=NqS_YWL-YT2vY0bDa5iPbDPxlcs0bbGiGy1fA7BsUGw,6154
|
|
5
|
+
spinup_py/types.py,sha256=5FmAz1iBP5HYtf54LEKzGi-PA0IRvkSA9LuFIw9XWeM,3826
|
|
6
|
+
spinup_py/update.py,sha256=PIUmKrivh3szHWVTiOQLAedK9UrdbBicmH2dge-Xw6g,16795
|
|
7
|
+
spinup_py-0.2.0.dist-info/licenses/LICENSE,sha256=dUhuoK-TCRQMpuLEAdfme-qPSJI0TlcH9jlNxeg9_EQ,1056
|
|
8
|
+
spinup_py-0.2.0.dist-info/METADATA,sha256=Y_KyJlCaIoEZmrnloLHCGR1m0dekRJlXKRsgiu0k2t8,7931
|
|
9
|
+
spinup_py-0.2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
10
|
+
spinup_py-0.2.0.dist-info/entry_points.txt,sha256=TKTB577NpHYnUUyJeqX8bjQYRSoXaq8iDzjMW2cm9m8,48
|
|
11
|
+
spinup_py-0.2.0.dist-info/top_level.txt,sha256=e39nqGFqbU1_iFZgUyXa_MMbQPwU61TAraWzASS_aOY,10
|
|
12
|
+
spinup_py-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spinup_py
|