copier-python 0.8.0__py3-none-any.whl → 0.9.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.
@@ -0,0 +1,140 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import shutil
5
+ from dataclasses import dataclass
6
+ from enum import Enum
7
+ from typing import TYPE_CHECKING, Annotated
8
+
9
+ from rich import print # noqa: A004
10
+ from rich.console import Console
11
+ from rich.panel import Panel
12
+ from rich.table import Table
13
+ from rich.text import Text
14
+ from typer import Argument, Context, Exit, Option, Typer
15
+
16
+ from .repo import RepoTarget
17
+ from .update import UpdateAction
18
+
19
+ if TYPE_CHECKING:
20
+ from collections.abc import Sequence
21
+
22
+
23
+ console = Console()
24
+
25
+
26
+ class Args:
27
+ Repos = Annotated[
28
+ list[str],
29
+ Argument(
30
+ help=(
31
+ "Repositories to update. Accepts gh:user/repo"
32
+ " or github.com/user/repo."
33
+ )
34
+ ),
35
+ ]
36
+ DryRun = Annotated[
37
+ bool, Option("--dry-run", "-n", help="Skip push and PR creation.")
38
+ ]
39
+
40
+
41
+ cli = Typer(
42
+ help="copier-python utilities",
43
+ add_completion=False,
44
+ no_args_is_help=True,
45
+ pretty_exceptions_enable=False,
46
+ )
47
+
48
+
49
+ @cli.callback()
50
+ def setup(ctx: Context) -> None:
51
+ pass
52
+
53
+
54
+ class UpdateStatus(Enum):
55
+ UPDATED = "bold green"
56
+ CURRENT = "bold blue"
57
+ FAILED = "bold red"
58
+
59
+ @property
60
+ def formatted_name(self) -> Text:
61
+ return Text(f"{self.name:<7}", style=self.value)
62
+
63
+
64
+ @dataclass
65
+ class UpdateResult:
66
+ repo: RepoTarget
67
+ status: UpdateStatus
68
+ exception: Exception | None = None
69
+ pr_url: str | None = None
70
+
71
+
72
+ @cli.command()
73
+ def update(
74
+ repos: Args.Repos,
75
+ *,
76
+ dry_run: Args.DryRun = False,
77
+ branch: Annotated[str, Option(help="Branch name to create.")] = "updates",
78
+ ) -> None:
79
+ """Apply copier-python template updates to one or more downstream repos."""
80
+ results = []
81
+
82
+ repo_targets = {(target := RepoTarget(repo)).url: target for repo in repos}
83
+ for target in repo_targets.values():
84
+ try:
85
+ pr_url = UpdateAction(target, branch=branch, dry_run=dry_run)()
86
+ if pr_url:
87
+ results.append(
88
+ UpdateResult(target, UpdateStatus.UPDATED, pr_url=pr_url)
89
+ )
90
+ else:
91
+ results.append(UpdateResult(target, UpdateStatus.CURRENT))
92
+ except Exception as exc: # noqa: BLE001, PERF203
93
+ console.print_exception()
94
+ results.append(
95
+ UpdateResult(target, status=UpdateStatus.FAILED, exception=exc)
96
+ )
97
+
98
+ _print_summary(results, dry_run=dry_run)
99
+ if any(r.status == UpdateStatus.FAILED for r in results):
100
+ raise Exit(1)
101
+
102
+
103
+ def _print_summary(results: Sequence[UpdateResult], *, dry_run: bool) -> None:
104
+ if not results:
105
+ return
106
+ grid = Table.grid(padding=(0, 1), expand=True)
107
+ grid.add_column()
108
+ grid.add_column()
109
+ grid.add_column()
110
+ for result in sorted(results, key=lambda r: r.repo.github_repo):
111
+ grid.add_row(
112
+ result.status.formatted_name,
113
+ Text.from_markup(
114
+ f"[link={result.repo.url}]{result.repo.github_repo}[/link]",
115
+ style="bold",
116
+ ),
117
+ Text(str(result.pr_url or result.exception)),
118
+ )
119
+ title = Text("Update Results", style="bold")
120
+ if dry_run:
121
+ title += Text(" (dry run)", style="green")
122
+ print(
123
+ Panel(grid, title=title, title_align="left", expand=False, padding=1)
124
+ )
125
+
126
+
127
+ def setup_env() -> None:
128
+ os.environ.pop("VIRTUAL_ENV", default=None)
129
+ os.environ["TERMINAL_WIDTH"] = str(
130
+ min(shutil.get_terminal_size().columns, 100)
131
+ )
132
+
133
+
134
+ def main() -> None:
135
+ setup_env()
136
+ cli()
137
+
138
+
139
+ if __name__ == "__main__":
140
+ main()
copier_python/repo.py ADDED
@@ -0,0 +1,156 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import re
5
+ import subprocess
6
+ import tempfile
7
+ from contextlib import contextmanager, suppress
8
+ from dataclasses import InitVar, dataclass, field
9
+ from functools import cached_property
10
+ from pathlib import Path
11
+ from typing import TYPE_CHECKING, Any, ClassVar
12
+
13
+ import yaml
14
+ from rich import print # noqa: A004
15
+ from rich.panel import Panel
16
+ from rich.text import Text
17
+ from typing_extensions import Self
18
+
19
+ if TYPE_CHECKING:
20
+ from collections.abc import Generator, Sequence
21
+
22
+
23
+ @dataclass(unsafe_hash=True)
24
+ class RepoTarget:
25
+ arg: InitVar[str | Path]
26
+ github_repo: str = field(init=False)
27
+
28
+ _GITHUB_REGEXES: ClassVar[Sequence[str]] = [
29
+ r"(\.git)?/?$",
30
+ r"^gh\:",
31
+ r"^(https?://)?github.com/",
32
+ r"^((git\+)?ssh://)?([a-zA-Z0-9]+)@github.com:",
33
+ ]
34
+
35
+ def __post_init__(self, arg: str | Path) -> None:
36
+ arg = str(arg).strip()
37
+ if ((path := Path(arg)).is_dir()) and (root := self.repo_root(path)):
38
+ arg = root
39
+ for regex in self._GITHUB_REGEXES:
40
+ arg = re.sub(regex, "", arg)
41
+ if not re.fullmatch(r"[\w-]+\/[\w-]+", arg):
42
+ raise ValueError(arg)
43
+ self.github_repo = arg
44
+
45
+ def repo_root(self, path: Path) -> str | None:
46
+ with suppress(subprocess.CalledProcessError):
47
+ return subprocess.check_output(
48
+ ["git", "config", "--local", "remote.origin.url"],
49
+ text=True,
50
+ cwd=path,
51
+ ).strip()
52
+ return None
53
+
54
+ @cached_property
55
+ def name(self) -> str:
56
+ return self.github_repo.split("/")[-1]
57
+
58
+ @property
59
+ def url(self) -> str:
60
+ return f"https://github.com/{self.github_repo}"
61
+
62
+ @cached_property
63
+ def push_url(self) -> str:
64
+ return f"git@github.com:{self.github_repo}.git"
65
+
66
+
67
+ @dataclass
68
+ class RepoWorktree:
69
+ path: Path
70
+ repo: RepoTarget
71
+ branch: str
72
+
73
+ @classmethod
74
+ @contextmanager
75
+ def clone(
76
+ cls, repo: RepoTarget, branch: str
77
+ ) -> Generator[Self, None, None]:
78
+ with tempfile.TemporaryDirectory() as td:
79
+ repo_dir = Path(td) / "worktree"
80
+ cls.run_in(["git", "clone", repo.url, str(repo_dir)], repo=repo)
81
+ for cmd in (
82
+ [
83
+ *["git", "remote", "set-url", "--push", "origin"],
84
+ repo.push_url,
85
+ ],
86
+ ["poe", "setup"],
87
+ ["git", "checkout", "-b", branch],
88
+ ):
89
+ cls.run_in(cmd, repo=repo, cwd=repo_dir)
90
+ yield cls(path=repo_dir, repo=repo, branch=branch)
91
+
92
+ @classmethod
93
+ def run_in(
94
+ cls, cmd: list[str], *, repo: RepoTarget, **kwargs: Any
95
+ ) -> subprocess.CompletedProcess[str]:
96
+ kwargs.setdefault("check", True)
97
+ kwargs.setdefault("text", True)
98
+ cmd_text = Text(" ".join(cmd), style="color(153)")
99
+ txt = Text.assemble(
100
+ Text(f"{repo.github_repo} => ", style="bold"),
101
+ cmd_text,
102
+ )
103
+ print(Panel(txt, expand=False, border_style="white dim"))
104
+ return subprocess.run(cmd, **kwargs) # noqa: S603 PLW1510
105
+
106
+ def run(
107
+ self, cmd: list[str], **kwargs: Any
108
+ ) -> subprocess.CompletedProcess[str]:
109
+ kwargs.setdefault("cwd", self.path)
110
+ return self.run_in(cmd, repo=self.repo, **kwargs)
111
+
112
+ def git_status(self) -> list[str]:
113
+ return self.run(
114
+ ["git", "status", "--porcelain"], capture_output=True
115
+ ).stdout.splitlines()
116
+
117
+ @staticmethod
118
+ def has_conflicts(status: list[str]) -> bool:
119
+ conflict_codes = {"UU", "AA", "DD", "AU", "UA", "DU", "UD"}
120
+ return any(line[:2] in conflict_codes for line in status)
121
+
122
+ @property
123
+ def template_ref(self) -> str:
124
+ return yaml.safe_load( # type: ignore[no-any-return]
125
+ (self.path / ".copier-answers.yml").read_text()
126
+ ).get("_commit")
127
+
128
+ def shell(self) -> None:
129
+ self.run([os.environ.get("SHELL", "/bin/bash")], check=False)
130
+
131
+ def open_pr(self, title: str, body: str) -> str:
132
+ result = self.run(
133
+ [
134
+ "gh",
135
+ "pr",
136
+ "create",
137
+ "--title",
138
+ title,
139
+ "--body",
140
+ body,
141
+ "--head",
142
+ self.branch,
143
+ ],
144
+ capture_output=True,
145
+ check=False,
146
+ )
147
+ if result.returncode == 0:
148
+ return result.stdout.strip()
149
+ view = self.run(
150
+ ["gh", "pr", "view", "--json", "url", "--jq", ".url"],
151
+ capture_output=True,
152
+ check=False,
153
+ )
154
+ if view.returncode == 0:
155
+ return view.stdout.strip()
156
+ raise RuntimeError(f"gh pr create failed: {result.stderr.strip()}")
@@ -0,0 +1,80 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import subprocess
6
+ from dataclasses import dataclass
7
+ from typing import TYPE_CHECKING
8
+
9
+ from .repo import RepoWorktree
10
+
11
+ if TYPE_CHECKING:
12
+ from .repo import RepoTarget
13
+
14
+
15
+ @dataclass
16
+ class UpdateAction:
17
+ repo: RepoTarget
18
+ branch: str
19
+ dry_run: bool = False
20
+
21
+ def __call__(self) -> str | None:
22
+ with RepoWorktree.clone(self.repo, branch=self.branch) as worktree:
23
+ return self._run(worktree)
24
+
25
+ def _run(self, repo: RepoWorktree) -> str | None:
26
+ """Run copier update in the repo worktree."""
27
+ copier_status = json.loads(
28
+ repo.run(
29
+ ["copier", "check-update", "--output-format", "json"],
30
+ capture_output=True,
31
+ ).stdout.strip()
32
+ )
33
+ start_ref = "v" + copier_status["current_version"]
34
+ end_ref = "v" + copier_status["latest_version"]
35
+ if not copier_status.get("update_available", False):
36
+ return None
37
+ repo.run(["copier", "update", "-l"])
38
+
39
+ status = repo.git_status()
40
+ if not status:
41
+ return None
42
+
43
+ if repo.has_conflicts(status):
44
+ print( # noqa: T201
45
+ "Conflicts detected."
46
+ " Resolve them and exit the shell to continue."
47
+ )
48
+ repo.shell()
49
+ status = repo.git_status()
50
+ if repo.has_conflicts(status):
51
+ raise RuntimeError("Conflicts remain, aborting")
52
+
53
+ try:
54
+ repo.run(["uv", "run", "poe", "lt"])
55
+ except subprocess.CalledProcessError:
56
+ print( # noqa: T201
57
+ "Lint/test failed. Fix errors and exit the shell to continue."
58
+ )
59
+ repo.shell()
60
+
61
+ title = "Apply template updates"
62
+ body = ""
63
+ if start_ref and end_ref and start_ref != end_ref:
64
+ ref_range = f"{start_ref}...{end_ref}"
65
+ body = os.linesep.join(
66
+ (
67
+ f"Applied updates from template: {ref_range}",
68
+ f"{repo.repo.url}/compare/{ref_range}",
69
+ )
70
+ )
71
+ repo.run(["git", "add", "-A"])
72
+ repo.run(["git", "commit", "-m", f"{title}\n\n{body}".strip()])
73
+
74
+ if self.dry_run:
75
+ return None
76
+
77
+ repo.run(
78
+ ["git", "push", "-u", "origin", repo.branch, "--force-with-lease"]
79
+ )
80
+ return repo.open_pr(title, body)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: copier-python
3
- Version: 0.8.0
3
+ Version: 0.9.0
4
4
  Summary: Copier template for Python projects with modern tooling
