-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
345 lines (330 loc) · 18.2 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# imports ------------------------------------------------------
import sys
import json
import os
import shutil
# --------------------------------------------------------------
# getting pathes -----------------------------------------------
if getattr(sys, 'frozen', False):
app_path: str = os.path.dirname(sys.executable)
elif __file__:
app_path: str = os.path.dirname(__file__)
app_path = app_path.replace("\\", "/")
call_path: str = os.getcwd().replace("\\", "/")
# --------------------------------------------------------------
# additional ---------------------------------------------------
def isempty(path: str) -> bool: # is the file empty
with open(path, 'r') as file:
if file.read() == '':
return True
return False
# --------------------------------------------------------------
# file types ---------------------------------------------------
def launch(path: str, workfolder: str = call_path) -> None: # installing launch.json
with open(path, 'r', encoding="utf-8") as file:
file_dict: dict = json.loads(file.read())
if not os.path.exists(f"{workfolder}/.vscode/launch.json"):
with open(f"{workfolder}/.vscode/launch.json", 'w', encoding="utf-8") as vsccm:
vsccm.write('{"version": "2.0.0", "configurations": []}')
if isempty(f"{workfolder}/.vscode/launch.json"):
with open(f"{workfolder}/.vscode/launch.json", 'w') as vsccm:
vsccm.write('{"version": "2.0.0", "configurations": []}')
with open(f"{workfolder}/.vscode/launch.json", 'r', encoding="utf-8") as vsc_file:
vsc_file_dict: dict = json.loads(vsc_file.read())
try:
for c in file_dict["configurations"]:
vsc_file_dict["configurations"].append(c)
except Exception:
print("Something wrong with a file")
return
vsc_config_list: list = []
for x in vsc_file_dict["configurations"]:
if x not in vsc_config_list:
vsc_config_list.append(x)
vsc_file_dict["configurations"] = vsc_config_list
with open(f"{workfolder}/.vscode/launch.json", 'w', encoding="utf-8") as vsc_file:
vsc_file.write(json.dumps(vsc_file_dict, ensure_ascii=False, indent=4))
def tasks(path: str, workfolder: str = call_path) -> None: # installing tasks.json
with open(path, 'r', encoding="utf-8") as file:
file_dict: dict = json.loads(file.read())
if not os.path.exists(f"{workfolder}/.vscode/tasks.json"):
with open(f"{workfolder}/.vscode/tasks.json", 'w', encoding="utf-8") as vsccm:
vsccm.write('{"version": "2.0.0", "tasks": []}')
with open(f"{workfolder}/.vscode/tasks.json", 'r', encoding="utf-8") as vsc_file:
vsc_file_dict: dict = json.loads(vsc_file.read())
if isempty(f"{workfolder}/.vscode/tasks.json"):
with open(f"{workfolder}/.vscode/tasks.json", 'w') as vsccm:
vsccm.write('{"version": "2.0.0", "tasks": []}')
try:
for c in file_dict["tasks"]:
vsc_file_dict["tasks"].append(c)
except Exception:
print("Something wrong with a file")
return
vsc_config_list: list = []
for x in vsc_file_dict["tasks"]:
if x not in vsc_config_list:
vsc_config_list.append(x)
vsc_file_dict["tasks"] = vsc_config_list
with open(f"{workfolder}/.vscode/tasks.json", 'w', encoding="utf-8") as vsc_file:
vsc_file.write(json.dumps(vsc_file_dict, ensure_ascii=False, indent=4))
def settings(path: str, workfolder: str = call_path) -> None: # installing settings.json
with open(path, 'r', encoding="utf-8") as file:
file_dict: dict = json.loads(file.read())
if not os.path.exists(f"{workfolder}/.vscode/settings.json"):
with open(f"{workfolder}/.vscode/settings.json", 'w', encoding="utf-8") as vsccm:
vsccm.write('{}')
with open(f"{workfolder}/.vscode/settings.json", 'r', encoding="utf-8") as vsc_file:
vsc_file_dict: dict = json.loads(vsc_file.read())
if isempty(f"{workfolder}/.vscode/settings.json"):
with open(f"{workfolder}/.vscode/settings.json", 'w') as vsccm:
vsccm.write('{}')
try:
for c in file_dict:
vsc_file_dict[c] = file_dict[c]
except Exception:
print("Something wrong with a file")
return
with open(f"{workfolder}/.vscode/settings.json", 'w', encoding="utf-8") as vsc_file:
vsc_file.write(json.dumps(vsc_file_dict, ensure_ascii=False, indent=4))
def c_cpp_properties(path: str, workfolder: str = call_path): # installing c_cpp_properties.json
with open(path, 'r', encoding="utf-8") as file:
file_dict: dict = json.loads(file.read())
if not os.path.exists(f"{workfolder}/.vscode/c_cpp_properties.json"):
with open(f"{workfolder}/.vscode/c_cpp_properties.json", 'w', encoding="utf-8") as vsccm:
vsccm.write('{"configurations": []}')
if isempty(f"{workfolder}/.vscode/c_cpp_properties.json"):
with open(f"{workfolder}/.vscode/c_cpp_properties.json", 'w') as vsccm:
vsccm.write('{"configurations": []}')
with open(f"{workfolder}/.vscode/c_cpp_properties.json", 'r', encoding="utf-8") as vsc_file:
vsc_file_dict: dict = json.loads(vsc_file.read())
try:
for c in file_dict["configurations"]:
vsc_file_dict["configurations"].append(c)
except Exception:
print("Something wrong with a file")
return
vsc_config_list: list = []
for x in vsc_file_dict["configurations"]:
if x not in vsc_config_list:
vsc_config_list.append(x)
vsc_file_dict["configurations"] = vsc_config_list
with open(f"{workfolder}/.vscode/c_cpp_properties.json", 'w', encoding="utf-8") as vsc_file:
vsc_file.write(json.dumps(vsc_file_dict, ensure_ascii=False, indent=4))
# --------------------------------------------------------------
# commands -----------------------------------------------------
def list_command(path: str) -> None: # command 'vsccm list'
if not os.path.exists(f"{path}/.vscode") or not os.path.exists(f"{path}/vsccm-lock.json"):
print("You haven't installed any configs")
else:
with open(f"{path}/vsccm-lock.json") as vsccm:
vsccm_dict: dict = json.loads(vsccm.read())
print("Installed configs:")
for cfg in vsccm_dict["configs"]:
print(f"|-{cfg}")
def remove_command(path: str) -> None: # command 'vsccm remove'
if os.path.exists(f"{path}/.vscode"):
shutil.rmtree(f"{path}/.vscode")
if os.path.exists(f"{path}/vsccm-lock.json"):
os.remove(f"{path}/vsccm-lock.json")
print("Directory configs removed")
def clear_command(path: str) -> None: # command 'vsccm clear'
if os.path.exists(f"{path}/.vscode"):
shutil.rmtree(f"{path}/.vscode")
os.mkdir(f"{path}/.vscode")
if os.path.exists(f"{path}/vsccm-lock.json"):
vsccm = open(f"{path}/vsccm-lock.json", 'w')
vsccm.write('{"configs": []}')
vsccm.close()
print("Directory configs cleared")
def install_list_command() -> None: # command 'vsccm install list'
global app_path
print("Your configs:")
for config in os.listdir(f"{app_path}/configs/"):
if os.path.isdir(f"{app_path}/configs/{config}"):
print(f"|-{config}")
for file in os.listdir(f"{app_path}/configs/{config}"):
if os.path.isfile(f"{app_path}/configs/{config}/{file}"):
print(f" |-{file}")
def install_args_command(path: str, config_name: str) -> None: # command 'vsccm install' with arguments
global app_path, call_path
if not os.path.exists(f"{path}"):
os.mkdir(f"{path}")
if not os.path.exists(f"{path}/.vscode"):
os.mkdir(f"{path}/.vscode")
if not os.path.exists(f"{path}/vsccm-lock.json"):
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write('{"configs": []}')
if isempty(f"{path}/vsccm-lock.json"):
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write('{"configs": []}')
with open(f"{path}/vsccm-lock.json", 'r') as vsccm:
configs: dict = json.loads(vsccm.read())
try:
configs_list: list = []
with open(f"{call_path}/{config_name + ('.json' if config_name[-5:] != '.json' else '')}", 'r') as vsccm:
configs_list = json.loads(vsccm.read())["configs"]
if len(configs_list) > 0:
for config in configs_list:
try:
files: list = list(filter(lambda file: os.path.isfile(f"{app_path}/configs/{config}/{file}"), os.listdir(f"{app_path}/configs/{config}")))
for f in files:
if f == "launch.json":
launch(f"{app_path}/configs/{config}/launch.json", path)
print(f"'{config}' launch.json successfully installed")
elif f == "tasks.json":
tasks(f"{app_path}/configs/{config}/tasks.json", path)
print(f"'{config}' tasks.json successfully installed")
elif f == "settings.json":
settings(f"{app_path}/configs/{config}/settings.json", path)
print(f"'{config}' settings.json successfully installed")
elif f == "c_cpp_properties.json":
c_cpp_properties(f"{app_path}/configs/{config}/c_cpp_properties.json", path)
print(f"'{config}' c_cpp_properties.json successfully installed")
configs["configs"].append(config)
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write(json.dumps(configs, ensure_ascii=False, indent=4))
print(f"'{config}' SUCCESSFULLY INSTALLED")
except Exception:
print(f"'{config}' NOT FOUND")
except Exception:
print("Uncorrect arguments")
def install_config(path: str, i: int) -> None: # install config
with open(f"{path}/vsccm-lock.json", 'r') as vsccm:
configs: dict = json.loads(vsccm.read())
files: list = list(filter(lambda file: os.path.isfile(f"{app_path}/configs/{sys.argv[i]}/{file}"), os.listdir(f"{app_path}/configs/{sys.argv[i]}")))
for f in files:
if f == "launch.json":
launch(f"{app_path}/configs/{sys.argv[i]}/launch.json", path)
print(f"'{sys.argv[i]}' launch.json successfully installed")
elif f == "tasks.json":
tasks(f"{app_path}/configs/{sys.argv[i]}/tasks.json")
print(f"'{sys.argv[i]}' tasks.json successfully installed", path)
elif f == "settings.json":
settings(f"{app_path}/configs/{sys.argv[i]}/settings.json", path)
print(f"'{sys.argv[i]}' settings.json successfully installed")
elif f == "c_cpp_properties.json":
c_cpp_properties(f"{app_path}/configs/{sys.argv[i]}/c_cpp_properties.json", path)
print(f"'{sys.argv[i]}' c_cpp_properties.json successfully installed")
configs["configs"].append(sys.argv[i])
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write(json.dumps(configs, ensure_ascii=False, indent=4))
print(f"'{sys.argv[i]}' SUCCESSFULLY INSTALLED")
def install_configs_command(path: str, start: int) -> None: # command 'vsccm install {config_name}'
if not os.path.exists(f"{path}"):
os.mkdir(f"{path}")
if not os.path.exists(f"{path}/.vscode"):
os.mkdir(f"{path}/.vscode")
if not os.path.exists(f"{path}/vsccm-lock.json"):
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write('{"configs": []}')
if isempty(f"{path}/vsccm-lock.json"):
with open(f"{path}/vsccm-lock.json", 'w') as vsccm:
vsccm.write('{"configs": []}')
with open(f"{path}/vsccm-lock.json", 'r') as vsccm:
configs: dict = json.loads(vsccm.read())
for i in range(start, len(sys.argv)):
if sys.argv[i].lower() not in configs["configs"]:
install_config(path, i)
else:
print(f"'{sys.argv[i]}' has already been installed")
def install_command(path: str) -> None: # command 'vsccm install'
global app_path
if os.path.exists(f"{path}/vsccm-lock.json"):
if not os.path.exists(f"{path}/.vscode"):
os.mkdir(f"{path}/.vscode")
configs_list: list = []
with open(f"{path}/vsccm-lock.json", 'r') as vsccm:
configs_list = json.loads(vsccm.read())["configs"]
if len(configs_list) > 0:
for config in configs_list:
try:
files: list = list(filter(lambda file: os.path.isfile(f"{app_path}/configs/{config}/{file}"), os.listdir(f"{app_path}/configs/{config}")))
for f in files:
if f == "launch.json":
launch(f"{app_path}/configs/{config}/launch.json", path)
print(f"'{config}' launch.json successfully installed")
elif f == "tasks.json":
tasks(f"{app_path}/configs/{config}/tasks.json", path)
print(f"'{config}' tasks.json successfully installed")
elif f == "settings.json":
settings(f"{app_path}/configs/{config}/settings.json", path)
print(f"'{config}' settings.json successfully installed")
elif f == "c_cpp_properties.json":
c_cpp_properties(f"{app_path}/configs/{config}/c_cpp_properties.json", path)
print(f"'{config}' c_cpp_properties.json successfully installed")
print(f"'{config}' SUCCESSFULLY INSTALLED")
except Exception:
print(f"'{config}' NOT FOUND")
else:
print("There is no configs in you vsccm config file")
else:
print("There is no folder or vsccm config file")
# --------------------------------------------------------------
# main part ----------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) == 1: # 'vsccm'
print("install — install config\nlist — list of installed configs\nremove — remove .vscode folder\nclear — clear configs")
elif sys.argv[1] == "list" or sys.argv[1] == "l":
if len(sys.argv) > 2 and sys.argv[2][:13] == "--workfolder=": # 'vsccm list --workfolder='
list_command(sys.argv[2][13:])
elif len(sys.argv) > 2 and sys.argv[2][:12] == "-workfolder=": # 'vsccm list -workfolder='
list_command(sys.argv[2][12:])
else: # 'vsccm list'
list_command(call_path)
elif sys.argv[1] == "remove" or sys.argv[1] == "r":
if len(sys.argv) > 2 and sys.argv[2][:13] == "--workfolder=": # 'vsccm remove --workfolder='
remove_command(sys.argv[2][13:])
elif len(sys.argv) > 2 and sys.argv[2][:12] == "-workfolder=": # 'vsccm remove -workfolder='
remove_command(sys.argv[2][12:])
else: # 'vsccm remove'
remove_command(call_path)
elif sys.argv[1] == "clear" or sys.argv[1] == "c":
if len(sys.argv) > 2 and sys.argv[2][:13] == "--workfolder=": # 'vsccm clear --workfolder='
clear_command(sys.argv[2][13:])
elif len(sys.argv) > 2 and sys.argv[2][:12] == "-workfolder=": # 'vsccm clear -workfolder='
clear_command(sys.argv[2][12:])
else: # 'vsccm clear'
clear_command(call_path)
elif sys.argv[1] == "install" or sys.argv[1] == "i": # 'vsccm install'
if len(sys.argv) == 3:
if sys.argv[2] == "list" or sys.argv[2] == "l": # 'vsccm install list'
install_list_command()
elif sys.argv[2][:13] == "--configfile=": # 'vsccm install --configfile='
install_args_command(call_path, sys.argv[2][13:])
elif sys.argv[2][:12] == "-configfile=": # 'vsccm install -configfile='
install_args_command(call_path, sys.argv[2][12:])
elif sys.argv[2][:13] == "--workfolder=": # 'vsccm install --workfolder='
install_command(sys.argv[2][13:])
elif sys.argv[2][:12] == "-workfolder=": # 'vsccm install -workfolder='
install_command(sys.argv[2][12:])
else: # 'vsccm install {config_name}'
install_configs_command(call_path, 2)
elif len(sys.argv) == 4:
if sys.argv[2][:13] == "--workfolder=" and sys.argv[3][:13] == "--configfile=": # 'vsccm install --workfolder= --configfile='
install_args_command(sys.argv[2][13:], sys.argv[3][13:])
elif sys.argv[2][:12] == "-workfolder=" and sys.argv[3][:12] == "-configfile=": # 'vsccm install -workfolder= -configfile='
install_args_command(sys.argv[2][12:], sys.argv[3][12:])
elif sys.argv[3][:13] == "--workfolder=" and sys.argv[2][:13] == "--configfile=": # 'vsccm install --configfile= --workfolder='
install_args_command(sys.argv[3][13:], sys.argv[2][13:])
elif sys.argv[3][:13] == "-workfolder=" and sys.argv[2][:13] == "-configfile=": # 'vsccm install -configfile= -workfolder='
install_args_command(sys.argv[3][12:], sys.argv[2][12:])
else:
if sys.argv[2][:13] == "--workfolder=": # 'vsccm install --workfolder= {config_name}'
install_configs_command(sys.argv[2][13:], 3)
elif sys.argv[2][:12] == "-workfolder=": # 'vsccm install -workfolder= {config_name}'
install_configs_command(sys.argv[2][12:], 3)
else: # 'vsccm install {config_name} {config_name}'
install_configs_command(call_path, 2)
elif len(sys.argv) > 4:
if sys.argv[2][:13] == "--workfolder=": # 'vsccm install --workfolder= {config_name}...'
install_configs_command(sys.argv[2][13:], 3)
elif sys.argv[2][:12] == "-workfolder=": # 'vsccm install -workfolder= {config_name}...'
install_configs_command(sys.argv[2][12:], 3)
else: # 'vsccm install {config_name}...'
install_configs_command(call_path, 2)
else: # 'vsccm install'
install_command(call_path)
else: # something went wrong
print("Uncorrect arguments")
# --------------------------------------------------------------