Coverage for compiler_admin / commands / init.py: 98%
35 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 05:48 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 05:48 +0000
1import os
2import subprocess
3from pathlib import Path
4from shutil import rmtree
6import click
8from compiler_admin.services.google import GoogleUsers
10GAM_CONFIG_DIR = os.environ.get("GAMCFGDIR", "./.config/gam")
11GAM_CONFIG_PATH = Path(GAM_CONFIG_DIR)
12GYB_CONFIG_PATH = GAM_CONFIG_PATH.parent / "gyb"
15def _clean_config_dir(config_dir: Path) -> None:
16 config_dir.mkdir(parents=True, exist_ok=True)
17 for path in config_dir.glob("**/*"):
18 if path.is_file():
19 path.unlink()
20 elif path.is_dir(): 20 ↛ 17line 20 didn't jump to line 17 because the condition on line 20 was always true
21 rmtree(path)
24@click.command()
25@click.option("--gam", "init_gam", is_flag=True)
26@click.option("--gyb", "init_gyb", is_flag=True)
27@click.argument("username")
28def init(username: str, init_gam: bool = False, init_gyb: bool = False):
29 """Initialize a new GAM and/or GYB project.
31 See:
33 - <https://github.com/GAM-team/GAM/wiki/How-to-Install-GAM7>
34 - <https://github.com/GAM-team/got-your-back/wiki>
35 """
36 google = GoogleUsers()
38 if init_gam:
39 _clean_config_dir(GAM_CONFIG_PATH)
40 # GAM is already installed via pyproject.toml
41 google.gam_command(("config", "drive_dir", str(GAM_CONFIG_PATH), "verify"))
42 google.gam_command(("create", "project"))
43 google.gam_command(("oauth", "create"))
44 google.gam_command(("user", username, "check", "serviceaccount"))
46 if init_gyb:
47 _clean_config_dir(GYB_CONFIG_PATH)
48 # download GYB installer to config directory
49 gyb = GYB_CONFIG_PATH / "gyb-install.sh"
50 with gyb.open("w+") as dest:
51 subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest)
53 subprocess.call(("chmod", "+x", str(gyb.absolute())))
55 # install, giving values to some options
56 # https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh
57 #
58 # use GYB_CONFIG_PATH.parent for the install directory option, otherwise we get a .config/gyb/gyb directory structure
59 subprocess.call((gyb, "-u", username, "-r", google.USER_ARCHIVE, "-d", str(GYB_CONFIG_PATH.parent)))