-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduleHelper.py
94 lines (79 loc) · 2.7 KB
/
moduleHelper.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
import json
import os
import shutil
import urllib.request
from zipfile import ZipFile
import config
import storage
import platform
modulesPath = "./modules/"
moduleInfoFileName = "info.json"
def getInstallableModules():
return storage.getInstallableModules()
def getInstallableModuleByName(name):
installableModules = getInstallableModules()
for moduleName in installableModules:
if moduleName.lower() == name.lower():
return installableModules[moduleName]
return {"version": "Na", "name": "Na"}
def getInstalledModules():
if not os.path.isdir(modulesPath):
os.makedirs(modulesPath)
items = os.listdir(modulesPath)
modules = {}
for item in items:
path = modulesPath + item
if os.path.isdir(path):
moduleInfoPath = path + "/" + moduleInfoFileName
if os.path.isfile(moduleInfoPath):
jsonData = json.load(open(moduleInfoPath, 'r'))
version = jsonData["version"]
name = jsonData["name"]
modules[name] = {"version": version, "name": name, "link": "na"}
return modules
def getInstalledModuleByName(name):
installedModules = getInstalledModules()
for moduleName in installedModules:
if moduleName.lower() == name.lower():
return installedModules[moduleName]
return {"version": "Na", "name": "Na", "link": "na"}
def getAllModules():
installed = getInstalledModules()
web = getInstallableModules()
allModules = {}
for m in installed:
if m not in allModules:
allModules[m] = installed[m]
for m in web:
if m not in allModules:
allModules[m] = web[m]
return allModules
def downloadModule(name):
module = getInstallableModuleByName(name)
link = module["link"]
path = modulesPath + name
downloadPath = modulesPath + "tmp"
if not os.path.isdir(path):
os.makedirs(path)
if not os.path.isdir(downloadPath):
os.makedirs(downloadPath)
zipPath = downloadPath + "/" + name + ".zip"
urllib.request.urlretrieve(link, zipPath)
with ZipFile(zipPath, 'r') as zipFile:
listOfFileNames = zipFile.namelist()
unzippedPath = downloadPath + "/unzipped"
zipFile.extractall(unzippedPath)
items = os.listdir(unzippedPath)
shutil.rmtree(path)
shutil.move(unzippedPath + "/" + items[0], path)
shutil.rmtree(downloadPath)
def installModule(name):
downloadModule(name)
fileAdd = ""
if "win" in platform.system().lower():
fileAdd = ".bat"
else:
fileAdd = ".sh"
runFilePath = modulesPath + name + "/install" + fileAdd
if os.path.isfile(runFilePath):
os.system(runFilePath)