10 from StringIO import StringIO
11 from optparse import OptionParser
12 from sfa.util.config import Config
14 def validator(validated_variables):
16 # maint_user = validated_variables["PLC_API_MAINTENANCE_USER"]
17 # root_user = validated_variables["PLC_ROOT_USER"]
18 # if maint_user == root_user:
19 # errStr="PLC_API_MAINTENANCE_USER=%s cannot be the same as PLC_ROOT_USER=%s"%(maint_user,root_user)
20 # raise plc_config.ConfigurationException(errStr)
23 "SFA_GENERIC_FLAVOUR",
25 "SFA_REGISTRY_ROOT_AUTH",
38 'usual_variables':usual_variables,
39 'config_dir':"/etc/sfa",
40 'validate_variables':{},
41 'validator':validator,
48 release_rev = "$Revision$"
56 def noop_validator(validated_variables):
59 # historically we could also configure the devel pkg....
60 def init_configuration ():
61 global g_configuration
62 global usual_variables, config_dir, service
64 usual_variables=g_configuration["usual_variables"]
65 config_dir=g_configuration["config_dir"]
66 service=g_configuration["service"]
68 global def_default_config, def_site_config, def_consolidated_config
69 def_default_config= "%s/default_config.xml" % config_dir
70 def_site_config = "%s/configs/site_config" % config_dir
71 def_consolidated_config = "%s/%s_config" % (config_dir, service)
74 mainloop_usage= """Available commands:
75 Uppercase versions give variables comments, when available
76 u/U\t\t\tEdit usual variables
78 r\t\t\tRestart %(service)s service
79 R\t\t\tReload %(service)s service (rebuild config files for sh, python....)
80 q\t\t\tQuit (without saving)
83 l/L [<cat>|<var>]\tShow Locally modified variables/values
84 s/S [<cat>|<var>]\tShow variables/values (all, in category, single)
85 e/E [<cat>|<var>]\tEdit variables (all, in category, single)
87 c\t\t\tList categories
88 v/V [<cat>|<var>]\tList Variables (all, in category, single)
90 Typical usage involves: u, [l,] w, r, q
94 command_usage="%prog [options] [default-xml [site-xml [consolidated-xml]]]"
97 \t default-xml defaults to %s
98 \t site-xml defaults to %s
99 \t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config)
103 variable_usage= """Edit Commands :
104 #\tShow variable comments
105 .\tStops prompting, return to mainloop
106 /\tCleans any site-defined value, reverts to default
107 =\tShows default value
108 >\tSkips to next category
113 def get_value (config, category_id, variable_id):
114 value = config.get (category_id, variable_id)
117 def get_type (config, category_id, variable_id):
118 value = config.get (category_id, variable_id)
119 #return variable['type']
122 def get_current_value (cread, cwrite, category_id, variable_id):
123 # the value stored in cwrite, if present, is the one we want
125 result=get_value (cwrite,category_id,variable_id)
127 result=get_value (cread,category_id,variable_id)
130 # refrain from using plc_config's _sanitize
131 def get_varname (config, category_id, variable_id):
132 varname = category_id +"_"+ variable_id
133 config.locate_varname(varname)
136 # could not avoid using _sanitize here..
137 def get_name_comments (config, cid, vid):
139 (category, variable) = config.get (cid, vid)
140 (id, name, value, comments) = config._sanitize_variable (cid,variable)
141 return (name,comments)
145 def print_name_comments (config, cid, vid):
146 (name,comments)=get_name_comments(config,cid,vid)
148 print "### %s" % name
150 for line in comments:
153 print "!!! No comment associated to %s_%s" % (cid,vid)
156 def list_categories (config):
158 for section in config.sections():
162 def print_categories (config):
163 print "Known categories"
164 for cid in list_categories(config):
165 print "%s" % (cid.upper())
168 def list_category (config, cid):
170 for section in config.sections():
171 if section == cid.lower():
172 for (name,value) in config.items(section):
173 result += ["%s_%s" %(cid,name)]
176 def print_category (config, cid, show_comments=True):
179 vids=list_category(config,cid)
181 print "%s : no such category"%CID
183 print "Category %s contains" %(CID)
188 def consolidate (default_config, site_config, consolidated_config):
191 conso = Config(default_config)
192 conso.load (site_config)
193 conso.save (consolidated_config)
194 except Exception, inst:
195 print "Could not consolidate, %s" % (str(inst))
197 print ("Merged\n\t%s\nand\t%s\ninto\t%s"%(default_config,site_config,
198 consolidated_config))
200 def reload_service ():
202 os.system("set -x ; service %s reload" % service)
205 def restart_service ():
207 print ("==================== Stopping %s" % service)
208 os.system("service %s stop" % service)
209 print ("==================== Starting %s" % service)
210 os.system("service %s start" % service)
213 def prompt_variable (cdef, cread, cwrite, category, variable,
214 show_comments, support_next=False):
217 category_id = category
218 variable_id = variable
221 default_value = get_value(cdef,category_id,variable_id)
222 variable_type = get_type(cdef,category_id,variable_id)
223 current_value = get_current_value(cread,cwrite,category_id, variable_id)
224 varname = get_varname (cread,category_id, variable_id)
227 print_name_comments (cdef, category_id, variable_id)
228 prompt = "== %s : [%s] " % (varname,current_value)
230 answer = raw_input(prompt).strip()
232 raise Exception ('BailOut')
233 except KeyboardInterrupt:
235 raise Exception ('BailOut')
238 if (answer == "") or (answer == current_value):
240 elif (answer == "."):
241 raise Exception ('BailOut')
242 elif (answer == "#"):
243 print_name_comments(cread,category_id,variable_id)
244 elif (answer == "?"):
245 print variable_usage.strip()
246 elif (answer == "="):
247 print ("%s defaults to %s" %(varname,default_value))
248 # revert to default : remove from cwrite (i.e. site-config)
249 elif (answer == "/"):
250 cwrite.delete(category_id,variable_id)
251 print ("%s reverted to %s" %(varname,default_value))
253 elif (answer == ">"):
255 raise Exception ('NextCategory')
257 print "No support for next category"
259 if cdef.validate_type(variable_type, answer):
260 cwrite.set(category_id, variable_id, answer)
263 print "Not a valid value"
265 def prompt_variables_all (cdef, cread, cwrite, show_comments):
267 for (category_id, (category, variables)) in cread.variables().iteritems():
268 print ("========== Category = %s" % category_id.upper())
269 for variable in variables.values():
271 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
273 except Exception, inst:
274 if (str(inst) == 'NextCategory'): break
277 except Exception, inst:
278 if (str(inst) == 'BailOut'): return
281 def prompt_variables_category (cdef, cread, cwrite, cid, show_comments):
285 print ("========== Category = %s" % CID)
286 for vid in list_category(cdef,cid):
287 (category,variable) = cdef.locate_varname(vid.upper())
288 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
289 show_comments, False)
290 except Exception, inst:
291 if (str(inst) == 'BailOut'): return
295 def show_variable (cdef, cread, cwrite,
296 category, variable,show_value,show_comments):
297 assert category.has_key('id')
298 assert variable.has_key('id')
300 category_id = category ['id']
301 variable_id = variable['id']
303 default_value = get_value(cdef,category_id,variable_id)
304 current_value = get_current_value(cread,cwrite,category_id,variable_id)
305 varname = get_varname (cread,category_id, variable_id)
307 print_name_comments (cdef, category_id, variable_id)
309 print "%s = %s" % (varname,current_value)
311 print "%s" % (varname)
313 def show_variables_all (cdef, cread, cwrite, show_value, show_comments):
314 for (category_id, (category, variables)) in cread.variables().iteritems():
315 print ("========== Category = %s" % category_id.upper())
316 for variable in variables.values():
317 show_variable (cdef, cread, cwrite,
318 category, variable,show_value,show_comments)
320 def show_variables_category (cdef, cread, cwrite, cid, show_value,show_comments):
323 print ("========== Category = %s" % CID)
324 for vid in list_category(cdef,cid):
325 (category,variable) = cdef.locate_varname(vid.upper())
326 show_variable (cdef, cread, cwrite, category, variable,
327 show_value,show_comments)
330 re_mainloop_0arg="^(?P<command>[uUwrRqlLsSeEcvVhH\?])[ \t]*$"
331 re_mainloop_1arg="^(?P<command>[sSeEvV])[ \t]+(?P<arg>\w+)$"
332 matcher_mainloop_0arg=re.compile(re_mainloop_0arg)
333 matcher_mainloop_1arg=re.compile(re_mainloop_1arg)
335 def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config):
339 answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
342 except KeyboardInterrupt:
346 if (answer == "") or (answer in "?hH"):
349 groups_parse = matcher_mainloop_0arg.match(answer)
352 command = groups_parse.group('command')
355 groups_parse = matcher_mainloop_1arg.match(answer)
357 command = groups_parse.group('command')
358 arg=groups_parse.group('arg')
360 print ("Unknown command >%s< -- use h for help" % answer)
363 show_comments=command.isupper()
369 variables=list_category (cdef,arg)
371 # category_id as the category name
372 # variables as the list of variable names
376 (category,variable)=cdef.locate_varname(arg)
378 # category/variable as output by locate_varname
381 print "%s: no such category or variable" % arg
385 # todo check confirmation
389 # Confirm that various constraints are met before saving file.
390 validate_variables = g_configuration.get('validate_variables',{})
391 validated_variables = cwrite.verify(cdef, cread, validate_variables)
392 validator = g_configuration.get('validator',noop_validator)
393 validator(validated_variables)
394 cwrite.save(site_config)
396 print "Save failed due to a configuration exception:"
397 print traceback.print_exc()
398 print ("Could not save -- fix write access on %s" % site_config)
400 print ("Wrote %s" % site_config)
401 consolidate(default_config, site_config, consolidated_config)
402 print ("You might want to type 'r' (restart %s), 'R' (reload %s) or 'q' (quit)" % \
404 elif command in "uU":
405 global usual_variables
407 for varname in usual_variables:
408 (category,variable) = cdef.locate_varname(varname)
409 if not (category is None and variable is None):
410 prompt_variable(cdef, cread, cwrite, category, variable, False)
411 except Exception, inst:
412 if (str(inst) != 'BailOut'):
419 print_categories(cread)
420 elif command in "eE":
422 prompt_variables_all(cdef, cread, cwrite,show_comments)
423 elif mode == 'CATEGORY':
424 prompt_variables_category(cdef,cread,cwrite,category_id,show_comments)
425 elif mode == 'VARIABLE':
427 prompt_variable (cdef,cread,cwrite,category,variable,
429 except Exception, inst:
430 if str(inst) != 'BailOut':
432 elif command in "vVsSlL":
433 show_value=(command in "sSlL")
434 (c1,c2,c3) = (cdef, cread, cwrite)
436 (c1,c2,c3) = (cwrite,cwrite,cwrite)
438 show_variables_all(c1,c2,c3,show_value,show_comments)
439 elif mode == 'CATEGORY':
440 show_variables_category(c1,c2,c3,category_id,show_value,show_comments)
441 elif mode == 'VARIABLE':
442 show_variable (c1,c2,c3,category,variable,show_value,show_comments)
444 print ("Unknown command >%s< -- use h for help" % answer)
448 # creates directory for file if not yet existing
449 def check_dir (config_file):
450 dirname = os.path.dirname (config_file)
451 if (not os.path.exists (dirname)):
453 os.makedirs(dirname,0755)
455 print "Cannot create dir %s due to %s - exiting" % (dirname,e)
458 if (not os.path.exists (dirname)):
459 print "Cannot create dir %s - exiting" % dirname
462 print "Created directory %s" % dirname
465 def optParserSetup(configuration):
466 parser = OptionParser(usage=usage(), version="%prog " + release_rev + release_url )
467 parser.set_defaults(config_dir=configuration['config_dir'],
468 service=configuration['service'],
469 usual_variables=configuration['usual_variables'])
470 parser.add_option("","--configdir",dest="config_dir",help="specify configuration directory")
471 parser.add_option("","--service",dest="service",help="specify /etc/init.d style service name")
472 parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable")
475 def main(command,argv,configuration):
476 global g_configuration
477 g_configuration=configuration
479 parser = optParserSetup(configuration)
480 (config,args) = parser.parse_args()
482 parser.error("too many arguments")
484 configuration['service']=config.service
485 configuration['usual_variables']=config.usual_variables
486 configuration['config_dir']=config.config_dir
487 # add in new usual_variables defined on the command line
488 for usual_variable in config.usual_variables:
489 if usual_variable not in configuration['usual_variables']:
490 configuration['usual_variables'].append(usual_variable)
492 # intialize configuration
495 (default_config,site_config,consolidated_config) = (def_default_config, def_site_config, def_consolidated_config)
497 default_config=args[0]
501 consolidated_config=args[2]
503 for c in (default_config,site_config,consolidated_config):
507 # the default settings only - read only
508 cdef = Config(default_config)
510 # in effect : default settings + local settings - read only
511 cread = Config(default_config)
513 print traceback.print_exc()
514 print ("default config files %s not found, is myplc installed ?" % default_config)
517 # local settings only, will be modified & saved
518 config_filename = "%s/sfa_config" % config.config_dir
520 cwrite=Config(config_filename)
521 except ConfigParser.MissingSectionHeaderError:
522 # remove legacy config
523 os.unlink(config_filename)
524 cwrite=Config(config_filename)
527 cread.load(site_config)
528 cwrite.load(site_config)
532 mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config)
535 if __name__ == '__main__':
538 main(command,argv,configuration)