Skip to content

Commit 5718ac5

Browse files
committed
feat: improve git setup logic
1 parent 71ba308 commit 5718ac5

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

{{cookiecutter.project_name}}/noxfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
REPO_ROOT: Path = Path(__file__).parent.resolve()
2727
SCRIPTS_FOLDER: Path = REPO_ROOT / "scripts"
2828
CRATES_FOLDER: Path = REPO_ROOT / "rust"
29+
30+
PROJECT_NAME: str = "{{cookiecutter.project_name}}"
2931
PACKAGE_NAME: str = "{{cookiecutter.package_name}}"
3032
GITHUB_USER: str = "{{cookiecutter.github_user}}"
3133

@@ -48,7 +50,7 @@
4850
def setup_git(session: Session) -> None:
4951
"""Set up the git repo for the current project."""
5052
session.run(
51-
"python", SCRIPTS_FOLDER / "setup-git.py", REPO_ROOT, "-u", GITHUB_USER, "-n", PACKAGE_NAME, external=True
53+
"python", SCRIPTS_FOLDER / "setup-git.py", REPO_ROOT, "-u", GITHUB_USER, "-n", PROJECT_NAME, external=True
5254
)
5355

5456

{{cookiecutter.project_name}}/scripts/setup-git.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ def setup_git(path: Path, github_user: str, repo_name: str) -> None:
2121
"""Set up the provided cookiecutter-robust-python project's git repo."""
2222
commands: list[list[str]] = [
2323
["git", "init"],
24-
["git", "branch", "-M", "main"],
24+
["git", "branch", "-m", "master", "main"],
25+
["git", "checkout", "main"],
2526
["git", "remote", "add", "origin", f"https://github.com/{github_user}/{repo_name}.git"],
2627
["git", "remote", "set-url", "origin", f"https://github.com/{github_user}/{repo_name}.git"],
2728
["git", "fetch", "origin"],
29+
["git", "pull"],
2830
["git", "push", "-u", "origin", "main"],
2931
["git", "checkout", "-b", "develop", "main"],
3032
["git", "push", "-u", "origin", "develop"],
@@ -35,7 +37,7 @@ def setup_git(path: Path, github_user: str, repo_name: str) -> None:
3537
check_dependencies(path=path, dependencies=["git"])
3638

3739
for command in commands:
38-
subprocess.run(command, cwd=path, stdout=subprocess.STDOUT, stderr=subprocess.STDOUT)
40+
subprocess.run(command, cwd=path, stderr=subprocess.STDOUT)
3941

4042

4143
def get_parser() -> argparse.ArgumentParser:

{{cookiecutter.project_name}}/scripts/setup-venv.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
Since this a first time setup script, we intentionally only use builtin Python dependencies.
44
"""
55
import argparse
6+
import shutil
67
import subprocess
78
from pathlib import Path
89

910
from util import check_dependencies
1011
from util import existing_dir
12+
from util import remove_readonly
1113

1214

1315
def main() -> None:
@@ -49,6 +51,10 @@ def setup_venv(path: Path, python_version: str) -> None:
4951
]
5052
check_dependencies(path=path, dependencies=["uv"])
5153

54+
venv_path: Path = path / ".venv"
55+
if venv_path.exists():
56+
shutil.rmtree(venv_path, onerror=remove_readonly)
57+
5258
for command in commands:
5359
subprocess.run(command, cwd=path, capture_output=True)
5460

{{cookiecutter.project_name}}/scripts/util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""Module containing util"""
22
import argparse
3+
import os
4+
import stat
35
import subprocess
46
from pathlib import Path
7+
from typing import Any
8+
from typing import Callable
59

610

711
class MissingDependencyError(Exception):
@@ -32,3 +36,12 @@ def existing_dir(value: str) -> Path:
3236
raise argparse.ArgumentTypeError(f"{path} is not a directory.")
3337

3438
return path
39+
40+
41+
def remove_readonly(func: Callable[[str], Any], path: str, _: Any) -> None:
42+
"""Clears the readonly bit and attempts to call the provided function.
43+
44+
This is passed to shutil.rmtree as the onerror kwarg.
45+
"""
46+
os.chmod(path, stat.S_IWRITE)
47+
func(path)

0 commit comments

Comments
 (0)