Skip to content

Commit 0aebe5c

Browse files
committed
fix: add interop layer between older noxfile methods and newer env var logic for the time being
1 parent a424626 commit 0aebe5c

File tree

3 files changed

+76
-51
lines changed

3 files changed

+76
-51
lines changed

noxfile.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import os
88
import shutil
9+
from dataclasses import asdict
910
from dataclasses import dataclass
1011
from pathlib import Path
12+
from typing import Any
1113

1214
import nox
1315
import platformdirs
@@ -85,6 +87,22 @@ class RepoMetadata:
8587
develop_branch=os.getenv("COOKIECUTTER_ROBUST_PYTHON__DEVELOP_BRANCH")
8688
)
8789

90+
PYTHON_DEMO: RepoMetadata = RepoMetadata(
91+
app_name=os.getenv("ROBUST_PYTHON_DEMO__APP_NAME"),
92+
app_author=os.getenv("ROBUST_PYTHON_DEMO__APP_AUTHOR"),
93+
remote=os.getenv("ROBUST_PYTHON_DEMO__REMOTE"),
94+
main_branch=os.getenv("ROBUST_PYTHON_DEMO__MAIN_BRANCH"),
95+
develop_branch=os.getenv("ROBUST_PYTHON_DEMO__DEVELOP_BRANCH")
96+
)
97+
98+
MATURIN_DEMO: RepoMetadata = RepoMetadata(
99+
app_name=os.getenv("ROBUST_MATURIN_DEMO__APP_NAME"),
100+
app_author=os.getenv("ROBUST_MATURIN_DEMO__APP_AUTHOR"),
101+
remote=os.getenv("ROBUST_MATURIN_DEMO__REMOTE"),
102+
main_branch=os.getenv("ROBUST_MATURIN_DEMO__MAIN_BRANCH"),
103+
develop_branch=os.getenv("ROBUST_MATURIN_DEMO__DEVELOP_BRANCH")
104+
)
105+
88106

89107
@nox.session(python=DEFAULT_TEMPLATE_PYTHON_VERSION, name="generate-demo")
90108
def generate_demo(session: Session) -> None:
@@ -164,17 +182,25 @@ def test(session: Session) -> None:
164182
session.run("pytest", "tests")
165183

166184

167-
@nox.parametrize(arg_names="add_rust_extension", arg_values_list=[False, True], ids=["no-rust", "rust"])
185+
@nox.parametrize(
186+
arg_names="demo",
187+
arg_values_list=[PYTHON_DEMO, MATURIN_DEMO],
188+
ids=["robust-python-demo", "robust-maturin-demo"]
189+
)
168190
@nox.session(python=DEFAULT_TEMPLATE_PYTHON_VERSION, name="update-demo")
169-
def update_demo(session: Session, add_rust_extension: bool) -> None:
191+
def update_demo(session: Session, demo: RepoMetadata) -> None:
170192
session.log("Installing script dependencies for updating generated project demos...")
171193
session.install("cookiecutter", "cruft", "platformdirs", "loguru", "python-dotenv", "typer")
172194

173195
session.log("Updating generated project demos...")
174196
args: list[str] = [*UPDATE_DEMO_OPTIONS]
175-
if add_rust_extension:
197+
if "maturin" in demo.app_name:
176198
args.append("--add-rust-extension")
177-
session.run("python", UPDATE_DEMO_SCRIPT, *args)
199+
200+
demo_env: dict[str, Any] = {
201+
key.replace(demo.app_name.upper(), "ROBUST_DEMO"): value for key, value in asdict(demo).items()
202+
}
203+
session.run("python", UPDATE_DEMO_SCRIPT, *args, env=demo_env)
178204

179205

180206
@nox.session(python=False, name="release-template")

