/
mpm.py
executable file
·303 lines (247 loc) · 12.8 KB
/
mpm.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
import os
from urllib.parse import urlparse
import json
from mpm_tools.Global import Global
from mpm_tools.SystemManager import SystemManager
from mpm_tools.NetworkManager import NetworkManager
from mpm_tools.FileManager import FileManager
from mpm_tools.Tools import Tools
from progress.bar import Bar
from pathlib import Path
#from tools.Responder import Responder
netMan = NetworkManager()
fileMan = FileManager()
gb = Global()
sysMan = SystemManager()
tools = Tools()
res = gb.getFundInfos("responses")
separator = "/"
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="mpm your full command line")
#parser.add_argument("cmd", type=str, metavar="create", help="Specify a command you want to execute")
parser.add_argument("-u", "--update",required=False, help="update your local mpm database")
parser.add_argument("-b", "--bored",required=False, help="uninstall mpm")
parser.add_argument("-d" , help="Specify the name of the directory you want to store the output of the command")
parser.add_argument("-v", "--version",required=False, help="see the installed version of mpm")
group = parser.add_mutually_exclusive_group()
group.add_argument("-c", "--create", help="create a complete project from the templates in our repositories")
group.add_argument("-i", "--install", help="install a plugin or a module")
group.add_argument("-o", "--clone", help="clone any zip file just using his url")
group.add_argument("-f", "--fileload", help="download any file and put it in the current directory just using his url")
#parser.add_argument("-up", "--upgrade",required=False, help="upgrade globally mpm and get the newest version")
args = parser.parse_args()
myOs = gb.getFundInfos("os")
if args.create or args.install or args.clone:
db = netMan.readFileFromUrl(gb.getFundInfos("url"))
if not isinstance(db, bool):
try:
fileMan.getSourceDir().createFile("package.mgx")
packages = fileMan.getSourceDir().getFileContent("package.mgx", "")
packages = {"projects": "", "modules": "", "cloned":"", "version": gb.getFundInfos("version")} if str(packages)=="" else json.loads(packages)
except Exception as e:
print("Something went wrong. Try to delete the packages.mgx file")
exit()
else:
print("problem to contact our database")
exit()
"""
errorContent = str(e)
errorSup = fileMan.setSrcDir().getFileContent("/error.log")
if not errorSup:
errorContent+=errorSup
fileMan.delFile("error.log")
fileMan.getSourceDir().createFile("error.log", e)
print(e)
"""
exit()
if args.create:
currentArg = args.create
"""
if args.create in packages["projects"]:
print(args.create, " already installed")
print(packages)
exit()
"""
if db:
db = json.loads(db)
try:
found = tools.in_json_array(currentArg, "name", db["projects"])
except Exception as e:
print("Something went wrong : problem to contact the database")
exit()
if(found[0]):
req = db["projects"][found[1]]
directory = currentArg if not args.d else args.d
print("creating of", currentArg, " project")
if req["git"]!="":
print("downloading and installing")
if netMan.gitClone(req["git"], directory):
print(res["process_completed"])
print("\n")
print("project created in the following directory :", directory)
packages["projects"]+=currentArg
packages["projects"]+=" "
with open(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', 'w') as outfile:
json.dump(packages, outfile)
fileMan.moveFile(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', directory+separator+"package.mgx")
elif req["zipurl"]!="":
fileName = tools.generateSessionId()+".zip"
print("downloading ")
try:
netMan.downloadFile(req["zipurl"], fileName)
print("installing")
fileMan.unzip(fileName, directory)
#loader = ProgressLoader(total=100)
#loader.progress(100)
print("\n")
fileMan.delFile(fileName)
print(currentArg, " project created in the following directory :", directory)
packages["projects"]+=currentArg
packages["projects"]+=" "
with open(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', 'w') as outfile:
json.dump(packages, outfile)
fileMan.moveFile(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', directory+separator+"package.mgx")
except Exception as e:
print("Something went wrong")
#print("this version is not yet compatible with the installation of this package try to upgrade mpm")
else:
print("problem to download required packages")
else:
print(currentArg, "not found in projects")
else:
print("problem to contact the database. Run mpm -u to solve the problem")
elif args.install:
currentArg = args.install
if db:
db = json.loads(db)
try:
found = tools.in_json_array(currentArg, "name", db["modules"])
except Exception as e:
print("Something went wrong : problem to contact the database")
exit()
if(found[0]):
req = db["modules"][found[1]]
directory = currentArg if not args.d else args.d
print("installing ", currentArg, " module")
if req["git"]!="":
print("downloading and installing")
if netMan.gitClone(req["git"], directory):
print(res["process_completed"])
print("\n")
print("Module installed")
packages["modules"]+=currentArg
packages["modules"]+=" "
with open(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', 'w') as outfile:
json.dump(packages, outfile)
fileMan.moveFile(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', directory+separator+"package.mgx")
elif req["zipurl"]!="":
fileName = tools.generateSessionId()+".zip"
print("downloading ")
try:
netMan.downloadFile(req["zipurl"], fileName)
print("installing")
fileMan.unzip(fileName, directory)
#loader = ProgressLoader(total=100)
#loader.progress(100)
print("\n")
fileMan.delFile(fileName)
print(args.install, " module installed")
packages["modules"]+=currentArg
packages["modules"]+=" "
with open(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', 'w') as outfile:
json.dump(packages, outfile)
fileMan.moveFile(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', directory+separator+"package.mgx")
except Exception as e:
print("Something went wrong")
#print("this version is not yet compatible with the installation of this package try to upgrade mpm")
else:
print("problem to download required packages")
else:
print(currentArg, "not found in modules")
else:
print("problem to contact the database. Run mpm -update to solve the problem")
elif args.clone:
currentArg = args.clone
fileName = tools.generateSessionId()+".zip"
directory = currentArg if not args.d else args.d
print("cloning ", currentArg)
try:
netMan.downloadFile(req["zipurl"], fileName)
print("installing")
fileMan.unzip(fileName, directory)
print("\n")
fileMan.delFile(fileName)
print(args.install, " cloned perfectly")
packages["cloned"]+=currentArg
packages["cloned"]+=" "
with open(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', 'w') as outfile:
json.dump(packages, outfile)
fileMan.moveFile(fileMan.getSourceDir().getCurrentSrcDir()+separator+'package.mgx', directory+separator+"package.mgx")
except Exception as e:
print("Something went wrong while cloning: the url miight be broken or your internet connection too slow")
elif args.fileload:
currentArg = args.fileload
directory = currentArg if not args.d else args.d
url = urlparse(currentArg)
fileName = os.path.basename(url.path)
print("cloning ", currentArg, "\n")
try:
netMan.downloadFile(currentArg, fileName)
print("file saved with the following name", fileName)
except Exception as e:
print("Something went wrong while cloning")
elif args.update:
print("updating mpm this will uninstall the old version before installing the new")
db = netMan.readFileFromUrl(gb.getFundInfos("url"))
if not isinstance(db, bool):
try:
db = json.loads(bytearray(str(db),"utf-8"))
if myOs == "windows":
print("download lastest mpm version by clicking on the following link", db[myOs])
else:
url = db[myOs]
#os.system("mpm -uninstall")
os.system("cd /tmp && wget "+db[myOs]+" && unzip mpm.zip && cd mpm/ && ./install.sh && cd ../ && sudo rm mpm.zip && sudo rm -r mpm")
except Exception as e:
print("Something went wrong while updating")
else:
print("Something went wrong while updating")
#print(e)
exit()
elif args.bored:
print("uninstalling mpm")
if myOs == "windows":
os.system("The uninstalling process is not still working on your mpm version. Go to the mpm installtion directory and run uninstall.exe file")
else:
os.system("sudo chmod +777 /usr/local/sbin/mpm_tools/uninstall.sh && cd /usr/local/sbin/mpm_tools && sudo ./uninstall.sh")
else:
print("command not found try mpm --help")
"""
elif args.update:
print("updating mpm this will uninstall the old version before installing the new")
db = netMan.readFileFromUrl(gb.getFundInfos("url"))
if not isinstance(db, bool):
try:
db = json.loads(bytearray(str(db),"utf-8"))
if myOs == "windows":
print("download last mpm version by clicking on the following link", db[myOs])
else:
url = db[myOs]
#os.system("mpm -uninstall")
os.system("cd /tmp && wget "+db[myOs]+" && unzip mpm.zip && cd mpm && ./install.sh && cd ../ && sudo rm mpm.zip && sudo rm -r mpm")
except Exception as e:
print("Something went wrong while updating")
else:
print("Something went wrong while updating")
#print(e)
exit()
elif args.uninstall:
print("uninstalling mpm")
if myOs == "windows":
os.system(fileMan.getSourceDir().getCurrentSrcDir()+"uninstall.exe")
else:
os.system("sudo chmod +777 /usr/local/sbin/mpm_tools/uninstall.sh && sudo ./usr/local/sbin/mpm_tools/uninstall.sh")
elif args.version:
print("the installed version of mpm is: ", gb.getFundInfos("version"))
"""