|
| 1 | +"""Script responsible for first time setup of the project's git repo's remote connection. |
| 2 | +
|
| 3 | +Since this is a first time setup script, we intentionally only use builtin Python dependencies. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from util import check_dependencies |
| 11 | +from util import existing_dir |
| 12 | + |
| 13 | + |
| 14 | +def main() -> None: |
| 15 | + """Parses command line input and passes it through to setup_git.""" |
| 16 | + parser: argparse.ArgumentParser = get_parser() |
| 17 | + args: argparse.Namespace = parser.parse_args() |
| 18 | + setup_remote( |
| 19 | + path=args.path, |
| 20 | + repository_host=args.repository_host, |
| 21 | + repository_path=args.repository_path, |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def setup_remote(path: Path, repository_host: str, repository_path: str) -> None: |
| 26 | + """Set up the provided cookiecutter-robust-python project's git repo.""" |
| 27 | + commands: list[list[str]] = [ |
| 28 | + [ |
| 29 | + "git", |
| 30 | + "remote", |
| 31 | + "add", |
| 32 | + "origin", |
| 33 | + f"https://{repository_host}/{repository_path}.git", |
| 34 | + ], |
| 35 | + [ |
| 36 | + "git", |
| 37 | + "remote", |
| 38 | + "set-url", |
| 39 | + "origin", |
| 40 | + f"https://{repository_host}/{repository_path}.git", |
| 41 | + ], |
| 42 | + ["git", "fetch", "origin"], |
| 43 | + ["git", "checkout", "main"], |
| 44 | + ["git", "push", "-u", "origin", "main"], |
| 45 | + ["git", "checkout", "develop"], |
| 46 | + ["git", "push", "-u", "origin", "develop"], |
| 47 | + ] |
| 48 | + check_dependencies(path=path, dependencies=["git"]) |
| 49 | + |
| 50 | + for command in commands: |
| 51 | + subprocess.run(command, cwd=path, stderr=subprocess.STDOUT) |
| 52 | + |
| 53 | + |
| 54 | +def get_parser() -> argparse.ArgumentParser: |
| 55 | + """Creates the argument parser for setup-remote.""" |
| 56 | + parser: argparse.ArgumentParser = argparse.ArgumentParser( |
| 57 | + prog="setup-remote", |
| 58 | + usage="python ./scripts/setup-remote.py . --host github.com --path 56kyle/robust-python-demo", |
| 59 | + description="Set up the provided cookiecutter-robust-python project's remote repo connection.", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "path", |
| 63 | + type=existing_dir, |
| 64 | + metavar="PATH", |
| 65 | + help="Path to the repo's root directory (must already exist).", |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--host", |
| 69 | + dest="repository_host", |
| 70 | + help="Repository host (e.g., github.com, gitlab.com).", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--path", |
| 74 | + dest="repository_path", |
| 75 | + help="Repository path (e.g., user/repo, group/subgroup/repo).", |
| 76 | + ) |
| 77 | + return parser |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments