|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import shlex |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +ERROR_MSG = "Error occured while running cmd" |
| 10 | +PRETTY_LINES = "*" * 80 |
| 11 | + |
| 12 | + |
| 13 | +def main() -> int: |
| 14 | + working_dir = Path().resolve() |
| 15 | + git_dir = Path(working_dir, ".git") |
| 16 | + ret_code_one = 0 |
| 17 | + if not git_dir.exists(): |
| 18 | + ret_code_one = initialize_git() |
| 19 | + |
| 20 | + if ret_code_one == 0: |
| 21 | + ret_code_two = setup_environment() |
| 22 | + return ret_code_one or ret_code_two or 0 |
| 23 | + |
| 24 | + |
| 25 | +def initialize_git() -> int: |
| 26 | + COMMANDS_AND_MESSAGE = { |
| 27 | + "default_branch": ( |
| 28 | + shlex.split("git config --global init.defaultBranch main"), |
| 29 | + PRETTY_LINES, |
| 30 | + ), |
| 31 | + "init_git": ( |
| 32 | + shlex.split("git init"), |
| 33 | + "Initializing an empty git repository\n", |
| 34 | + ), |
| 35 | + } |
| 36 | + for cmds, message in COMMANDS_AND_MESSAGE.values(): |
| 37 | + print(message) |
| 38 | + try: |
| 39 | + subprocess.run(cmds, check=True) |
| 40 | + except subprocess.CalledProcessError as e: |
| 41 | + print(ERROR_MSG, e) |
| 42 | + return e.returncode |
| 43 | + |
| 44 | + return 0 |
| 45 | + |
| 46 | + |
| 47 | +def setup_environment() -> int: |
| 48 | + |
| 49 | + COMMANDS_AND_MESSAGE = { |
| 50 | + "install_poetry": ( |
| 51 | + shlex.split("poetry install"), |
| 52 | + f"\n{PRETTY_LINES}\nInstalling poetry virtual environment", |
| 53 | + ), |
| 54 | + "install_pre_commit": ( |
| 55 | + shlex.split( |
| 56 | + "poetry run pre-commit install --hook-type pre-commit --hook-type pre-push" |
| 57 | + ), |
| 58 | + f"\n{PRETTY_LINES}\nInstalling pre-commit hooks", |
| 59 | + ), |
| 60 | + } |
| 61 | + for cmds, message in COMMANDS_AND_MESSAGE.values(): |
| 62 | + print(message) |
| 63 | + try: |
| 64 | + subprocess.run(cmds, check=True) |
| 65 | + except subprocess.CalledProcessError as e: |
| 66 | + print(ERROR_MSG, e) |
| 67 | + return e.returncode |
| 68 | + |
| 69 | + return 0 |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + sys.exit(main()) |
0 commit comments