forked from unias/docklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmgr.py
executable file
·190 lines (177 loc) · 7.55 KB
/
sysmgr.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
import re, string, os
editableParms = ["ADMIN_EMAIL_ADDRESS","LOG_LEVEL"]
configPath = {"docklet": os.environ.get("DOCKLET_CONF")+"/docklet.conf",
"container": os.environ.get("DOCKLET_CONF")+"/container.conf"}
#configPath = {"docklet": "../conf/docklet.conf",
# "container": "../conf/container.conf"}
defaultPattern = re.compile(u'# *\S+ *= *\S+')
activePattern = re.compile(u'\S+ *= *\S+')
historyPattern = re.compile(u'## *\S+ *= *\S+')
def parse_line(line):
kind = ""
parm = ""
val = ""
if defaultPattern.match(line) != None and not "==" in line:
kind = "default"
elif activePattern.match(line) != None and not "#" in line:
kind = "active"
elif historyPattern.match(line) != None and not "==" in line:
kind = "history"
if kind != "":
line = line.replace("#", "").replace("\n", "")
parm = line[:line.find("=")].strip()
val = line[line.find("=")+1:].strip()
return [kind, parm, val]
class SystemManager():
def getParmList(*args, **kwargs):
#result = {"docklet": "", "container": ""}
result = {"docklet": "", "container": ""}
for field in ["docklet"]:
configFile = open(configPath[field])
lines = configFile.readlines()
configFile.close()
configFile = open(configPath[field])
wholeFile = configFile.read()
configFile.close()
conf = {}
segs = wholeFile.split("\n\n")
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if lineparm in editableParms:
editable = 1
else:
editable = 0
if linekind == "default":
conf[lineparm] = {"val": "novalidvaluea", "default": lineval,
"history": [], "editable": editable, "details": ""}
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "active":
try:
conf[lineparm]["val"] = lineval
except:
if lineparm in editableParms:
editable = 1
else:
editable = 0
conf[lineparm] = {"val": lineval, "default": lineval,
"history": [], "editable": editable, "details": ""}
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "history":
conf[lineparm]["history"].append(lineval)
for parm in conf.keys():
for seg in segs:
if parm in seg:
conf[parm]["details"] = seg
result[field] = [({'parm': parm, 'val': conf[parm]['val'],
'default': conf[parm]['default'], "history": conf[parm]['history'],
"editable": conf[parm]['editable'], "details": conf[parm]['details']}) for parm in sorted(conf.keys())]
configFile = open(configPath["container"])
wholeFile = configFile.read()
configFile.close()
result["container"] = wholeFile
return result
# 1. def and not act 2. act and not def 3. def and act
# have def and act and hist
def modify(self, field, parm, val):
configFile = open(configPath[field])
lines = configFile.readlines()
configFile.close()
finish = False
for i in range(0, len(lines)):
line = lines[i]
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "active" and lineparm == parm:
lines[i] = "## " + parm + "=" + lineval + "\n"
lines.insert(i, parm + "=" + val + "\n")
if i == 0 or not parm in lines[i-1] or not "=" in lines[i-1]:
lines.insert(i, "# " + parm + "=" + lineval + "\n")
finish = True
break
if finish == False:
for i in range(0, len(lines)):
line = lines[i]
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "default" and parm == lineparm:
lines.insert(i+1, parm + "=" + val + "\n")
break
for i in range(0, len(lines)):
line = lines[i]
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "history" and parm == lineparm and val == lineval:
lines.pop(i)
break
configFile = open(configPath[field], "w")
for line in lines:
configFile.write(line)
configFile.close()
os.environ[parm] = val
return [True, ""]
def clear(self, field, parm):
configFile = open(configPath[field])
lines = configFile.readlines()
configFile.close()
finish = False
for i in range(0, len(lines)):
line = lines[i]
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "history" and parm == lineparm:
lines[i] = ""
configFile = open(configPath[field], "w")
for line in lines:
configFile.write(line)
configFile.close()
return [True, ""]
def add(self, field, parm, val):
configFile = open(configPath[field], "a")
configFile.write("\n" + "# " + parm + "=" + val + "\n" + parm + "=" + val + "\n")
configFile.close()
return [True, ""]
def delete(self, field, parm):
configFile = open(configPath[field])
lines = configFile.readlines()
configFile.close()
for i in range(0, len(lines)):
line = lines[i]
if parm in line:
lines[i] = ""
configFile = open(configPath[field], "w")
for line in lines:
configFile.write(line)
configFile.close()
return [True, ""]
def reset_all(self, field):
configFile = open(configPath[field])
lines = configFile.readlines()
configFile.close()
conf = {}
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "default":
conf[lineparm] = {"val": lineval, "default": lineval, "history": []}
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "active":
try:
conf[lineparm]["val"] = lineval
except:
conf[lineparm] = {"val": lineval, "default": lineval, "history": []}
for line in lines:
[linekind, lineparm, lineval] = parse_line(line)
if linekind == "history":
conf[lineparm]["history"].append(lineval)
for i in range(0, len(lines)):
line = lines[i]
if activePattern.match(line) != None and not "#" in line:
segs = line.replace("\n", "").split("=")
lines[i] = segs[0].strip() + "=" + conf[segs[0].strip()]["default"] + "\n"
elif historyPattern.match(line) != None and not "==" in line:
lines[i] = ""
configFile = open(configPath[field], "w")
for line in lines:
configFile.write(line)
configFile.close()
return [True, ""]
#sysmgr = SystemManager()
#print(sysmgr.getParmList())