Skip to content

Commit a41996d

Browse files
authored
Refactor apply_configuration.py for repo_root usage
1 parent 3aa510e commit a41996d

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

apply_configuration.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import subprocess
1616
from pathlib import Path
1717

18+
REPO_ROOT = Path(__file__).resolve().parent
19+
1820

1921
def _multi_replace(substitutions: dict[str, str], text: str) -> str:
2022
# Use re to replace everything in one pass, avoiding issues with the user's
@@ -26,16 +28,23 @@ def _multi_replace(substitutions: dict[str, str], text: str) -> str:
2628

2729
def _replace_all_occurences(
2830
substitutions: dict[str, str],
31+
repo_root: Path,
2932
exclude: set[Path] | None = None,
3033
) -> None:
3134
if exclude is None:
3235
exclude = set()
3336
# Get files in this repository (e.g., exclude venv/).
3437
known_files: set[Path] = set()
35-
outer_dir = Path(".").parent.resolve()
36-
proc = subprocess.run(["git", "ls-files"], encoding="utf-8", stdout=subprocess.PIPE, check=True)
38+
proc = subprocess.run(
39+
["git", "ls-files"],
40+
cwd=repo_root,
41+
encoding="utf-8",
42+
stdout=subprocess.PIPE,
43+
check=True,
44+
)
3745
for line in proc.stdout.split("\n"):
38-
known_files.add((outer_dir / line).resolve())
46+
if line:
47+
known_files.add((repo_root / line).resolve())
3948
known_files -= exclude
4049
for file_path in known_files:
4150
if file_path.is_dir():
@@ -50,8 +59,8 @@ def _replace_all_occurences(
5059

5160
def _main() -> None:
5261
# Parse the config.
53-
outer_dir = Path(".").parent.resolve()
54-
config_file = outer_dir / "config.json"
62+
repo_root = REPO_ROOT
63+
config_file = repo_root / "config.json"
5564
assert config_file.exists(), "Missing config file"
5665
with open(config_file, encoding="utf-8") as fp:
5766
config = json.load(fp)
@@ -71,10 +80,10 @@ def _main() -> None:
7180
assert python_subversion.isdigit()
7281

7382
# Get the repository name from this directory.
74-
repo_name = outer_dir.name
83+
repo_name = repo_root.name
7584

7685
# Delete the existing git files if they are from the starter repo.
77-
git_repo = outer_dir / ".git"
86+
git_repo = repo_root / ".git"
7887
if git_repo.exists():
7988
git_config_file = git_repo / "config"
8089
with open(git_config_file, encoding="utf-8") as fp:
@@ -85,22 +94,24 @@ def _main() -> None:
8594
shutil.rmtree(git_repo)
8695

8796
# Initialize the repo anew.
88-
subprocess.run(["git", "init"], check=True, capture_output=True)
97+
subprocess.run(["git", "init"], cwd=repo_root, check=True, capture_output=True)
8998
# Rename branch to main if not already on it
9099
current_branch = subprocess.run(
91-
["git", "rev-parse", "--abbrev-ref", "HEAD"],
100+
["git", "symbolic-ref", "--short", "HEAD"],
101+
cwd=repo_root,
92102
check=True,
93103
capture_output=True,
94104
encoding="utf-8",
95105
).stdout.strip()
96106
if current_branch != "main":
97-
subprocess.run(["git", "branch", "-M", "main"], check=True, capture_output=True)
98-
subprocess.run(["git", "add", "."], check=True, capture_output=True)
107+
subprocess.run(["git", "branch", "-M", "main"], cwd=repo_root, check=True, capture_output=True)
108+
subprocess.run(["git", "add", "."], cwd=repo_root, check=True, capture_output=True)
99109

100110
# Check if the remote already exists (if this script is being run twice).
101111
# This can happen if the user makes a mistake in their GitHub username.
102112
ret = subprocess.run(
103113
["git", "remote", "get-url", "origin"],
114+
cwd=repo_root,
104115
check=False,
105116
capture_output=True,
106117
)
@@ -119,6 +130,7 @@ def _main() -> None:
119130
"origin",
120131
github_url,
121132
],
133+
cwd=repo_root,
122134
check=True,
123135
capture_output=True,
124136
)
@@ -134,19 +146,16 @@ def _main() -> None:
134146
}
135147
_replace_all_occurences(
136148
substitutions,
149+
repo_root=repo_root,
137150
exclude={
138-
outer_dir / "apply_configuration.py",
151+
repo_root / "apply_configuration.py",
139152
config_file,
140-
outer_dir / "uv.lock",
153+
repo_root / "uv.lock",
141154
},
142155
)
143156

144157
# Rename the package repo.
145-
subprocess.run(
146-
["mv", "src/python_starter", f"src/{package_name}"],
147-
check=True,
148-
capture_output=True,
149-
)
158+
(repo_root / "src" / "python_starter").rename(repo_root / "src" / package_name)
150159

151160
# Report succcess.
152161
print("Configuration applied successfully.")

0 commit comments

Comments
 (0)