10 from StringIO import StringIO
11 from optparse import OptionParser
13 from sfa.util.version import version_tag
14 from sfa.util.config import Config
16 def validator(validated_variables):
18 # maint_user = validated_variables["PLC_API_MAINTENANCE_USER"]
19 # root_user = validated_variables["PLC_ROOT_USER"]
20 # if maint_user == root_user:
21 # errStr="PLC_API_MAINTENANCE_USER=%s cannot be the same as PLC_ROOT_USER=%s"%(maint_user,root_user)
22 # raise plc_config.ConfigurationException(errStr)
25 "SFA_GENERIC_FLAVOUR",
27 "SFA_REGISTRY_ROOT_AUTH",
40 'usual_variables':usual_variables,
41 'config_dir':"/etc/sfa",
42 'validate_variables':{},
43 'validator':validator,
54 def noop_validator(validated_variables):
57 # historically we could also configure the devel pkg....
58 def init_configuration ():
59 global g_configuration
60 global usual_variables, config_dir, service
62 usual_variables=g_configuration["usual_variables"]
63 config_dir=g_configuration["config_dir"]
64 service=g_configuration["service"]
66 global def_default_config, def_site_config, def_consolidated_config
67 def_default_config= "%s/default_config.xml" % config_dir
68 def_site_config = "%s/configs/site_config" % config_dir
69 def_consolidated_config = "%s/%s_config" % (config_dir, service)
72 mainloop_usage= """Available commands:
73 Uppercase versions give variables comments, when available
74 u/U\t\t\tEdit usual variables
76 r\t\t\tRestart %(service)s service
77 R\t\t\tReload %(service)s service (rebuild config files for sh, python....)
78 q\t\t\tQuit (without saving)
81 l/L [<cat>|<var>]\tShow Locally modified variables/values
82 s/S [<cat>|<var>]\tShow variables/values (all, in category, single)
83 e/E [<cat>|<var>]\tEdit variables (all, in category, single)
85 c\t\t\tList categories
86 v/V [<cat>|<var>]\tList Variables (all, in category, single)
88 Typical usage involves: u, [l,] w, r, q
92 command_usage="%prog [options] [default-xml [site-xml [consolidated-xml]]]"
95 \t default-xml defaults to %s
96 \t site-xml defaults to %s
97 \t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config)
101 variable_usage= """Edit Commands :
102 #\tShow variable comments
103 .\tStops prompting, return to mainloop
104 /\tCleans any site-defined value, reverts to default
105 =\tShows default value
106 >\tSkips to next category
111 def get_value (config, category_id, variable_id):
112 value = config.get (category_id, variable_id)
115 def get_type (config, category_id, variable_id):
116 value = config.get (category_id, variable_id)
117 #return variable['type']
120 def get_current_value (cread, cwrite, category_id, variable_id):
121 # the value stored in cwrite, if present, is the one we want
123 result=get_value (cwrite,category_id,variable_id)
125 result=get_value (cread,category_id,variable_id)
128 # refrain from using plc_config's _sanitize
129 def get_varname (config, category_id, variable_id):
130 varname = category_id +"_"+ variable_id
131 config.locate_varname(varname)
134 # could not avoid using _sanitize here..
135 def get_name_comments (config, cid, vid):
137 (category, variable) = config.get (cid, vid)
138 (id, name, value, comments) = config._sanitize_variable (cid,variable)
139 return (name,comments)
143 def print_name_comments (config, cid, vid):
144 (name,comments)=get_name_comments(config,cid,vid)
146 print "### %s" % name
148 for line in comments:
151 print "!!! No comment associated to %s_%s" % (cid,vid)
154 def list_categories (config):
156 for section in config.sections():
160 def print_categories (config):
161 print "Known categories"
162 for cid in list_categories(config):
163 print "%s" % (cid.upper())
166 def list_category (config, cid):
168 for section in config.sections():
169 if section == cid.lower():
170 for (name,value) in config.items(section):
171 result += ["%s_%s" %(cid,name)]
174 def print_category (config, cid, show_comments=True):
177 vids=list_category(config,cid)
179 print "%s : no such category"%CID
181 print "Category %s contains" %(CID)
186 def consolidate (default_config, site_config, consolidated_config):
189 conso = Config(default_config)
190 conso.load (site_config)
191 conso.save (consolidated_config)
192 except Exception, inst:
193 print "Could not consolidate, %s" % (str(inst))
195 print ("Merged\n\t%s\nand\t%s\ninto\t%s"%(default_config,site_config,
196 consolidated_config))
198 def reload_service ():
200 os.system("set -x ; service %s reload" % service)
203 def restart_service ():
205 print ("==================== Stopping %s" % service)
206 os.system("service %s stop" % service)
207 print ("==================== Starting %s" % service)
208 os.system("service %s start" % service)
211 def prompt_variable (cdef, cread, cwrite, category, variable,
212 show_comments, support_next=False):
215 category_id = category
216 variable_id = variable
219 default_value = get_value(cdef,category_id,variable_id)
220 variable_type = get_type(cdef,category_id,variable_id)
221 current_value = get_current_value(cread,cwrite,category_id, variable_id)
222 varname = get_varname (cread,category_id, variable_id)
225 print_name_comments (cdef, category_id, variable_id)
226 prompt = "== %s : [%s] " % (varname,current_value)
228 answer = raw_input(prompt).strip()
230 raise Exception ('BailOut')
231 except KeyboardInterrupt:
233 raise Exception ('BailOut')
236 if (answer == "") or (answer == current_value):
238 elif (answer == "."):
239 raise Exception ('BailOut')
240 elif (answer == "#"):
241 print_name_comments(cread,category_id,variable_id)
242 elif (answer == "?"):
243 print variable_usage.strip()
244 elif (answer == "="):
245 print ("%s defaults to %s" %(varname,default_value))
246 # revert to default : remove from cwrite (i.e. site-config)
247 elif (answer == "/"):
248 cwrite.delete(category_id,variable_id)
249 print ("%s reverted to %s" %(varname,default_value))
251 elif (answer == ">"):
253 raise Exception ('NextCategory')
255 print "No support for next category"
257 if cdef.validate_type(variable_type, answer):
258 cwrite.set(category_id, variable_id, answer)
261 print "Not a valid value"
263 def prompt_variables_all (cdef, cread, cwrite, show_comments):
265 for (category_id, (category, variables)) in cread.variables().iteritems():
266 print ("========== Category = %s" % category_id.upper())
267 for variable in variables.values():
269 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
271 except Exception, inst:
272 if (str(inst) == 'NextCategory'): break
275 except Exception, inst:
276 if (str(inst) == 'BailOut'): return
279 def prompt_variables_category (cdef, cread, cwrite, cid, show_comments):
283 print ("========== Category = %s" % CID)
284 for vid in list_category(cdef,cid):
285 (category,variable) = cdef.locate_varname(vid.upper())
286 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
287 show_comments, False)
288 except Exception, inst:
289 if (str(inst) == 'BailOut'): return
293 def show_variable (cdef, cread, cwrite,
294 category, variable,show_value,show_comments):
295 assert category.has_key('id')
296 assert variable.has_key('id')
298 category_id = category ['id']
299 variable_id = variable['id']
301 default_value = get_value(cdef,category_id,variable_id)
302 current_value = get_current_value(cread,cwrite,category_id,variable_id)
303 varname = get_varname (cread,category_id, variable_id)
305 print_name_comments (cdef, category_id, variable_id)
307 print "%s = %s" % (varname,current_value)
309 print "%s" % (varname)
311 def show_variables_all (cdef, cread, cwrite, show_value, show_comments):
312 for (category_id, (category, variables)) in cread.variables().iteritems():
313 print ("========== Category = %s" % category_id.upper())
314 for variable in variables.values():
315 show_variable (cdef, cread, cwrite,
316 category, variable,show_value,show_comments)
318 def show_variables_category (cdef, cread, cwrite, cid, show_value,show_comments):
321 print ("========== Category = %s" % CID)
322 for vid in list_category(cdef,cid):
323 (category,variable) = cdef.locate_varname(vid.upper())
324 show_variable (cdef, cread, cwrite, category, variable,
325 show_value,show_comments)
328 re_mainloop_0arg="^(?P<command>[uUwrRqlLsSeEcvVhH\?])[ \t]*$"
329 re_mainloop_1arg="^(?P<command>[sSeEvV])[ \t]+(?P<arg>\w+)$"
330 matcher_mainloop_0arg=re.compile(re_mainloop_0arg)
331 matcher_mainloop_1arg=re.compile(re_mainloop_1arg)
333 def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config):
337 answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
340 except KeyboardInterrupt:
344 if (answer == "") or (answer in "?hH"):
347 groups_parse = matcher_mainloop_0arg.match(answer)
350 command = groups_parse.group('command')
353 groups_parse = matcher_mainloop_1arg.match(answer)
355 command = groups_parse.group('command')
356 arg=groups_parse.group('arg')
358 print ("Unknown command >%s< -- use h for help" % answer)
361 show_comments=command.isupper()
367 variables=list_category (cdef,arg)
369 # category_id as the category name
370 # variables as the list of variable names
374 (category,variable)=cdef.locate_varname(arg)
376 # category/variable as output by locate_varname
379 print "%s: no such category or variable" % arg
383 # todo check confirmation
387 # Confirm that various constraints are met before saving file.
388 validate_variables = g_configuration.get('validate_variables',{})
389 validated_variables = cwrite.verify(cdef, cread, validate_variables)
390 validator = g_configuration.get('validator',noop_validator)
391 validator(validated_variables)
392 cwrite.save(site_config)
394 print "Save failed due to a configuration exception:"
395 print traceback.print_exc()
396 print ("Could not save -- fix write access on %s" % site_config)
398 print ("Wrote %s" % site_config)
399 consolidate(default_config, site_config, consolidated_config)
400 print ("You might want to type 'r' (restart %s), 'R' (reload %s) or 'q' (quit)" % \
402 elif command in "uU":
403 global usual_variables
405 for varname in usual_variables:
406 (category,variable) = cdef.locate_varname(varname)
407 if not (category is None and variable is None):
408 prompt_variable(cdef, cread, cwrite, category, variable, False)
409 except Exception, inst:
410 if (str(inst) != 'BailOut'):
417 print_categories(cread)
418 elif command in "eE":
420 prompt_variables_all(cdef, cread, cwrite,show_comments)
421 elif mode == 'CATEGORY':
422 prompt_variables_category(cdef,cread,cwrite,category_id,show_comments)
423 elif mode == 'VARIABLE':
425 prompt_variable (cdef,cread,cwrite,category,variable,
427 except Exception, inst:
428 if str(inst) != 'BailOut':
430 elif command in "vVsSlL":
431 show_value=(command in "sSlL")
432 (c1,c2,c3) = (cdef, cread, cwrite)
434 (c1,c2,c3) = (cwrite,cwrite,cwrite)
436 show_variables_all(c1,c2,c3,show_value,show_comments)
437 elif mode == 'CATEGORY':
438 show_variables_category(c1,c2,c3,category_id,show_value,show_comments)
439 elif mode == 'VARIABLE':
440 show_variable (c1,c2,c3,category,variable,show_value,show_comments)
442 print ("Unknown command >%s< -- use h for help" % answer)
446 # creates directory for file if not yet existing
447 def check_dir (config_file):
448 dirname = os.path.dirname (config_file)
449 if (not os.path.exists (dirname)):
451 os.makedirs(dirname,0755)
453 print "Cannot create dir %s due to %s - exiting" % (dirname,e)
456 if (not os.path.exists (dirname)):
457 print "Cannot create dir %s - exiting" % dirname
460 print "Created directory %s" % dirname
463 def optParserSetup(configuration):
464 parser = OptionParser(usage=usage(), version="%prog " + version_tag )
465 parser.set_defaults(config_dir=configuration['config_dir'],
466 service=configuration['service'],
467 usual_variables=configuration['usual_variables'])
468 parser.add_option("","--configdir",dest="config_dir",help="specify configuration directory")
469 parser.add_option("","--service",dest="service",help="specify /etc/init.d style service name")
470 parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable")
473 def main(command,argv,configuration):
474 global g_configuration
475 g_configuration=configuration
477 parser = optParserSetup(configuration)
478 (config,args) = parser.parse_args()
480 parser.error("too many arguments")
482 configuration['service']=config.service
483 configuration['usual_variables']=config.usual_variables
484 configuration['config_dir']=config.config_dir
485 # add in new usual_variables defined on the command line
486 for usual_variable in config.usual_variables:
487 if usual_variable not in configuration['usual_variables']:
488 configuration['usual_variables'].append(usual_variable)
490 # intialize configuration
493 (default_config,site_config,consolidated_config) = (def_default_config, def_site_config, def_consolidated_config)
495 default_config=args[0]
499 consolidated_config=args[2]
501 for c in (default_config,site_config,consolidated_config):
505 # the default settings only - read only
506 cdef = Config(default_config)
508 # in effect : default settings + local settings - read only
509 cread = Config(default_config)
511 print traceback.print_exc()
512 print ("default config files %s not found, is myplc installed ?" % default_config)
515 # local settings only, will be modified & saved
516 config_filename = "%s/sfa_config" % config.config_dir
517 cwrite=Config(config_filename)
519 cread.load(site_config)
520 cwrite.load(default_config)
521 cwrite.load(site_config)
525 mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config)
528 if __name__ == '__main__':
531 main(command,argv,configuration)