-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·103 lines (84 loc) · 2.95 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python3
import sys
import os
import glob
from importlib import import_module
import requests
import hashlib
import json
import cci
# Self-updater for modules
def update():
r = requests.get(
"https://api.github.com/repos/gmemstr/circleci-api-scripts/commits")
data = r.json()
file_exists = os.path.isfile("versions")
if file_exists is False:
with open("versions", "w+") as f:
f.write("{}")
print("Created versions file")
with open("versions", "r+") as cache:
version_cache = json.load(cache)
if "sha" not in version_cache:
version_cache['sha'] = ""
version_cache['modules'] = {}
if version_cache['sha'] != data[0]['sha']:
# Check modules for updates
r_mod = requests.get(
"https://api.github.com/repos/gmemstr/circleci-api-scripts/contents/modules")
data_mod = r_mod.json()
for module in data_mod:
name = module['name']
if name not in version_cache['modules']:
version_cache['modules'][name] = {"version": ""}
if version_cache['modules'][name]['version'] != module['sha']:
print("{name} updating...".format(name=name))
r_u = requests.get(module['download_url'])
with open("modules/" + name, "w+") as module_file:
module_file.write(r_u.text)
version_cache['modules'][name]['version'] = module['sha']
# Finally, write new versions file
version_cache['sha'] = data[0]['sha']
with open("versions", "w") as cache:
json.dump(version_cache, cache, indent=4)
def get_modules():
base_path = os.path.dirname(__file__)
files = [f for f in glob.glob(base_path + "/modules/*.py")]
processed_files = []
for name in files:
if "modules/__init__.py" in name:
continue
f = name.replace(".py", "").replace("modules/", "").replace(base_path+"/", "")
processed_files.append(f)
return processed_files
# Wrapper around Python scripts to make it easier to run.
# argv index 1 is the script to run
# argv index 2 onward is passed to the runCommand
if __name__ == '__main__':
if sys.argv[1] is None:
print("No command given")
exit(1)
command = sys.argv[1].lower()
modules = get_modules()
# Reserved commands - ['modules', 'update', 'test']
if command == "modules":
for module in modules:
print(module)
exit(1)
if command == "update":
Update()
exit(1)
if command == "test":
mod = import_module("tests.tests")
results = mod.run_tests()
exit(results)
valid = cci.validate_setup()
if not valid:
print("Cannot reach the CircleCI API:", valid)
exit(1)
if command in modules:
mod = import_module("modules." + command)
result = mod.run_command(sys.argv[2:])
else:
result = "Command not found"
print(result)