5
5
  Project-URL: Homepage, https://smkent.github.io/copier-python
6
6
  Project-URL: Repository, https://github.com/smkent/copier-python
@@ -22,6 +22,8 @@ Classifier: Topic :: Software Development
22
22
  Classifier: Topic :: Utilities
23
23
  Classifier: Typing :: Typed
24
24
  Requires-Python: >=3.10
25
+ Requires-Dist: pyyaml>=6
26
+ Requires-Dist: typer>=0.9
25
27
  Description-Content-Type: text/markdown
26
28
 
27
29
  # copier-python
@@ -0,0 +1,10 @@
1
+ copier_python/__init__.py,sha256=FxuK5Dek123GsD9rxpsXCP4FjxI7unpoPYe6Mto1Rjk,289
2
+ copier_python/__main__.py,sha256=DhAwkZmfhMa40m91IgDcVIDTjjaiT1hFHrZ7AqzJdVY,3560
3
+ copier_python/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ copier_python/repo.py,sha256=rFAFZ_roP5juWoxVzagJq8UQkhZijIftHeJC0EQlihk,4787
5
+ copier_python/update.py,sha256=PH_qP1zUHgNSAtUL0WnZSaweLIJ4rGXbyfayIv0NWUs,2433
6
+ copier_python-0.9.0.dist-info/METADATA,sha256=UQf-grCwkpg0EE8trREiIp7NvHXNC29tllz2EYQVpTU,4600
7
+ copier_python-0.9.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
8
+ copier_python-0.9.0.dist-info/entry_points.txt,sha256=HjthcI_ZBjVT8uC8vjlk8m4XWpmR9_b89rpAWjzFZvo,62
9
+ copier_python-0.9.0.dist-info/licenses/LICENSE,sha256=UHHRyYuAwaR1EBittWk49EmWR0c7MIIMBXt_L_auZtQ,1069
10
+ copier_python-0.9.0.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ copier-python = copier_python.__main__:main
@@ -1,6 +0,0 @@
1
- copier_python/__init__.py,sha256=FxuK5Dek123GsD9rxpsXCP4FjxI7unpoPYe6Mto1Rjk,289
2
- copier_python/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- copier_python-0.8.0.dist-info/METADATA,sha256=ngWMkwcjumtafs-AR1anHpk2m9mc2UUskqChuEJ4Wtg,4549
4
- copier_python-0.8.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
5
- copier_python-0.8.0.dist-info/licenses/LICENSE,sha256=UHHRyYuAwaR1EBittWk49EmWR0c7MIIMBXt_L_auZtQ,1069
6
- copier_python-0.8.0.dist-info/RECORD,,