1 | import json |
---|
2 | import os.path |
---|
3 | import os |
---|
4 | import time |
---|
5 | import xmlrpclib |
---|
6 | import socket |
---|
7 | import netifaces |
---|
8 | import re |
---|
9 | import importlib |
---|
10 | import sys |
---|
11 | import tarfile |
---|
12 | import threading |
---|
13 | |
---|
14 | class VariablesManager: |
---|
15 | |
---|
16 | VARIABLES_FILE="/var/lib/n4d/variables" |
---|
17 | VARIABLES_DIR="/var/lib/n4d/variables-dir/" |
---|
18 | LOCK_FILE="/tmp/.llxvarlock" |
---|
19 | INBOX="/var/lib/n4d/variables-inbox/" |
---|
20 | TRASH="/var/lib/n4d/variables-trash/" |
---|
21 | CUSTOM_INSTALLATION_DIR="/usr/share/n4d/variablesmanager-funcs/" |
---|
22 | LOG="/var/log/n4d/variables-manager" |
---|
23 | |
---|
24 | def __init__(self): |
---|
25 | |
---|
26 | self.variables={} |
---|
27 | self.variables_ok=False |
---|
28 | if os.path.exists(VariablesManager.LOCK_FILE): |
---|
29 | os.remove(VariablesManager.LOCK_FILE) |
---|
30 | |
---|
31 | |
---|
32 | if os.path.exists(VariablesManager.VARIABLES_FILE): |
---|
33 | self.variables_ok,ret=self.load_json(VariablesManager.VARIABLES_FILE) |
---|
34 | try: |
---|
35 | os.remove(VariablesManager.VARIABLES_FILE) |
---|
36 | except: |
---|
37 | pass |
---|
38 | else: |
---|
39 | self.variables_ok,ret=self.load_json(None) |
---|
40 | |
---|
41 | if self.variables_ok: |
---|
42 | #print "\nVARIABLES FILE" |
---|
43 | #print "==============================" |
---|
44 | #self.listvars() |
---|
45 | self.read_inbox(False) |
---|
46 | #print "\nAFTER INBOX" |
---|
47 | #print "==============================" |
---|
48 | #print self.listvars(True) |
---|
49 | self.empty_trash(False) |
---|
50 | #print "\nAFTER TRASH" |
---|
51 | #print "==============================" |
---|
52 | #print self.listvars(True) |
---|
53 | self.add_volatile_info() |
---|
54 | self.write_file() |
---|
55 | else: |
---|
56 | print("[VariablesManager] Loading variables failed because: " + str(ret)) |
---|
57 | |
---|
58 | |
---|
59 | #def __init__ |
---|
60 | |
---|
61 | # DO NOT TOUCH THIS |
---|
62 | |
---|
63 | def startup(self,options): |
---|
64 | pass |
---|
65 | |
---|
66 | # DONE ============ |
---|
67 | |
---|
68 | |
---|
69 | def backup(self,dir="/backup"): |
---|
70 | |
---|
71 | try: |
---|
72 | |
---|
73 | #file_path=dir+"/"+self.get_time()+"_VariablesManager.tar.gz" |
---|
74 | file_path=dir+"/"+get_backup_name("VariablesManager") |
---|
75 | |
---|
76 | tar=tarfile.open(file_path,"w:gz") |
---|
77 | |
---|
78 | tar.add(VariablesManager.VARIABLES_DIR) |
---|
79 | |
---|
80 | tar.close() |
---|
81 | |
---|
82 | return [True,file_path] |
---|
83 | |
---|
84 | except Exception as e: |
---|
85 | return [False,str(e)] |
---|
86 | |
---|
87 | #def backup |
---|
88 | |
---|
89 | def restore(self,file_path=None): |
---|
90 | |
---|
91 | |
---|
92 | if file_path==None: |
---|
93 | for f in sorted(os.listdir("/backup"),reverse=True): |
---|
94 | if "VariablesManager" in f: |
---|
95 | file_path="/backup/"+f |
---|
96 | break |
---|
97 | |
---|
98 | try: |
---|
99 | |
---|
100 | if os.path.exists(file_path): |
---|
101 | |
---|
102 | tmp_dir=tempfile.mkdtemp() |
---|
103 | tar=tarfile.open(file_path) |
---|
104 | tar.extractall(tmp_dir) |
---|
105 | tar.close() |
---|
106 | |
---|
107 | if not os.path.exists(VariablesManager.VARIABLES_DIR): |
---|
108 | os.mkdir(VariablesManager.VARIABLES_DIR) |
---|
109 | |
---|
110 | for f in os.listdir(tmp_dir+VariablesManager.VARIABLES_DIR): |
---|
111 | tmp_path=tmp_dir+VariablesManager.VARIABLES_DIR+f |
---|
112 | shutil.copy(tmp_path,VariablesManager.VARIABLES_DIR) |
---|
113 | |
---|
114 | self.load_json(None) |
---|
115 | |
---|
116 | return [True,""] |
---|
117 | |
---|
118 | except Exception as e: |
---|
119 | |
---|
120 | return [False,str(e)] |
---|
121 | |
---|
122 | #def restore |
---|
123 | |
---|
124 | def log(self,txt): |
---|
125 | |
---|
126 | try: |
---|
127 | f=open(VariablesManager.LOG,"a") |
---|
128 | txt=str(txt) |
---|
129 | f.write(txt+"\n") |
---|
130 | f.close() |
---|
131 | except Exception as e: |
---|
132 | pass |
---|
133 | |
---|
134 | #def log |
---|
135 | |
---|
136 | def listvars(self,extra_info=False,custom_dic=None): |
---|
137 | ret="" |
---|
138 | |
---|
139 | try: |
---|
140 | |
---|
141 | if custom_dic==None: |
---|
142 | custom_dic=self.variables |
---|
143 | for variable in custom_dic: |
---|
144 | if type(custom_dic[variable])==type({}) and "root_protected" in custom_dic[variable] and custom_dic[variable]["root_protected"]: |
---|
145 | continue |
---|
146 | value=self.get_variable(variable) |
---|
147 | if value==None: |
---|
148 | continue |
---|
149 | ret+=variable+ "='" + str(value).encode("utf-8") + "';\n" |
---|
150 | if extra_info: |
---|
151 | ret+= "\tDescription: " + self.variables[variable][u"description"] + "\n" |
---|
152 | ret+="\tUsed by:\n" |
---|
153 | for depend in self.variables[variable][u"packages"]: |
---|
154 | ret+= "\t\t" + depend.encode("utf-8") + "\n" |
---|
155 | |
---|
156 | return ret.strip("\n") |
---|
157 | except Exception as e: |
---|
158 | return str(e) |
---|
159 | |
---|
160 | #def listvars |
---|
161 | |
---|
162 | def calculate_variable(self,value): |
---|
163 | pattern="_@START@_.*?_@END@_" |
---|
164 | variables=[] |
---|
165 | |
---|
166 | ret=re.findall(pattern,value) |
---|
167 | |
---|
168 | print "hola",ret |
---|
169 | |
---|
170 | for item in ret: |
---|
171 | tmp=item.replace("_@START@_","") |
---|
172 | tmp=tmp.replace("_@END@_","") |
---|
173 | variables.append(tmp) |
---|
174 | |
---|
175 | for var in variables: |
---|
176 | value=value.replace("_@START@_"+var+"_@END@_",self.get_variable(var)) |
---|
177 | |
---|
178 | return value |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | #def remove_calculated_chars |
---|
183 | |
---|
184 | def add_volatile_info(self): |
---|
185 | |
---|
186 | for item in self.variables: |
---|
187 | |
---|
188 | if not self.variables[item].has_key("volatile"): |
---|
189 | self.variables[item]["volatile"]=False |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | #def add_volatile_info |
---|
194 | |
---|
195 | |
---|
196 | def showvars(self,var_list,extra_info=False): |
---|
197 | |
---|
198 | ret="" |
---|
199 | |
---|
200 | for var in var_list: |
---|
201 | ret+=var+"='" |
---|
202 | if self.variables.has_key(var): |
---|
203 | try: |
---|
204 | ret+=self.variables[var][u'value'].encode("utf-8")+"';\n" |
---|
205 | except Exception as e: |
---|
206 | #it's probably something old showvars couldn't have stored anyway |
---|
207 | ret+="';\n" |
---|
208 | if extra_info: |
---|
209 | ret+= "\tDescription: " + self.variables[var][u"description"] + "\n" |
---|
210 | ret+="\tUsed by:\n" |
---|
211 | for depend in self.variables[var][u"packages"]: |
---|
212 | ret+= "\t\t" + depend.encode("utf-8") + "\n" |
---|
213 | else: |
---|
214 | ret+="'\n" |
---|
215 | |
---|
216 | return ret.strip("\n") |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | #def showvars |
---|
221 | |
---|
222 | def get_variables(self): |
---|
223 | |
---|
224 | return self.variables |
---|
225 | |
---|
226 | #def get_variables |
---|
227 | |
---|
228 | |
---|
229 | def load_json(self, file=None): |
---|
230 | |
---|
231 | self.variables={} |
---|
232 | |
---|
233 | if file!=None: |
---|
234 | |
---|
235 | try: |
---|
236 | |
---|
237 | f=open(file,"r") |
---|
238 | data=json.load(f) |
---|
239 | f.close() |
---|
240 | self.variables=data |
---|
241 | |
---|
242 | #return [True,""] |
---|
243 | |
---|
244 | except Exception as e: |
---|
245 | print(str(e)) |
---|
246 | #return [False,e.message] |
---|
247 | |
---|
248 | for file in os.listdir(VariablesManager.VARIABLES_DIR): |
---|
249 | try: |
---|
250 | sys.stdout.write("\t[VariablesManager] Loading " + file + " ... ") |
---|
251 | f=open(VariablesManager.VARIABLES_DIR+file) |
---|
252 | data=json.load(f) |
---|
253 | f.close() |
---|
254 | self.variables[file]=data[file] |
---|
255 | print("OK") |
---|
256 | except Exception as e: |
---|
257 | print("FAILED ["+str(e)+"]") |
---|
258 | |
---|
259 | return [True,""] |
---|
260 | |
---|
261 | #def load_json |
---|
262 | |
---|
263 | def read_inbox(self, force_write=False): |
---|
264 | |
---|
265 | ''' |
---|
266 | value |
---|
267 | function |
---|
268 | description |
---|
269 | packages |
---|
270 | ''' |
---|
271 | |
---|
272 | if self.variables_ok: |
---|
273 | |
---|
274 | if os.path.exists(VariablesManager.INBOX): |
---|
275 | |
---|
276 | for file in os.listdir(VariablesManager.INBOX): |
---|
277 | file_path=VariablesManager.INBOX+file |
---|
278 | print "[VariablesManager] Adding " + file_path + " info..." |
---|
279 | try: |
---|
280 | f=open(file_path,"r") |
---|
281 | data=json.load(f) |
---|
282 | f.close() |
---|
283 | |
---|
284 | for item in data: |
---|
285 | if self.variables.has_key(item): |
---|
286 | for key in data[item].keys(): |
---|
287 | if not self.variables[item].has_key(unicode(key)): |
---|
288 | self.variables[item][unicode(key)] = data[item][key] |
---|
289 | if data[item].has_key(unicode('function')): |
---|
290 | self.variables[item][unicode('function')] = data[item][u'function'] |
---|
291 | for depend in data[item][u'packages']: |
---|
292 | if depend not in self.variables[item][u'packages']: |
---|
293 | self.variables[item][u'packages'].append(depend) |
---|
294 | #if self.variables[item][u'value']==None or self.variables[item][u'value']=="": |
---|
295 | # self.variables[item][u'value']=data[item][u'values'] |
---|
296 | else: |
---|
297 | self.variables[item]=data[item] |
---|
298 | |
---|
299 | |
---|
300 | except Exception as e: |
---|
301 | print e |
---|
302 | #return [False,e.message] |
---|
303 | os.remove(file_path) |
---|
304 | |
---|
305 | if force_write: |
---|
306 | try: |
---|
307 | self.add_volatile_info() |
---|
308 | self.write_file() |
---|
309 | except Exception as e: |
---|
310 | print(e) |
---|
311 | |
---|
312 | |
---|
313 | return [True,""] |
---|
314 | |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | ''' |
---|
319 | |
---|
320 | if os.path.exists(VariablesManager.INBOX): |
---|
321 | |
---|
322 | for file in os.listdir(VariablesManager.INBOX): |
---|
323 | file_path=VariablesManager.INBOX+file |
---|
324 | |
---|
325 | try: |
---|
326 | execfile(file_path) |
---|
327 | os.remove(file_path) |
---|
328 | except Exception as e: |
---|
329 | self.log(file_path + ": " + str(e)) |
---|
330 | |
---|
331 | ''' |
---|
332 | |
---|
333 | #def read_inbox |
---|
334 | |
---|
335 | def empty_trash(self,force_write=False): |
---|
336 | |
---|
337 | |
---|
338 | if self.variables_ok: |
---|
339 | |
---|
340 | for file in os.listdir(VariablesManager.TRASH): |
---|
341 | file_path=VariablesManager.TRASH+file |
---|
342 | #print "[VariablesManager] Removing " + file_path + " info..." |
---|
343 | try: |
---|
344 | f=open(file_path,"r") |
---|
345 | data=json.load(f) |
---|
346 | f.close() |
---|
347 | |
---|
348 | for item in data: |
---|
349 | if self.variables.has_key(item): |
---|
350 | if data[item][u'packages'][0] in self.variables[item][u'packages']: |
---|
351 | count=0 |
---|
352 | for depend in self.variables[item][u'packages']: |
---|
353 | if depend==data[item][u'packages'][0]: |
---|
354 | self.variables[item][u'packages'].pop(count) |
---|
355 | if len(self.variables[item][u'packages'])==0: |
---|
356 | self.variables.pop(item) |
---|
357 | break |
---|
358 | else: |
---|
359 | count+=1 |
---|
360 | |
---|
361 | #os.remove(file_path) |
---|
362 | |
---|
363 | |
---|
364 | except Exception as e: |
---|
365 | print e |
---|
366 | pass |
---|
367 | #return [False,e.message] |
---|
368 | os.remove(file_path) |
---|
369 | |
---|
370 | if force_write: |
---|
371 | try: |
---|
372 | self.write_file() |
---|
373 | except Exception as e: |
---|
374 | print(e) |
---|
375 | |
---|
376 | return [True,''] |
---|
377 | |
---|
378 | |
---|
379 | #def empty_trash |
---|
380 | |
---|
381 | def get_ip(self): |
---|
382 | |
---|
383 | for item in netifaces.interfaces(): |
---|
384 | tmp=netifaces.ifaddresses(item) |
---|
385 | if tmp.has_key(netifaces.AF_INET): |
---|
386 | if tmp[netifaces.AF_INET][0].has_key("broadcast") and tmp[netifaces.AF_INET][0]["broadcast"]=="10.0.2.255": |
---|
387 | return tmp[netifaces.AF_INET][0]["addr"] |
---|
388 | return None |
---|
389 | |
---|
390 | #def get_ip |
---|
391 | |
---|
392 | def get_variable_list(self,variable_list,store=False,full_info=False): |
---|
393 | |
---|
394 | ret={} |
---|
395 | if variable_list!=None: |
---|
396 | for item in variable_list: |
---|
397 | try: |
---|
398 | ret[item]=self.get_variable(item,store,full_info) |
---|
399 | #if ret[item]==None: |
---|
400 | # ret[item]="" |
---|
401 | except Exception as e: |
---|
402 | print e |
---|
403 | |
---|
404 | return ret |
---|
405 | |
---|
406 | #def get_variable_list |
---|
407 | |
---|
408 | |
---|
409 | def get_variable(self,name,store=False,full_info=False,key=None): |
---|
410 | |
---|
411 | global master_password |
---|
412 | |
---|
413 | if name in self.variables and self.variables[name].has_key("root_protected") and self.variables[name]["root_protected"] and key!=master_password: |
---|
414 | return None |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | if name in self.variables and self.variables[name].has_key("function"): |
---|
419 | try: |
---|
420 | if not full_info: |
---|
421 | if (type(self.variables[name][u"value"])==type("") or type(self.variables[name][u"value"])==type(u"")) and self.variables[name][u"value"].find("_@START@_")!=-1: |
---|
422 | #print "I have to ask for " + name + " which has value: " + self.variables[name][u'value'] |
---|
423 | value=self.calculate_variable(self.variables[name][u"value"]) |
---|
424 | else: |
---|
425 | value=self.variables[name][u"value"] |
---|
426 | #return str(value.encode("utf-8") |
---|
427 | if type(value)==type(u""): |
---|
428 | try: |
---|
429 | ret=value.encode("utf-8") |
---|
430 | return ret |
---|
431 | except: |
---|
432 | return value |
---|
433 | else: |
---|
434 | return value |
---|
435 | else: |
---|
436 | variable=self.variables[name].copy() |
---|
437 | variable["remote"]=False |
---|
438 | if type(variable[u"value"])==type(""): |
---|
439 | if variable[u"value"].find("_@START@_")!=-1: |
---|
440 | variable["original_value"]=variable[u"value"] |
---|
441 | variable[u"value"]=self.calculate_variable(self.variables[name][u"value"]) |
---|
442 | variable["calculated"]=True |
---|
443 | return variable |
---|
444 | except: |
---|
445 | return None |
---|
446 | else: |
---|
447 | if self.variables.has_key("REMOTE_VARIABLES_SERVER") and self.variables["REMOTE_VARIABLES_SERVER"][u"value"]!="" and self.variables["REMOTE_VARIABLES_SERVER"][u"value"]!=None: |
---|
448 | try: |
---|
449 | server_ip=socket.gethostbyname(self.variables["REMOTE_VARIABLES_SERVER"][u"value"]) |
---|
450 | except: |
---|
451 | return None |
---|
452 | if self.get_ip()!=server_ip: |
---|
453 | for count in range(0,3): |
---|
454 | try: |
---|
455 | |
---|
456 | server=xmlrpclib.ServerProxy("https://"+server_ip+":9779",allow_none=True) |
---|
457 | var=server.get_variable("","VariablesManager",name,True,True) |
---|
458 | |
---|
459 | if var==None: |
---|
460 | return None |
---|
461 | if (var!="" or type(var)!=type("")) and store: |
---|
462 | |
---|
463 | self.add_variable(name,var[u"value"],var[u"function"],var[u"description"],var[u"packages"],False) |
---|
464 | return self.get_variable(name,store,full_info) |
---|
465 | else: |
---|
466 | if full_info: |
---|
467 | var["remote"]=True |
---|
468 | return var |
---|
469 | else: |
---|
470 | return var["value"] |
---|
471 | |
---|
472 | except Exception as e: |
---|
473 | time.sleep(1) |
---|
474 | |
---|
475 | return None |
---|
476 | else: |
---|
477 | return None |
---|
478 | else: |
---|
479 | |
---|
480 | return None |
---|
481 | |
---|
482 | #def get_variable |
---|
483 | |
---|
484 | |
---|
485 | def set_variable(self,name,value,depends=[],force_volatile_flag=False): |
---|
486 | |
---|
487 | if name in self.variables: |
---|
488 | if type(value)==type(""): |
---|
489 | self.variables[name][u"value"]=unicode(value).encode("utf-8") |
---|
490 | else: |
---|
491 | self.variables[name][u"value"]=value |
---|
492 | |
---|
493 | if len(depends)>0: |
---|
494 | for depend in depends: |
---|
495 | self.variables[unicode(name).encode("utf-8")][u"packages"].append(depend) |
---|
496 | |
---|
497 | if not force_volatile_flag: |
---|
498 | self.write_file() |
---|
499 | else: |
---|
500 | |
---|
501 | self.variables[name]["volatile"]=True |
---|
502 | if "function" not in self.variables["name"]: |
---|
503 | self.variables[name]["function"]="" |
---|
504 | if "description" not in self.variables["name"]: |
---|
505 | self.variables[name]["description"]="" |
---|
506 | |
---|
507 | return [True,""] |
---|
508 | else: |
---|
509 | return [False,"Variable not found. Use add_variable"] |
---|
510 | |
---|
511 | |
---|
512 | #def set_variable |
---|
513 | |
---|
514 | def add_variable(self,name,value,function,description,depends,volatile=False,root_protected=False): |
---|
515 | |
---|
516 | if name not in self.variables: |
---|
517 | dic={} |
---|
518 | if type(value)==type(""): |
---|
519 | dic[u"value"]=unicode(value).encode("utf-8") |
---|
520 | else: |
---|
521 | dic[u"value"]=value |
---|
522 | dic[u"function"]=function |
---|
523 | dic[u"description"]=unicode(description).encode("utf-8") |
---|
524 | if type(depends)==type(""): |
---|
525 | dic[u"packages"]=[unicode(depends).encode("utf-8")] |
---|
526 | elif type(depends)==type([]): |
---|
527 | dic[u"packages"]=depends |
---|
528 | else: |
---|
529 | dic[u"packages"]=[] |
---|
530 | dic["volatile"]=volatile |
---|
531 | dic["root_protected"]=root_protected |
---|
532 | self.variables[unicode(name)]=dic |
---|
533 | if not volatile: |
---|
534 | self.write_file() |
---|
535 | return [True,""] |
---|
536 | else: |
---|
537 | return [False,"Variable already exists. Use set_variable"] |
---|
538 | |
---|
539 | def write_file(self,fname=None): |
---|
540 | |
---|
541 | ''' |
---|
542 | |
---|
543 | try: |
---|
544 | while os.path.exists(VariablesManager.LOCK_FILE): |
---|
545 | time.sleep(2) |
---|
546 | f=open(VariablesManager.LOCK_FILE,"w") |
---|
547 | f.close() |
---|
548 | tmp={} |
---|
549 | for item in self.variables: |
---|
550 | if self.variables[item].has_key("volatile") and self.variables[item]["volatile"]==False: |
---|
551 | tmp[item]=self.variables[item] |
---|
552 | |
---|
553 | if fname==None: |
---|
554 | f=open(VariablesManager.VARIABLES_FILE,"w") |
---|
555 | else: |
---|
556 | f=open(fname,"w") |
---|
557 | |
---|
558 | data=unicode(json.dumps(tmp,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
559 | f.write(data) |
---|
560 | f.close() |
---|
561 | os.remove(VariablesManager.LOCK_FILE) |
---|
562 | return True |
---|
563 | |
---|
564 | except Exception as e: |
---|
565 | os.remove(VariablesManager.LOCK_FILE) |
---|
566 | print(e) |
---|
567 | return False |
---|
568 | |
---|
569 | ''' |
---|
570 | |
---|
571 | try: |
---|
572 | while os.path.exists(VariablesManager.LOCK_FILE): |
---|
573 | time.sleep(2) |
---|
574 | |
---|
575 | f=open(VariablesManager.LOCK_FILE,"w") |
---|
576 | f.close() |
---|
577 | tmp_vars={} |
---|
578 | for item in self.variables: |
---|
579 | if self.variables[item].has_key("volatile") and self.variables[item]["volatile"]==False: |
---|
580 | tmp_vars[item]=self.variables[item] |
---|
581 | |
---|
582 | for item in tmp_vars: |
---|
583 | |
---|
584 | tmp={} |
---|
585 | tmp[item]=tmp_vars[item] |
---|
586 | f=open(VariablesManager.VARIABLES_DIR+item,"w") |
---|
587 | data=unicode(json.dumps(tmp,indent=4,encoding="utf-8",ensure_ascii=False)).encode("utf-8") |
---|
588 | f.write(data) |
---|
589 | f.close() |
---|
590 | |
---|
591 | if "root_protected" in tmp_vars[item]: |
---|
592 | if tmp_vars[item]["root_protected"]: |
---|
593 | self.chmod(VariablesManager.VARIABLES_DIR+item,0600) |
---|
594 | |
---|
595 | |
---|
596 | os.remove(VariablesManager.LOCK_FILE) |
---|
597 | return True |
---|
598 | |
---|
599 | |
---|
600 | except Exception as e: |
---|
601 | os.remove(VariablesManager.LOCK_FILE) |
---|
602 | print (e) |
---|
603 | return False |
---|
604 | |
---|
605 | |
---|
606 | |
---|
607 | #def write_file |
---|
608 | |
---|
609 | def chmod(self,file,mode): |
---|
610 | prevmask = os.umask(0) |
---|
611 | try: |
---|
612 | os.chmod(file,mode) |
---|
613 | os.umask(prevmask) |
---|
614 | return True |
---|
615 | except Exception as e: |
---|
616 | print e |
---|
617 | os.umask(prevmask) |
---|
618 | return False |
---|
619 | #def chmod |
---|
620 | |
---|
621 | def init_variable(self,variable,args={},force=False,full_info=False): |
---|
622 | |
---|
623 | try: |
---|
624 | funct=self.variables[variable]["function"] |
---|
625 | mod_name=funct[:funct.rfind(".")] |
---|
626 | funct_name=funct[funct.rfind(".")+1:] |
---|
627 | funct_name=funct_name.replace("(","") |
---|
628 | funct_name=funct_name.replace(")","") |
---|
629 | mod=importlib.import_module(mod_name) |
---|
630 | ret=getattr(mod,funct_name)(args) |
---|
631 | ok,exc=self.set_variable(variable,ret) |
---|
632 | if ok: |
---|
633 | return (True,ret) |
---|
634 | else: |
---|
635 | return (False,ret) |
---|
636 | except Exception as e: |
---|
637 | return (False,e) |
---|
638 | |
---|
639 | |
---|
640 | #def init_variable |
---|
641 | |
---|
642 | |
---|
643 | #class VariablesManager |
---|
644 | |
---|
645 | |
---|
646 | if __name__=="__main__": |
---|
647 | |
---|
648 | vm=VariablesManager() |
---|
649 | |
---|
650 | print vm.listvars() |
---|
651 | print vm.init_variable("name_center") |
---|
652 | args={} |
---|
653 | args["iface"]="eth0" |
---|
654 | print vm.init_variable("SERVER_IP",args) |
---|
655 | print vm.write_file() |
---|
656 | #print vm.get_variable("VARIABLE2",full_info=True) |
---|
657 | #print vm.get_variable("VARIABLE3",full_info=True) |
---|
658 | #print vm.showvars(var_list) |
---|
659 | |
---|
660 | |
---|
661 | |
---|
662 | |
---|
663 | |
---|
664 | |
---|
665 | |
---|
666 | |
---|