Skip to content

Commit 51c5ce7

Browse files
committed
refactor(init): optionally init GAM, GYB projects
1 parent ee12839 commit 51c5ce7

File tree

2 files changed

+66
-41
lines changed

2 files changed

+66
-41
lines changed

compiler_admin/commands/init.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,62 @@
77
from compiler_admin.services.google import USER_ARCHIVE, CallGAMCommand
88

99

10-
CONFIG_DIR = os.environ.get("GAMCFGDIR", "./.config")
11-
CONFIG_PATH = Path(CONFIG_DIR)
12-
CONFIG_PATH_NAME = str(CONFIG_PATH)
10+
GAM_CONFIG_DIR = os.environ.get("GAMCFGDIR", "./.config/gam")
11+
GAM_CONFIG_PATH = Path(GAM_CONFIG_DIR)
12+
GYB_CONFIG_PATH = GAM_CONFIG_PATH.parent / "gyb"
1313

1414

15-
def _clean_config_dir():
16-
for path in CONFIG_PATH.glob("**/*"):
17-
if path.is_file():
18-
path.unlink()
19-
elif path.is_dir():
20-
rmtree(path)
15+
def _clean_config_dir(gam: bool, gyb: bool) -> None:
16+
if gam:
17+
for path in GAM_CONFIG_PATH.glob("**/*"):
18+
if path.is_file():
19+
path.unlink()
20+
elif path.is_dir():
21+
rmtree(path)
22+
if gyb:
23+
for path in GYB_CONFIG_PATH.glob("**/*"):
24+
if path.is_file():
25+
path.unlink()
26+
elif path.is_dir():
27+
rmtree(path)
2128

2229

23-
def init(admin_user: str) -> int:
30+
def init(admin_user: str, gam: bool = False, gyb: bool = False) -> int:
2431
"""Initialize a new GAM project.
2532
2633
See https://github.com/taers232c/GAMADV-XTD3/wiki/How-to-Install-Advanced-GAM
2734
2835
Args:
2936
admin_user (str): The Compiler admin with which to initialize a new project.
37+
38+
init_gam (bool): If True, initialize a new GAM project.
39+
40+
init_gyb (bool): If True, initialize a new GYB project.
3041
Returns:
3142
A value indicating if the operation succeeded or failed.
3243
"""
33-
if CONFIG_PATH.exists():
34-
_clean_config_dir()
35-
36-
res = CallGAMCommand(("config", "drive_dir", CONFIG_PATH_NAME, "verify"))
37-
res += CallGAMCommand(("create", "project"))
38-
res += CallGAMCommand(("oauth", "create"))
39-
res += CallGAMCommand(("user", admin_user, "check", "serviceaccount"))
40-
41-
# download GYB installer to config directory
42-
gyb = CONFIG_PATH / "gyb-install.sh"
43-
with gyb.open("w+") as dest:
44-
res += subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest)
45-
46-
# install, giving values to options that prompt by default
47-
# https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh
48-
res += subprocess.call((gyb, "-u", admin_user, "-r", USER_ARCHIVE))
44+
_clean_config_dir(gam, gyb)
45+
46+
res = RESULT_SUCCESS
47+
48+
if gam:
49+
GAM_CONFIG_PATH.mkdir(parents=True, exist_ok=True)
50+
res += CallGAMCommand(("config", "drive_dir", str(GAM_CONFIG_PATH), "verify"))
51+
res += CallGAMCommand(("create", "project"))
52+
res += CallGAMCommand(("oauth", "create"))
53+
res += CallGAMCommand(("user", admin_user, "check", "serviceaccount"))
54+
55+
if gyb:
56+
GYB_CONFIG_PATH.mkdir(parents=True, exist_ok=True)
57+
# download GYB installer to config directory
58+
gyb = GYB_CONFIG_PATH / "gyb-install.sh"
59+
with gyb.open("w+") as dest:
60+
res += subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest)
61+
62+
# install, giving values to some options
63+
# https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh
64+
#
65+
# use GYB_CONFIG_PATH.parent for the install directory option, otherwise we get a .config/gyb/gyb directory structure
66+
res += subprocess.call((gyb, "-u", admin_user, "-r", USER_ARCHIVE, "-d", str(GYB_CONFIG_PATH.parent)))
4967

5068
return RESULT_SUCCESS if res == RESULT_SUCCESS else RESULT_FAILURE

tests/commands/test_init.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import pytest
22

3+
import compiler_admin.commands.init
34
from compiler_admin.commands.init import init, __name__ as MODULE
45

56

67
@pytest.fixture
7-
def mock_CONFIG_PATH(mocker):
8-
return mocker.patch(f"{MODULE}.CONFIG_PATH")
8+
def spy_GAM_CONFIG_PATH(mocker):
9+
return mocker.spy(compiler_admin.commands.init, "GAM_CONFIG_PATH")
10+
11+
12+
@pytest.fixture
13+
def spy_GYB_CONFIG_PATH(mocker):
14+
return mocker.spy(compiler_admin.commands.init, "GYB_CONFIG_PATH")
915

1016

1117
@pytest.fixture
@@ -23,23 +29,24 @@ def mock_subprocess_call(mocker):
2329
return mocker.patch(f"{MODULE}.subprocess.call")
2430

2531

26-
def test_init_config_path_exists(mock_CONFIG_PATH, mock_clean_config_dir, mock_google_CallGAMCommand, mock_subprocess_call):
27-
mock_CONFIG_PATH.exists.return_value = True
28-
29-
init("username")
32+
def test_init_cleans_config(mock_clean_config_dir):
33+
init("username", gam=False, gyb=False)
3034

3135
mock_clean_config_dir.assert_called_once()
32-
assert mock_google_CallGAMCommand.call_count > 0
33-
assert mock_subprocess_call.call_count > 0
34-
36+
assert mock_clean_config_dir.call_args.args == (False, False)
3537

36-
def test_init_config_path_does_not_exist(
37-
mock_CONFIG_PATH, mock_clean_config_dir, mock_google_CallGAMCommand, mock_subprocess_call
38-
):
39-
mock_CONFIG_PATH.exists.return_value = False
4038

41-
init("username")
39+
@pytest.mark.usefixtures("mock_clean_config_dir")
40+
def test_init_gam(spy_GAM_CONFIG_PATH, mock_google_CallGAMCommand):
41+
init("username", gam=True, gyb=False)
4242

43-
assert mock_clean_config_dir.call_count == 0
43+
spy_GAM_CONFIG_PATH.mkdir.assert_called_once()
4444
assert mock_google_CallGAMCommand.call_count > 0
45+
46+
47+
@pytest.mark.usefixtures("mock_clean_config_dir")
48+
def test_init_gyb(spy_GYB_CONFIG_PATH, mock_subprocess_call):
49+
init("username", gam=False, gyb=True)
50+
51+
spy_GYB_CONFIG_PATH.mkdir.assert_called_once()
4552
assert mock_subprocess_call.call_count > 0

0 commit comments

Comments
 (0)