scripts/update-demo.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,49 +43,44 @@ def update_demo(
4343
max_python_version: Annotated[str, typer.Option("--max-python-version")] = "3.14"
4444
) -> None:
4545
"""Runs precommit in a generated project and matches the template to the results."""
46-
try:
47-
demo_name: str = get_demo_name(add_rust_extension=add_rust_extension)
48-
demo_path: Path = demos_cache_folder / demo_name
49-
50-
current_branch: str = get_current_branch()
51-
template_commit: str = get_current_commit()
52-
53-
_validate_template_main_not_checked_out(branch=current_branch)
54-
require_clean_and_up_to_date_repo(demo_path=demo_path)
55-
_checkout_demo_develop_or_existing_branch(demo_path=demo_path, branch=current_branch)
56-
last_update_commit: str = get_last_cruft_update_commit(demo_path=demo_path)
57-
58-
if not is_ancestor(last_update_commit, template_commit):
59-
raise ValueError(
60-
f"The last update commit '{last_update_commit}' is not an ancestor of the current commit "
61-
f"'{template_commit}'."
62-
)
63-
64-
typer.secho(f"Updating demo project at {demo_path=}.", fg="yellow")
65-
with work_in(demo_path):
66-
if current_branch != "develop":
67-
git("checkout", "-b", current_branch)
68-
69-
uv("python", "pin", min_python_version)
70-
uv("python", "install", min_python_version)
71-
cruft.update(
72-
project_dir=demo_path,
73-
template_path=REPO_FOLDER,
74-
extra_context={
75-
"project_name": demo_name,
76-
"add_rust_extension": add_rust_extension,
77-
"min_python_version": min_python_version,
78-
"max_python_version": max_python_version
79-
},
80-
)
81-
uv("lock")
82-
git("add", ".")
83-
git("commit", "-m", f"chore: {last_update_commit} -> {template_commit}", "--no-verify")
84-
git("push", "-u", "origin", current_branch)
85-
86-
except Exception as error:
87-
typer.secho(f"error: {error}", fg="red")
88-
sys.exit(1)
46+
demo_name: str = get_demo_name(add_rust_extension=add_rust_extension)
47+
demo_path: Path = demos_cache_folder / demo_name
48+
49+
current_branch: str = get_current_branch()
50+
template_commit: str = get_current_commit()
51+
52+
_validate_template_main_not_checked_out(branch=current_branch)
53+
require_clean_and_up_to_date_repo(demo_path=demo_path)
54+
_checkout_demo_develop_or_existing_branch(demo_path=demo_path, branch=current_branch)
55+
last_update_commit: str = get_last_cruft_update_commit(demo_path=demo_path)
56+
57+
if not is_ancestor(last_update_commit, template_commit):
58+
raise ValueError(
59+
f"The last update commit '{last_update_commit}' is not an ancestor of the current commit "
60+
f"'{template_commit}'."
61+
)
62+
63+
typer.secho(f"Updating demo project at {demo_path=}.", fg="yellow")
64+
with work_in(demo_path):
65+
if current_branch != "develop":
66+
git("checkout", "-b", current_branch)
67+
68+
uv("python", "pin", min_python_version)
69+
uv("python", "install", min_python_version)
70+
cruft.update(
71+
project_dir=demo_path,
72+
template_path=REPO_FOLDER,
73+
extra_context={
74+
"project_name": demo_name,
75+
"add_rust_extension": add_rust_extension,
76+
"min_python_version": min_python_version,
77+
"max_python_version": max_python_version
78+
},
79+
)
80+
uv("lock")
81+
git("add", ".")
82+
git("commit", "-m", f"chore: {last_update_commit} -> {template_commit}", "--no-verify")
83+
git("push", "-u", "origin", current_branch)
8984

9085

9186
def _checkout_demo_develop_or_existing_branch(demo_path: Path, branch: str) -> None:

scripts/util.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,14 @@ def run_command(command: str, *args: str, ignore_error: bool = False) -> Optiona
129129

130130
def require_clean_and_up_to_date_repo(demo_path: Path) -> None:
131131
"""Checks if the repo is clean and up to date with any important branches."""
132-
with work_in(demo_path):
133-
git("fetch")
134-
git("status", "--porcelain")
135-
validate_is_synced_ancestor(ancestor=DEMO.main_branch, descendent=DEMO.develop_branch)
132+
try:
133+
with work_in(demo_path):
134+
git("fetch")
135+
git("status", "--porcelain")
136+
validate_is_synced_ancestor(ancestor=DEMO.main_branch, descendent=DEMO.develop_branch)
137+
except Exception as e:
138+
typer.secho(f"Failed initial repo state check.")
139+
raise e
136140

137141

138142
def validate_is_synced_ancestor(ancestor: str, descendent: str) -> None:

0 commit comments

Comments
 (0)