3 # Interactively prompts for variable values
4 # expected arguments are
5 # command -d [default-xml [custom-xml [ consolidated-xml ]]]
7 # -d is for the myplc-devel package
9 # we use 3 instances of PLCConfiguration throughout:
10 # cdef : models the defaults, from plc_default.xml
11 # cread : merged from plc_default & configs/site.xml
12 # cwrite : site.xml + pending changes
20 from plc_config import PLCConfiguration
23 release_id = "$Id: plc-config-tty,v 1.10 2006/12/12 10:14:44 thierry Exp $"
24 release_rev = "$Revision: 1.10 $"
26 def init_flavour (flavour):
28 global usual_variables
29 if (flavour == "devel"):
31 usual_variables=("PLC_DEVEL_FEDORA_URL",
33 config_dir = "/plc/devel/data/etc/planetlab"
36 usual_variables=("PLC_NAME",
41 "PLC_MAIL_SUPPORT_ADDRESS",
49 config_dir = "/etc/planetlab"
50 global def_default_config
51 def_default_config= "%s/default_config.xml" % config_dir
52 global def_site_config
53 def_site_config = "%s/configs/site.xml" % config_dir
54 global def_consolidated_config
55 def_consolidated_config = "%s/plc_config.xml" % config_dir
58 mainloop_usage= """Available commands:
59 Uppercase versions give variables comments, when available
60 -u/U\t\t\tEdit usual variables
61 -w\t\t\tWrite & consolidate
62 -r\t\t\tRestart %s service
63 -q\t\t\tQuit (without saving)
66 l/L [<cat>|<var>]\tShow Locally modified variables/values
67 -s/S [<cat>|<var>]\tShow variables/values (all, in category, single)
68 -e/E [<cat>|<var>]\tEdit variables (all, in category, single)
70 -c\t\t\tList categories
71 -v/V [<cat>|<var>]List Variables (all, in category, single)
73 Typical usage involves: u, [l,] w, r, q
77 command_usage="Usage: %s [-d] [-v] [default-xml [site-xml [consolidated-xml]]]"% sys.argv[0]
80 -v shows version and exits
81 \t default-xml defaults to %s
82 \t site-xml defaults to %s
83 \t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config)
85 Unless you specify the -d option, meaning you want to configure
86 myplc-devel instead of regular myplc, in which case"""
87 init_flavour ("devel")
89 \t default-xml defaults to %s
90 \t site-xml defaults to %s
91 \t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config)
96 variable_usage= """Edit Commands :
97 #\tShow variable comments
98 .\tStops prompting, return to mainloop
99 /\tCleans any site-defined value, reverts to default
100 =\tShows default value
101 >\tSkips to next category
106 def get_value (config, category_id, variable_id):
107 (category, variable) = config.get (category_id, variable_id)
108 return variable['value']
110 def get_current_value (cread, cwrite, category_id, variable_id):
111 # the value stored in cwrite, if present, is the one we want
113 result=get_value (cwrite,category_id,variable_id)
115 result=get_value (cread,category_id,variable_id)
118 # refrain from using plc_config's _sanitize
119 def get_varname (config, category_id, variable_id):
120 (category, variable) = config.get (category_id, variable_id)
121 return (category_id+"_"+variable['id']).upper()
123 # could not avoid using _sanitize here..
124 def get_name_comments (config, cid, vid):
126 (category, variable) = config.get (cid, vid)
127 (id, name, value, comments) = config._sanitize_variable (cid,variable)
128 return (name,comments)
132 def print_name_comments (config, cid, vid):
133 (name,comments)=get_name_comments(config,cid,vid)
135 print "### %s" % name
137 for line in comments:
140 print "!!! No comment associated to %s_%s" % (cid,vid)
143 def list_categories (config):
145 for (category_id, (category, variables)) in config.variables().iteritems():
146 result += [category_id]
149 def print_categories (config):
150 print "Known categories"
151 for cid in list_categories(config):
152 print "%s" % (cid.upper())
155 def list_category (config, cid):
157 for (category_id, (category, variables)) in config.variables().iteritems():
158 if (cid == category_id):
159 for variable in variables.values():
160 result += ["%s_%s" %(cid,variable['id'])]
163 def print_category (config, cid, show_comments=True):
166 vids=list_category(config,cid)
168 print "%s : no such category"%CID
170 print "Category %s contains" %(CID)
175 def consolidate (default_config, site_config, consolidated_config):
177 conso = PLCConfiguration (default_config)
178 conso.load (site_config)
179 conso.save (consolidated_config)
180 except Exception, inst:
181 print "Could not consolidate, %s" % (str(inst))
183 print ("Merged\n\t%s\nand\t%s\ninto\t%s"%(default_config,site_config,
184 consolidated_config))
188 print ("==================== Stopping %s" % service)
189 os.system("service %s stop" % service)
190 print ("==================== Starting %s" % service)
191 os.system("service %s start" % service)
194 def prompt_variable (cdef, cread, cwrite, category, variable,
195 show_comments, support_next=False):
197 assert category.has_key('id')
198 assert variable.has_key('id')
200 category_id = category ['id']
201 variable_id = variable['id']
204 default_value = get_value(cdef,category_id,variable_id)
205 current_value = get_current_value(cread,cwrite,category_id, variable_id)
206 varname = get_varname (cread,category_id, variable_id)
209 print_name_comments (cdef, category_id, variable_id)
210 prompt = "== %s : [%s] " % (varname,current_value)
212 answer = raw_input(prompt).strip()
214 raise Exception ('BailOut')
217 if (answer == "") or (answer == current_value):
219 elif (answer == "."):
220 raise Exception ('BailOut')
221 elif (answer == "#"):
222 print_name_comments(cread,category_id,variable_id)
223 elif (answer == "?"):
224 print variable_usage.strip()
225 elif (answer == "="):
226 print ("%s defaults to %s" %(varname,default_value))
227 # revert to default : remove from cwrite (i.e. site-config)
228 elif (answer == "/"):
229 cwrite.delete(category_id,variable_id)
230 print ("%s reverted to %s" %(varname,default_value))
232 elif (answer == ">"):
234 raise Exception ('NextCategory')
236 print "No support for next category"
238 variable['value'] = answer
239 cwrite.set(category,variable)
242 def prompt_variables_all (cdef, cread, cwrite, show_comments):
244 for (category_id, (category, variables)) in cread.variables().iteritems():
245 print ("========== Category = %s" % category_id.upper())
246 for variable in variables.values():
248 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
250 except Exception, inst:
251 if (str(inst) == 'NextCategory'): break
254 except Exception, inst:
255 if (str(inst) == 'BailOut'): return
258 def prompt_variables_category (cdef, cread, cwrite, cid, show_comments):
262 print ("========== Category = %s" % CID)
263 for vid in list_category(cdef,cid):
264 (category,variable) = cdef.locate_varname(vid.upper())
265 newvar = prompt_variable (cdef, cread, cwrite, category, variable,
266 show_comments, False)
267 except Exception, inst:
268 if (str(inst) == 'BailOut'): return
272 def show_variable (cdef, cread, cwrite,
273 category, variable,show_value,show_comments):
274 assert category.has_key('id')
275 assert variable.has_key('id')
277 category_id = category ['id']
278 variable_id = variable['id']
280 default_value = get_value(cdef,category_id,variable_id)
281 current_value = get_current_value(cread,cwrite,category_id,variable_id)
282 varname = get_varname (cread,category_id, variable_id)
284 print_name_comments (cdef, category_id, variable_id)
286 print "%s = %s" % (varname,current_value)
288 print "%s" % (varname)
290 def show_variables_all (cdef, cread, cwrite, show_value, show_comments):
291 for (category_id, (category, variables)) in cread.variables().iteritems():
292 print ("========== Category = %s" % category_id.upper())
293 for variable in variables.values():
294 show_variable (cdef, cread, cwrite,
295 category, variable,show_value,show_comments)
297 def show_variables_category (cdef, cread, cwrite, cid, show_value,show_comments):
300 print ("========== Category = %s" % CID)
301 for vid in list_category(cdef,cid):
302 (category,variable) = cdef.locate_varname(vid.upper())
303 show_variable (cdef, cread, cwrite, category, variable,
304 show_value,show_comments)
307 re_mainloop_0arg="^(?P<command>[uUwrqlLsSeEcvVhH\?])[ \t]*$"
308 re_mainloop_1arg="^(?P<command>[sSeEvV])[ \t]+(?P<arg>\w+)$"
309 matcher_mainloop_0arg=re.compile(re_mainloop_0arg)
310 matcher_mainloop_1arg=re.compile(re_mainloop_1arg)
312 def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config):
315 answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
318 if (answer == "") or (answer in "?hH"):
321 groups_parse = matcher_mainloop_0arg.match(answer)
324 command = groups_parse.group('command')
327 groups_parse = matcher_mainloop_1arg.match(answer)
329 command = groups_parse.group('command')
330 arg=groups_parse.group('arg')
332 print ("Unknown command >%s< -- use h for help" % answer)
335 show_comments=command.isupper()
336 command=command.lower()
342 variables=list_category (cdef,arg)
344 # category_id as the category name
345 # variables as the list of variable names
349 (category,variable)=cdef.locate_varname(arg)
351 # category/variable as output by locate_varname
354 print "%s: no such category or variable" % arg
357 if (command in "qQ"):
358 # todo check confirmation
360 elif (command in "wW"):
362 cwrite.save(site_config)
364 print ("Could not save -- fix write access on %s" % site_config)
366 print ("Wrote %s" % site_config)
367 consolidate(default_config, site_config, consolidated_config)
368 print ("You might want to type 'r' (restart plc) or 'q' (quit)")
369 elif (command == "u"):
371 for varname in usual_variables:
372 (category,variable) = cdef.locate_varname(varname)
373 prompt_variable(cdef, cread, cwrite, category, variable, False)
374 except Exception, inst:
375 if (str(inst) != 'BailOut'):
377 elif (command == "r"):
379 elif (command == "c"):
380 print_categories(cread)
381 elif (command in "eE"):
383 prompt_variables_all(cdef, cread, cwrite,show_comments)
384 elif mode == 'CATEGORY':
385 prompt_variables_category(cdef,cread,cwrite,category_id,show_comments)
386 elif mode == 'VARIABLE':
388 prompt_variable (cdef,cread,cwrite,category,variable,
390 except Exception, inst:
391 if (str(inst) != 'BailOut'):
393 elif (command in "vVsSlL"):
394 show_value=(command in "sSlL")
395 (c1,c2,c3) = (cdef, cread, cwrite)
396 if (command in "lL"):
397 (c1,c2,c3) = (cwrite,cwrite,cwrite)
399 show_variables_all(c1,c2,c3,show_value,show_comments)
400 elif mode == 'CATEGORY':
401 show_variables_category(c1,c2,c3,category_id,show_value,show_comments)
402 elif mode == 'VARIABLE':
403 show_variable (c1,c2,c3,category,variable,show_value,show_comments)
405 print ("Unknown command >%s< -- use h for help" % answer)
408 def check_dir (config_file):
409 dirname = os.path.dirname (config_file)
410 if (not os.path.exists (dirname)):
411 print "Config file %s located under a non-existing directory" % config_file
412 answer=raw_input("Want to create %s [y]/n ? " % dirname)
413 answer = answer.lower()
415 print "Cannot proceed - good bye"
418 os.makedirs(dirname,0755)
419 if (not os.path.exists (dirname)):
420 print "Cannot create dir %s - exiting" % dirname
423 print "Created directory %s" % dirname
433 # default is myplc (non -devel) unless -d is specified
435 optlist,list = getopt.getopt(argv,":dhv")
440 print ("This is %s - %s" %(command,release_rev))
443 init_flavour("devel")
447 (default_config,site_config,consolidated_config) = (def_default_config, def_site_config, def_consolidated_config)
449 (default_config,site_config,consolidated_config) = (argv[0], def_site_config, def_consolidated_config)
451 (default_config, site_config,consolidated_config) = (argv[0], argv[1], def_consolidated_config)
453 (default_config, site_config,consolidated_config) = argv
457 for c in (default_config,site_config,consolidated_config):
461 # the default settings only - read only
462 cdef = PLCConfiguration(default_config)
464 # in effect : default settings + local settings - read only
465 cread = PLCConfiguration(default_config)
468 print ("default config files not found, is myplc installed ?")
471 # local settings only, will be modified & saved
472 cwrite=PLCConfiguration()
475 cread.load(site_config)
476 cwrite.load(site_config)
478 cwrite = PLCConfiguration()
480 mainloop (cdef, cread, cwrite,default_config, site_config, consolidated_config)
483 if __name__ == '__main__':