-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (90 loc) · 3.38 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
104
105
106
107
108
import subprocess
import json
import os
import sys
import atexit
import platform
log = open("log.txt", "w")
atexit.register(lambda: log.close)
def download_file(url: str, save_path: str) -> bool:
try:
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as file:
file.write(response.content)
return True
else:
return False
except Exception:
return False
def run_cmd(cmd: str) -> bool:
log.write(f"COMMAND: \n{cmd}\nOUTPUT:\n")
try:
process = subprocess.run(cmd, shell=True, text=True, capture_output=True)
if process.stdout: log.write(process.stdout)
if process.stderr: log.write(process.stderr)
log.write(f"EXIT CODE: {process.returncode}\n")
return process.returncode == 0
except Exception as e:
log.write(f"ERROR: {e}\n")
return False
def import_requests() -> None:
cmdlets = ["", "py -m", "python3 -m", "python -m"]
for cmdlet in cmdlets:
if run_cmd(f"{cmdlet} pip install requests"):
return
print("FATAL ERROR: Failed to install \'requests\' module")
exit(1)
def pause(text: str) -> None:
print(f"{text} \nEnter \"continue\" to continue")
while input().strip().lower() != "continue":
print("Enter \"continue\" to continue")
# MAIN
if len(sys.argv) != 2:
print("1 command line argument is expected. See the Readme for more information")
exit(1)
inp = sys.argv[1]
import_requests()
import requests
user_path = os.path.expanduser("~")
match platform.system():
case "Windows":
config = "windows.json"
case "Darwin":
config = "mac.json"
case _:
print("Unsupported OS: Only Windows and MacOS are supported")
exit(1)
with open(config, "r") as file:
instructions = json.load(file)
for instruction in instructions:
iterations = instruction["iterations"] if "iterations" in instruction else ["1 iteration only"]
for iter in iterations:
text = instruction["text"].replace("%ITER%", iter).replace("%IN%", inp)+":"
print(text)
log.write(f"\n\n{text}\n")
if "path" in instruction and os.path.exists(instruction["path"].replace("~~~", user_path)):
print("\tRequirement already fulfilled, \033[33mskipping\033[0m")
continue
if "skip_cmd" in instruction and run_cmd(instruction["skip_cmd"].replace("%ITER%", iter).replace("%IN%", inp)):
print("\tRequirement already fulfilled, \033[33mskipping\033[0m")
continue
if "download_URL" in instruction:
url = instruction["download_URL"][0]
save_path = instruction["download_URL"][1]
result = download_file(url, save_path)
if result:
print("\tDownloaded \033[32msuccessfully\033[0m")
else:
print("\tDownload \033[31mfailed\033[0m")
continue
cmd = instruction["cmd"].replace("%ITER%", iter).replace("%IN%", inp).replace("~~~", user_path)
result = run_cmd(cmd)
if result:
print("\tCommand executed \033[32msuccessfully\033[0m")
else:
print("\tCommand execution \033[31mfailed\033[0m")
continue
if "pause" in instruction:
pause(instruction["pause"])
print("DONE! Check \"log.txt\" for debug information")