Added reset method to Opts so that create.parseCommandLine can be used more than

once.  This makes it possible to unit test this code.  Remove the odd aliasing
of gopts to opts inside parseCommandLine.

Rename local variables globals and locals inside opts.load -- these are built-in
functions.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
This commit is contained in:
emellor@leeni.uk.xensource.com committed 2005-11-21 13:00:36 +01:00
1 parent 8b3050a110
commit bbd9571e5f
2 files changed
+40 -24

No files matched your search

+17 -17
View File
@@ -879,33 +879,33 @@ def balloon_out(dom0_min_mem, opts):
def parseCommandLine(argv):
opts = gopts
args = opts.parse(argv)
if opts.vals.help:
opts.usage()
if opts.vals.help or opts.vals.help_config:
opts.load_defconfig(help=1)
if opts.vals.help or opts.vals.help_config:
gopts.reset()
args = gopts.parse(argv)
if gopts.vals.help:
gopts.usage()
if gopts.vals.help or gopts.vals.help_config:
gopts.load_defconfig(help=1)
if gopts.vals.help or gopts.vals.help_config:
return (None, None)
if not opts.vals.display:
opts.vals.display = os.getenv("DISPLAY")
if not gopts.vals.display:
gopts.vals.display = os.getenv("DISPLAY")
# Process remaining args as config variables.
for arg in args:
if '=' in arg:
(var, val) = arg.strip().split('=', 1)
gopts.setvar(var.strip(), val.strip())
if opts.vals.config:
config = opts.vals.config
if gopts.vals.config:
config = gopts.vals.config
else:
opts.load_defconfig()
preprocess(opts.vals)
if not opts.getopt('name') and opts.getopt('defconfig'):
opts.setopt('name', os.path.basename(opts.getopt('defconfig')))
config = make_config(opts.vals)
gopts.load_defconfig()
preprocess(gopts.vals)
if not gopts.getopt('name') and gopts.getopt('defconfig'):
gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
config = make_config(gopts.vals)
return (opts, config)
return (gopts, config)
def main(argv):
+23 -7
View File
@@ -60,6 +60,14 @@ class Opt:
self.value = None
self.set(default)
def reset(self):
self.specified_opt = None
self.specified_val = None
self.value = None
self.set(self.default)
def __repr__(self):
return self.name + '=' + str(self.specified_val)
@@ -223,6 +231,14 @@ class Opts:
# Option to use for bare words.
self.default_opt = None
def reset(self):
self.vals = OptVals()
self.vars = {}
for opt in self.options:
opt.reset()
def __repr__(self):
return '\n'.join(map(str, self.options))
@@ -416,22 +432,22 @@ class Opts:
are used to set options with the same names.
Variables are not used to set options that are already specified.
"""
# Create global and lobal dicts for the file.
# Create global and local dicts for the file.
# Initialize locals to the vars.
# Use exec to do the standard imports and
# define variables we are passing to the script.
globals = {}
locals = {}
locals.update(self.vars)
globs = {}
locs = {}
locs.update(self.vars)
cmd = '\n'.join(self.imports +
[ "from xen.xm.help import Vars",
"xm_file = '%s'" % defconfig,
"xm_help = %d" % help,
"xm_vars = Vars(xm_file, xm_help, locals())"
])
exec cmd in globals, locals
exec cmd in globs, locs
try:
execfile(defconfig, globals, locals)
execfile(defconfig, globs, locs)
except:
if not help: raise
if help:
@@ -444,7 +460,7 @@ class Opts:
types.IntType,
types.FloatType
]
for (k, v) in locals.items():
for (k, v) in locs.items():
if self.specified(k): continue
if not(type(v) in vtypes): continue
self.setopt(k, v)
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.