Skip to content

Commit addd1ca

Browse files
committed
feat: improve lint-from-demo.py in attempt to get it to work smoothly
1 parent 0cf57c5 commit addd1ca

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

scripts/lint-from-demo.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from pathlib import Path
32
from typing import Annotated
43

@@ -11,6 +10,14 @@
1110
from util import in_new_demo
1211

1312

13+
# These still may need linted, but retrocookie shouldn't be used on them
14+
IGNORED_FILES: list[str] = [
15+
"{{cookiecutter.project_name}}/pyproject.toml",
16+
"{{cookiecutter.project_name}}/uv.lock",
17+
""
18+
]
19+
20+
1421
cli: typer.Typer = typer.Typer()
1522

1623

@@ -21,20 +28,21 @@ def lint_from_demo(
2128
no_cache: Annotated[bool, typer.Option("--no-cache", "-n")] = False
2229
) -> None:
2330
"""Runs precommit in a generated project and matches the template to the results."""
24-
try:
25-
with in_new_demo(
26-
demos_cache_folder=demos_cache_folder,
27-
add_rust_extension=add_rust_extension,
28-
no_cache=no_cache
29-
) as demo_path:
30-
pre_commit.main.main(["run", "--all-files", "--hook-stage=manual", "--show-diff-on-failure"])
31-
try:
32-
retrocookie(instance_path=demo_path, commits=["HEAD"])
33-
finally:
34-
git("checkout", "HEAD", "--", "{{cookiecutter.project_name}}/pyproject.toml")
35-
except Exception as error:
36-
typer.secho(f"error: {error}", fg="red")
37-
sys.exit(1)
31+
with in_new_demo(
32+
demos_cache_folder=demos_cache_folder,
33+
add_rust_extension=add_rust_extension,
34+
no_cache=no_cache
35+
) as demo_path:
36+
git("status", "--porcelain")
37+
git("branch", "-D", "temp/lint-from-demo", ignore_error=True)
38+
git("checkout", "-b", "temp/lint-from-demo", "develop")
39+
pre_commit.main.main(["run", "--all-files", "--hook-stage=manual", "--show-diff-on-failure"])
40+
41+
for path in IGNORED_FILES:
42+
git("checkout", "HEAD", "--", path)
43+
git("add", ".")
44+
git("commit", "-m", "meta: lint-from-demo", "--no-verify")
45+
retrocookie(instance_path=demo_path, commits=["HEAD"], branch="temp/lint-from-demo")
3846

3947

4048
if __name__ == '__main__':

scripts/util.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from typing import Any
1111
from typing import Callable
1212
from typing import Generator
13+
from typing import Literal
14+
from typing import Optional
15+
from typing import overload
1316

1417
import cruft
1518
import typer
@@ -35,12 +38,24 @@ def remove_readonly(func: Callable[[str], Any], path: str, _: Any) -> None:
3538
func(path)
3639

3740

38-
def run_command(command: str, *args: str) -> subprocess.CompletedProcess:
41+
@overload
42+
def run_command(command: str, *args: str, ignore_error: Literal[True]) -> Optional[subprocess.CompletedProcess]:
43+
...
44+
45+
46+
@overload
47+
def run_command(command: str, *args: str, ignore_error: Literal[False] = ...) -> subprocess.CompletedProcess:
48+
...
49+
50+
51+
def run_command(command: str, *args: str, ignore_error: bool = False) -> Optional[subprocess.CompletedProcess]:
3952
"""Runs the provided command in a subprocess."""
4053
try:
4154
process = subprocess.run([command, *args], check=True, capture_output=True, text=True)
4255
return process
4356
except subprocess.CalledProcessError as error:
57+
if ignore_error:
58+
return None
4459
print(error.stdout, end="")
4560
print(error.stderr, end="", file=sys.stderr)
4661
raise

0 commit comments

Comments
 (0)