-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkout.py
executable file
·87 lines (68 loc) · 2.02 KB
/
checkout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import json
import os
import sys
def getModules():
try:
with open('fgs.json') as f:
config = json.load(f)
except FileNotFoundError:
displayHelp(True)
sys.exit(1)
# Add the default branch, if not explicitly indicated in the config.
config = {
k: {**config[k], 'branch': config[k].get('branch', 'main')}
for k in config
}
return config
def checkPath(path):
if not os.path.exists(path):
return "missing"
if os.path.isdir(path):
return "folder"
return "notFolder"
def execGitCommands(modules):
"""
Turn config into appropriate git commands. If a destination already
exists then we do git pull, if not: git clone
"""
for module in modules:
branch = modules[module]['branch']
path = module
url = modules[module]['url']
whatIsIt = checkPath(module)
if whatIsIt == "missing":
cmd = f"git clone -b {branch} {url} {path}"
# print(cmd)
os.system(cmd)
if whatIsIt == "folder":
cmd = f"cd {module} && git pull"
# print(cmd)
os.system(cmd)
if whatIsIt == "notFolder":
print ("Cannot create '{module}' folder because path already exists")
def addIfMissing(ignorefile, entry):
if not os.path.exists(ignorefile):
os.system(f"touch {ignorefile}")
with open(ignorefile,mode='r') as file:
contents = file.read()
if not entry in contents:
with open(ignorefile, 'a') as fo:
fo.write(f"{entry}\n")
print (f"Added {entry} to {ignorefile}");
#else:
# print (f"ignorefile has {entry}")
def displayHelp(missingConfig=False):
iam = os.path.basename(__file__)
if missingConfig == True:
print (f"Missing: fgs.json configuration file!")
print("For an example fgs.json, see: https://github.com/inadarei/faux-git-submodules")
print("")
print ("Command utility to easily check-out faux git submodules.")
print("Usage: ./"+f"{iam}")
print ("For more information: https://github.com/inadarei/faux-git-submodules")
#-------------------------- Main Logic
modules = getModules();
execGitCommands(modules);
for reponame in modules:
addIfMissing(".gitignore", reponame)