mirror of
https://github.com/davidism/modify-repos.git
synced 2025-07-08 03:45:41 +00:00
refactor for extension
add git, text, and template helpers
This commit is contained in:
parent
63d983afcf
commit
a35e283b79
16 changed files with 422 additions and 243 deletions
|
@ -9,11 +9,10 @@ license-files = ["LICENSE.txt"]
|
||||||
requires-python = "~=3.13"
|
requires-python = "~=3.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"click>=8.1.8",
|
"click>=8.1.8",
|
||||||
|
"jinja2>=3.1.5",
|
||||||
|
"platformdirs>=4.3.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
|
||||||
modify-repos = "modify_repos.cli:entry_point"
|
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["pdm-backend>=2.4.3"]
|
requires = ["pdm-backend>=2.4.3"]
|
||||||
build-backend = "pdm.backend"
|
build-backend = "pdm.backend"
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
from .repo.base import Repo
|
||||||
|
from .repo.github import GitHubRepo
|
||||||
|
from .script.base import Script
|
||||||
|
from .script.github import GitHubScript
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"GitHubRepo",
|
||||||
|
"GitHubScript",
|
||||||
|
"Repo",
|
||||||
|
"Script",
|
||||||
|
]
|
|
@ -1,3 +0,0 @@
|
||||||
from modify_repos.cli import cli
|
|
||||||
|
|
||||||
cli()
|
|
|
@ -1,22 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import click
|
|
||||||
|
|
||||||
from modify_repos.models import Script
|
|
||||||
|
|
||||||
|
|
||||||
@click.command
|
|
||||||
@click.option("-s", "--script", "script_name", required=True)
|
|
||||||
@click.option("--push/--no-push")
|
|
||||||
def cli(script_name: str, push: bool) -> None:
|
|
||||||
script_cls = Script.load_cls(script_name)
|
|
||||||
script = script_cls(push)
|
|
||||||
script.run()
|
|
||||||
|
|
||||||
|
|
||||||
def entry_point() -> None:
|
|
||||||
sys.path.insert(0, os.getcwd())
|
|
||||||
cli()
|
|
|
@ -1,32 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import collections.abc as c
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
import typing as t
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import click
|
|
||||||
|
|
||||||
type CmdArg = str | Path
|
|
||||||
|
|
||||||
|
|
||||||
def run_cmd(*args: CmdArg, **kwargs: t.Any) -> subprocess.CompletedProcess[str]:
|
|
||||||
echo_cmd(args)
|
|
||||||
result: subprocess.CompletedProcess[str] = subprocess.run(
|
|
||||||
args,
|
|
||||||
text=True,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
**kwargs,
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode:
|
|
||||||
click.echo(result.stdout)
|
|
||||||
click.secho(f"exited with code {result.returncode}", fg="red")
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def echo_cmd(args: c.Iterable[CmdArg]) -> None:
|
|
||||||
click.echo(f"$ {shlex.join(str(v) for v in args)}")
|
|
|
@ -1,170 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import dataclasses
|
|
||||||
import typing as t
|
|
||||||
from contextlib import chdir
|
|
||||||
from functools import cached_property
|
|
||||||
from inspect import isclass
|
|
||||||
from pathlib import Path
|
|
||||||
from pkgutil import resolve_name
|
|
||||||
|
|
||||||
import click
|
|
||||||
|
|
||||||
from modify_repos.cmd import echo_cmd
|
|
||||||
from modify_repos.cmd import run_cmd
|
|
||||||
from modify_repos.wrap import wrap
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
|
||||||
class Repo:
|
|
||||||
org: str
|
|
||||||
name: str
|
|
||||||
dir: Path
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def full_name(self) -> str:
|
|
||||||
return f"{self.org}/{self.name}"
|
|
||||||
|
|
||||||
def clone(self, script: Script) -> None:
|
|
||||||
self.dir.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
if self.dir.exists():
|
|
||||||
with chdir(self.dir):
|
|
||||||
run_cmd("git", "switch", "-f", script.target)
|
|
||||||
run_cmd("git", "pull", "--prune")
|
|
||||||
else:
|
|
||||||
run_cmd(
|
|
||||||
"gh",
|
|
||||||
"repo",
|
|
||||||
"clone",
|
|
||||||
f"{self.org}/{self.name}",
|
|
||||||
self.dir,
|
|
||||||
"--",
|
|
||||||
"-b",
|
|
||||||
script.target,
|
|
||||||
)
|
|
||||||
|
|
||||||
def modify(self, script: Script) -> None:
|
|
||||||
run_cmd(
|
|
||||||
"git",
|
|
||||||
"switch",
|
|
||||||
"--no-track",
|
|
||||||
"-C",
|
|
||||||
script.branch,
|
|
||||||
f"origin/{script.target}",
|
|
||||||
)
|
|
||||||
script.modify(self)
|
|
||||||
|
|
||||||
if run_cmd("git", "status", "--porcelain").stdout:
|
|
||||||
run_cmd("git", "add", "--all")
|
|
||||||
run_cmd("git", "commit", "--message", f"{script.title}\n\n{script.body}")
|
|
||||||
|
|
||||||
def push(self, script: Script) -> None:
|
|
||||||
if not script.push:
|
|
||||||
return
|
|
||||||
|
|
||||||
echo_cmd(
|
|
||||||
[
|
|
||||||
"git",
|
|
||||||
"push",
|
|
||||||
"--set-upstream",
|
|
||||||
"origin",
|
|
||||||
script.branch,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
echo_cmd(
|
|
||||||
[
|
|
||||||
"gh",
|
|
||||||
"pr",
|
|
||||||
"create",
|
|
||||||
"--base",
|
|
||||||
script.target,
|
|
||||||
"--title",
|
|
||||||
script.title,
|
|
||||||
"--body",
|
|
||||||
script.body,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Script:
|
|
||||||
orgs: list[str]
|
|
||||||
target: str = "main"
|
|
||||||
branch: str
|
|
||||||
title: str
|
|
||||||
body: str
|
|
||||||
|
|
||||||
def __init_subclass__(cls, **kwargs: t.Any) -> None:
|
|
||||||
super().__init_subclass__(**kwargs)
|
|
||||||
cls.body = wrap(cls.body, width=72)
|
|
||||||
|
|
||||||
def __init__(self, push: bool) -> None:
|
|
||||||
self.clones_dir: Path = Path("clones")
|
|
||||||
self.push = push
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def load_cls(cls, name: str) -> type[Script]:
|
|
||||||
obj = resolve_name(name)
|
|
||||||
|
|
||||||
if isclass(obj) and obj is not Script and issubclass(obj, Script):
|
|
||||||
return obj
|
|
||||||
|
|
||||||
for val in vars(obj).values():
|
|
||||||
if isclass(val) and val is not Script and issubclass(val, Script):
|
|
||||||
return val
|
|
||||||
|
|
||||||
raise RuntimeError(f"Could not load script {name!r}.")
|
|
||||||
|
|
||||||
def list_repos(self) -> list[Repo]:
|
|
||||||
return [
|
|
||||||
Repo(org, name, self.clones_dir / org / name)
|
|
||||||
for org in self.orgs
|
|
||||||
for name in run_cmd(
|
|
||||||
"gh",
|
|
||||||
"repo",
|
|
||||||
"list",
|
|
||||||
"--no-archived",
|
|
||||||
"--json",
|
|
||||||
"name",
|
|
||||||
"--jq",
|
|
||||||
".[] | .name",
|
|
||||||
org,
|
|
||||||
).stdout.splitlines()
|
|
||||||
]
|
|
||||||
|
|
||||||
def run(self) -> None:
|
|
||||||
self.clones_dir.mkdir(exist_ok=True)
|
|
||||||
ignore = self.clones_dir / ".gitignore"
|
|
||||||
|
|
||||||
if not ignore.exists():
|
|
||||||
ignore.write_text("*\n")
|
|
||||||
|
|
||||||
for repo in self.list_repos():
|
|
||||||
click.secho(repo.full_name, fg="green")
|
|
||||||
|
|
||||||
if self.select_for_clone(repo):
|
|
||||||
repo.clone(self)
|
|
||||||
|
|
||||||
with chdir(repo.dir):
|
|
||||||
if self.select_for_modify(repo):
|
|
||||||
repo.modify(self)
|
|
||||||
|
|
||||||
if self.push:
|
|
||||||
repo.push(self)
|
|
||||||
else:
|
|
||||||
click.secho("skipping push", fg="yellow")
|
|
||||||
else:
|
|
||||||
click.secho("skipping modify", fg="yellow")
|
|
||||||
else:
|
|
||||||
click.secho("skipping clone", fg="yellow")
|
|
||||||
|
|
||||||
click.echo()
|
|
||||||
|
|
||||||
def select_for_clone(self, repo: Repo) -> bool:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def select_for_modify(self, repo: Repo) -> bool:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def modify(self, repo: Repo) -> None:
|
|
||||||
raise NotImplementedError
|
|
0
src/modify_repos/repo/__init__.py
Normal file
0
src/modify_repos/repo/__init__.py
Normal file
68
src/modify_repos/repo/base.py
Normal file
68
src/modify_repos/repo/base.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing as t
|
||||||
|
from contextlib import chdir
|
||||||
|
from functools import cached_property
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
if t.TYPE_CHECKING:
|
||||||
|
from ..script.base import Script
|
||||||
|
|
||||||
|
|
||||||
|
class Repo:
|
||||||
|
remote_id: str
|
||||||
|
|
||||||
|
def __init__(self, script: Script[t.Any], remote_id: str) -> None:
|
||||||
|
self.script = script
|
||||||
|
self.remote_id = remote_id
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def local_dir(self) -> Path:
|
||||||
|
return self.script.clones_dir / self.remote_id
|
||||||
|
|
||||||
|
def clone(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def clone_if_needed(self) -> None:
|
||||||
|
if not self.local_dir.exists():
|
||||||
|
self.clone()
|
||||||
|
|
||||||
|
def reset_target(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def reset_branch(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def needs_commit(self) -> bool:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def commit(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def commit_if_needed(self) -> None:
|
||||||
|
if self.needs_commit():
|
||||||
|
self.commit()
|
||||||
|
|
||||||
|
def needs_submit(self) -> bool:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def submit(self) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def submit_if_needed(self) -> None:
|
||||||
|
if self.needs_submit():
|
||||||
|
self.submit()
|
||||||
|
|
||||||
|
def run(self) -> None:
|
||||||
|
self.clone_if_needed()
|
||||||
|
|
||||||
|
with chdir(self.local_dir):
|
||||||
|
self.reset_target()
|
||||||
|
|
||||||
|
if not self.script.select_for_modify(self):
|
||||||
|
return
|
||||||
|
|
||||||
|
self.reset_branch()
|
||||||
|
self.script.modify(self)
|
||||||
|
self.commit_if_needed()
|
||||||
|
self.submit_if_needed()
|
68
src/modify_repos/repo/git.py
Normal file
68
src/modify_repos/repo/git.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from shutil import which
|
||||||
|
from subprocess import CompletedProcess
|
||||||
|
|
||||||
|
from ..utils import run_cmd
|
||||||
|
from .base import Repo
|
||||||
|
|
||||||
|
|
||||||
|
class GitRepo(Repo):
|
||||||
|
_git_exe = which("git")
|
||||||
|
add_untracked: bool = False
|
||||||
|
|
||||||
|
def git_cmd(self, *args: str | Path) -> CompletedProcess[str]:
|
||||||
|
if self._git_exe is None:
|
||||||
|
raise RuntimeError("Git is not installed.")
|
||||||
|
|
||||||
|
return run_cmd(self._git_exe, *args)
|
||||||
|
|
||||||
|
def reset_target(self) -> None:
|
||||||
|
self.git_cmd("switch", "-f", self.script.target)
|
||||||
|
self.git_cmd("reset", "--hard", self.script.full_target)
|
||||||
|
self.git_cmd("pull", "--prune")
|
||||||
|
|
||||||
|
def reset_branch(self) -> None:
|
||||||
|
self.git_cmd("switch", "-C", self.script.branch, self.script.target)
|
||||||
|
|
||||||
|
def needs_commit(self) -> bool:
|
||||||
|
args = ["status", "--porcelain"]
|
||||||
|
|
||||||
|
if not self.add_untracked:
|
||||||
|
args.append("--untracked-files=no")
|
||||||
|
|
||||||
|
return bool(self.git_cmd(*args).stdout)
|
||||||
|
|
||||||
|
def commit(self) -> None:
|
||||||
|
if self.add_untracked:
|
||||||
|
self.add_files(all=True)
|
||||||
|
else:
|
||||||
|
self.add_files(update=True)
|
||||||
|
|
||||||
|
self.git_cmd("commit", "--message", self.script.commit_message)
|
||||||
|
|
||||||
|
def needs_submit(self) -> bool:
|
||||||
|
return bool(self.git_cmd("cherry", self.script.full_target).stdout)
|
||||||
|
|
||||||
|
def submit(self) -> None:
|
||||||
|
self.git_cmd("switch", self.script.target)
|
||||||
|
self.git_cmd("merge", "--ff-only", self.script.branch)
|
||||||
|
self.git_cmd("push", "--dry-run")
|
||||||
|
|
||||||
|
def add_files(
|
||||||
|
self, *items: str | Path, update: bool = False, all: bool = False
|
||||||
|
) -> None:
|
||||||
|
if all:
|
||||||
|
self.git_cmd("add", "--all")
|
||||||
|
|
||||||
|
if update:
|
||||||
|
self.git_cmd("add", "--update")
|
||||||
|
|
||||||
|
self.git_cmd("add", *items)
|
||||||
|
|
||||||
|
def rm_files(self, *items: str | Path) -> None:
|
||||||
|
to_remove = [item for item in items if Path(item).exists()]
|
||||||
|
|
||||||
|
if to_remove:
|
||||||
|
self.git_cmd("rm", *to_remove)
|
65
src/modify_repos/repo/github.py
Normal file
65
src/modify_repos/repo/github.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing as t
|
||||||
|
from functools import cached_property
|
||||||
|
from pathlib import Path
|
||||||
|
from shutil import which
|
||||||
|
from subprocess import CompletedProcess
|
||||||
|
|
||||||
|
from ..repo.git import GitRepo
|
||||||
|
from ..utils import run_cmd
|
||||||
|
|
||||||
|
if t.TYPE_CHECKING:
|
||||||
|
from ..script.base import Script
|
||||||
|
|
||||||
|
|
||||||
|
class GitHubRepo(GitRepo):
|
||||||
|
_gh_exe: str | None = which("gh")
|
||||||
|
direct_submit: bool = False
|
||||||
|
|
||||||
|
def __init__(self, script: Script[t.Any], org: str, name: str) -> None:
|
||||||
|
self.org = org
|
||||||
|
self.name = name
|
||||||
|
super().__init__(script=script, remote_id=self.full_name)
|
||||||
|
|
||||||
|
def gh_cmd(self, *args: str | Path) -> CompletedProcess[str]:
|
||||||
|
if self._gh_exe is None:
|
||||||
|
raise RuntimeError("GitHub CLI is not installed.")
|
||||||
|
|
||||||
|
return run_cmd(self._gh_exe, *args)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def full_name(self) -> str:
|
||||||
|
return f"{self.org}/{self.name}"
|
||||||
|
|
||||||
|
def clone(self) -> None:
|
||||||
|
self.local_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
self.gh_cmd(
|
||||||
|
"repo",
|
||||||
|
"clone",
|
||||||
|
self.full_name,
|
||||||
|
self.local_dir,
|
||||||
|
"--",
|
||||||
|
"-b",
|
||||||
|
self.script.target,
|
||||||
|
)
|
||||||
|
|
||||||
|
def submit(self) -> None:
|
||||||
|
if self.direct_submit:
|
||||||
|
super().submit()
|
||||||
|
return
|
||||||
|
|
||||||
|
self.git_cmd(
|
||||||
|
"push", "--dry-run", "--set-upstream", "origin", self.script.branch
|
||||||
|
)
|
||||||
|
self.gh_cmd(
|
||||||
|
"pr",
|
||||||
|
"create",
|
||||||
|
"--dry-run",
|
||||||
|
"--base",
|
||||||
|
self.script.target,
|
||||||
|
"--title",
|
||||||
|
self.script.title,
|
||||||
|
"--body",
|
||||||
|
self.script.body,
|
||||||
|
)
|
0
src/modify_repos/script/__init__.py
Normal file
0
src/modify_repos/script/__init__.py
Normal file
73
src/modify_repos/script/base.py
Normal file
73
src/modify_repos/script/base.py
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
import inspect
|
||||||
|
import typing as t
|
||||||
|
from functools import cached_property
|
||||||
|
from os import PathLike
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import jinja2
|
||||||
|
import platformdirs
|
||||||
|
|
||||||
|
from ..repo.base import Repo
|
||||||
|
from ..utils import read_text
|
||||||
|
from ..utils import wrap_text
|
||||||
|
|
||||||
|
|
||||||
|
class Script[RepoType: Repo]:
|
||||||
|
target: str = "main"
|
||||||
|
branch: str
|
||||||
|
title: str
|
||||||
|
body: str
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
source_file = inspect.getsourcefile(self.__class__)
|
||||||
|
|
||||||
|
if source_file is None:
|
||||||
|
raise RuntimeError("Could not determine script root.")
|
||||||
|
|
||||||
|
self.root_dir: Path = Path(source_file).parent
|
||||||
|
self.jinja_env: jinja2.Environment = jinja2.Environment(
|
||||||
|
loader=jinja2.FileSystemLoader(self.root_dir),
|
||||||
|
trim_blocks=True,
|
||||||
|
lstrip_blocks=True,
|
||||||
|
keep_trailing_newline=True,
|
||||||
|
)
|
||||||
|
self.clones_dir: Path = platformdirs.user_cache_path("modify-repos") / "clones"
|
||||||
|
self.clones_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
if not (ignore := self.clones_dir / ".gitignore").exists():
|
||||||
|
ignore.write_text("*\n")
|
||||||
|
|
||||||
|
self.body = wrap_text(self.body, width=72)
|
||||||
|
|
||||||
|
def render_template(self, name: str, /, **kwargs: t.Any) -> str:
|
||||||
|
return self.jinja_env.get_template(name).render(**kwargs)
|
||||||
|
|
||||||
|
def list_all_repos(self) -> list[RepoType]:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def list_repos(self) -> list[RepoType]:
|
||||||
|
return [r for r in self.list_all_repos() if self.select_for_clone(r)]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def full_target(self) -> str:
|
||||||
|
return f"origin/{self.target}"
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def commit_message(self) -> str:
|
||||||
|
return f"{self.title}\n\n{self.body}"
|
||||||
|
|
||||||
|
def select_for_clone(self, repo: Repo) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def select_for_modify(self, repo: Repo) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def modify(self, repo: Repo) -> None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def run(self) -> None:
|
||||||
|
for repo in self.list_repos():
|
||||||
|
repo.run()
|
||||||
|
|
||||||
|
def read_text(self, path: str | PathLike[str], strip: bool = True) -> str:
|
||||||
|
return read_text(self.root_dir / path, strip=strip)
|
30
src/modify_repos/script/github.py
Normal file
30
src/modify_repos/script/github.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
from shutil import which
|
||||||
|
|
||||||
|
from ..repo.github import GitHubRepo
|
||||||
|
from ..utils import run_cmd
|
||||||
|
from .base import Script
|
||||||
|
|
||||||
|
|
||||||
|
class GitHubScript(Script[GitHubRepo]):
|
||||||
|
def __init__(self, orgs: list[str] | None = None) -> None:
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
if orgs is not None:
|
||||||
|
self.orgs = orgs
|
||||||
|
|
||||||
|
def list_all_repos(self) -> list[GitHubRepo]:
|
||||||
|
return [
|
||||||
|
GitHubRepo(self, org, name)
|
||||||
|
for org in self.orgs
|
||||||
|
for name in run_cmd(
|
||||||
|
which("gh"), # type: ignore[arg-type]
|
||||||
|
"repo",
|
||||||
|
"list",
|
||||||
|
"--no-archived",
|
||||||
|
"--json",
|
||||||
|
"name",
|
||||||
|
"--jq",
|
||||||
|
".[] | .name",
|
||||||
|
org,
|
||||||
|
).stdout.splitlines()
|
||||||
|
]
|
58
src/modify_repos/utils.py
Normal file
58
src/modify_repos/utils.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
|
import textwrap
|
||||||
|
import typing as t
|
||||||
|
from inspect import cleandoc
|
||||||
|
from os import PathLike
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
|
||||||
|
def run_cmd(
|
||||||
|
*args: str | PathLike[str], **kwargs: t.Any
|
||||||
|
) -> subprocess.CompletedProcess[str]:
|
||||||
|
echo_cmd(*args)
|
||||||
|
result: subprocess.CompletedProcess[str] = subprocess.run(
|
||||||
|
args,
|
||||||
|
text=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.returncode:
|
||||||
|
click.echo(result.stdout)
|
||||||
|
click.secho(f"exited with code {result.returncode}", fg="red")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def echo_cmd(*args: str | PathLike[str]) -> None:
|
||||||
|
click.echo(f"$ {shlex.join(str(v) for v in args)}")
|
||||||
|
|
||||||
|
|
||||||
|
def wrap_text(text: str, width: int = 80) -> str:
|
||||||
|
"""Wrap a multi-line, multi-paragraph string."""
|
||||||
|
return "\n\n".join(
|
||||||
|
textwrap.fill(p, width=width, tabsize=4, break_long_words=False)
|
||||||
|
for p in cleandoc(text).split("\n\n")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def read_text(path: str | PathLike[str], strip: bool = True) -> str:
|
||||||
|
text = Path(path).read_text("utf8")
|
||||||
|
|
||||||
|
if strip:
|
||||||
|
text = text.strip()
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def write_text(path: str | PathLike[str], text: str, end_nl: bool = True) -> None:
|
||||||
|
if end_nl:
|
||||||
|
text = f"{text.rstrip('\n')}\n"
|
||||||
|
|
||||||
|
Path(path).write_text(text, "utf8")
|
|
@ -1,12 +0,0 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import textwrap
|
|
||||||
from inspect import cleandoc
|
|
||||||
|
|
||||||
|
|
||||||
def wrap(text: str, width: int = 80) -> str:
|
|
||||||
"""Wrap a multi-line, multi-paragraph string."""
|
|
||||||
return "\n\n".join(
|
|
||||||
textwrap.fill(p, width=width, tabsize=4, break_long_words=False)
|
|
||||||
for p in cleandoc(text).split("\n\n")
|
|
||||||
)
|
|
48
uv.lock
generated
48
uv.lock
generated
|
@ -85,12 +85,54 @@ wheels = [
|
||||||
{ url = "https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 },
|
{ url = "https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374", size = 5892 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jinja2"
|
||||||
|
version = "3.1.5"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "markupsafe" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/af/92/b3130cbbf5591acf9ade8708c365f3238046ac7cb8ccba6e81abccb0ccff/jinja2-3.1.5.tar.gz", hash = "sha256:8fefff8dc3034e27bb80d67c671eb8a9bc424c0ef4c0826edbff304cceff43bb", size = 244674 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/bd/0f/2ba5fbcd631e3e88689309dbe978c5769e883e4b84ebfe7da30b43275c5a/jinja2-3.1.5-py3-none-any.whl", hash = "sha256:aba0f4dc9ed8013c424088f68a5c226f7d6097ed89b246d7749c2ec4175c6adb", size = 134596 },
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "markupsafe"
|
||||||
|
version = "3.0.2"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/b2/97/5d42485e71dfc078108a86d6de8fa46db44a1a9295e89c5d6d4a06e23a62/markupsafe-3.0.2.tar.gz", hash = "sha256:ee55d3edf80167e48ea11a923c7386f4669df67d7994554387f84e7d8b0a2bf0", size = 20537 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/83/0e/67eb10a7ecc77a0c2bbe2b0235765b98d164d81600746914bebada795e97/MarkupSafe-3.0.2-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:ba9527cdd4c926ed0760bc301f6728ef34d841f405abf9d4f959c478421e4efd", size = 14274 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/2b/6d/9409f3684d3335375d04e5f05744dfe7e9f120062c9857df4ab490a1031a/MarkupSafe-3.0.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f8b3d067f2e40fe93e1ccdd6b2e1d16c43140e76f02fb1319a05cf2b79d99430", size = 12352 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/d2/f5/6eadfcd3885ea85fe2a7c128315cc1bb7241e1987443d78c8fe712d03091/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:569511d3b58c8791ab4c2e1285575265991e6d8f8700c7be0e88f86cb0672094", size = 24122 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/0c/91/96cf928db8236f1bfab6ce15ad070dfdd02ed88261c2afafd4b43575e9e9/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:15ab75ef81add55874e7ab7055e9c397312385bd9ced94920f2802310c930396", size = 23085 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/c2/cf/c9d56af24d56ea04daae7ac0940232d31d5a8354f2b457c6d856b2057d69/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f3818cb119498c0678015754eba762e0d61e5b52d34c8b13d770f0719f7b1d79", size = 22978 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/2a/9f/8619835cd6a711d6272d62abb78c033bda638fdc54c4e7f4272cf1c0962b/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:cdb82a876c47801bb54a690c5ae105a46b392ac6099881cdfb9f6e95e4014c6a", size = 24208 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/f9/bf/176950a1792b2cd2102b8ffeb5133e1ed984547b75db47c25a67d3359f77/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:cabc348d87e913db6ab4aa100f01b08f481097838bdddf7c7a84b7575b7309ca", size = 23357 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/ce/4f/9a02c1d335caabe5c4efb90e1b6e8ee944aa245c1aaaab8e8a618987d816/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:444dcda765c8a838eaae23112db52f1efaf750daddb2d9ca300bcae1039adc5c", size = 23344 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/ee/55/c271b57db36f748f0e04a759ace9f8f759ccf22b4960c270c78a394f58be/MarkupSafe-3.0.2-cp313-cp313-win32.whl", hash = "sha256:bcf3e58998965654fdaff38e58584d8937aa3096ab5354d493c77d1fdd66d7a1", size = 15101 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/29/88/07df22d2dd4df40aba9f3e402e6dc1b8ee86297dddbad4872bd5e7b0094f/MarkupSafe-3.0.2-cp313-cp313-win_amd64.whl", hash = "sha256:e6a2a455bd412959b57a172ce6328d2dd1f01cb2135efda2e4576e8a23fa3b0f", size = 15603 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/62/6a/8b89d24db2d32d433dffcd6a8779159da109842434f1dd2f6e71f32f738c/MarkupSafe-3.0.2-cp313-cp313t-macosx_10_13_universal2.whl", hash = "sha256:b5a6b3ada725cea8a5e634536b1b01c30bcdcd7f9c6fff4151548d5bf6b3a36c", size = 14510 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/7a/06/a10f955f70a2e5a9bf78d11a161029d278eeacbd35ef806c3fd17b13060d/MarkupSafe-3.0.2-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:a904af0a6162c73e3edcb969eeeb53a63ceeb5d8cf642fade7d39e7963a22ddb", size = 12486 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/34/cf/65d4a571869a1a9078198ca28f39fba5fbb910f952f9dbc5220afff9f5e6/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4aa4e5faecf353ed117801a068ebab7b7e09ffb6e1d5e412dc852e0da018126c", size = 25480 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/0c/e3/90e9651924c430b885468b56b3d597cabf6d72be4b24a0acd1fa0e12af67/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c0ef13eaeee5b615fb07c9a7dadb38eac06a0608b41570d8ade51c56539e509d", size = 23914 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/66/8c/6c7cf61f95d63bb866db39085150df1f2a5bd3335298f14a66b48e92659c/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d16a81a06776313e817c951135cf7340a3e91e8c1ff2fac444cfd75fffa04afe", size = 23796 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/bb/35/cbe9238ec3f47ac9a7c8b3df7a808e7cb50fe149dc7039f5f454b3fba218/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:6381026f158fdb7c72a168278597a5e3a5222e83ea18f543112b2662a9b699c5", size = 25473 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/e6/32/7621a4382488aa283cc05e8984a9c219abad3bca087be9ec77e89939ded9/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:3d79d162e7be8f996986c064d1c7c817f6df3a77fe3d6859f6f9e7be4b8c213a", size = 24114 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/0d/80/0985960e4b89922cb5a0bac0ed39c5b96cbc1a536a99f30e8c220a996ed9/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:131a3c7689c85f5ad20f9f6fb1b866f402c445b220c19fe4308c0b147ccd2ad9", size = 24098 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/82/78/fedb03c7d5380df2427038ec8d973587e90561b2d90cd472ce9254cf348b/MarkupSafe-3.0.2-cp313-cp313t-win32.whl", hash = "sha256:ba8062ed2cf21c07a9e295d5b8a2a5ce678b913b45fdf68c32d95d6c1291e0b6", size = 15208 },
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/4f/65/6079a46068dfceaeabb5dcad6d674f5f5c61a6fa5673746f42a9f4c233b3/MarkupSafe-3.0.2-cp313-cp313t-win_amd64.whl", hash = "sha256:e444a31f8db13eb18ada366ab3cf45fd4b31e4db1236a4448f68778c1d1a5a2f", size = 15739 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "modify-repos"
|
name = "modify-repos"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = { editable = "." }
|
source = { editable = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "click" },
|
{ name = "click" },
|
||||||
|
{ name = "jinja2" },
|
||||||
|
{ name = "platformdirs" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dev-dependencies]
|
[package.dev-dependencies]
|
||||||
|
@ -112,7 +154,11 @@ typing = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [{ name = "click", specifier = ">=8.1.8" }]
|
requires-dist = [
|
||||||
|
{ name = "click", specifier = ">=8.1.8" },
|
||||||
|
{ name = "jinja2", specifier = ">=3.1.5" },
|
||||||
|
{ name = "platformdirs", specifier = ">=4.3.6" },
|
||||||
|
]
|
||||||
|
|
||||||
[package.metadata.requires-dev]
|
[package.metadata.requires-dev]
|
||||||
dev = [
|
dev = [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue