ia64/xen-unstable

changeset 7945:4f7b4ca2e30c

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>
author emellor@leeni.uk.xensource.com
date Mon Nov 21 13:00:36 2005 +0100 (2005-11-21)
parents 508bc376c1dd
children d88b33abf533
files tools/python/xen/xm/create.py tools/python/xen/xm/opts.py
line diff
     1.1 --- a/tools/python/xen/xm/create.py	Mon Nov 21 12:55:51 2005 +0100
     1.2 +++ b/tools/python/xen/xm/create.py	Mon Nov 21 13:00:36 2005 +0100
     1.3 @@ -879,33 +879,33 @@ def balloon_out(dom0_min_mem, opts):
     1.4  
     1.5  
     1.6  def parseCommandLine(argv):
     1.7 -    opts = gopts
     1.8 -    args = opts.parse(argv)
     1.9 -    if opts.vals.help:
    1.10 -        opts.usage()
    1.11 -    if opts.vals.help or opts.vals.help_config:
    1.12 -        opts.load_defconfig(help=1)
    1.13 -    if opts.vals.help or opts.vals.help_config:
    1.14 +    gopts.reset()
    1.15 +    args = gopts.parse(argv)
    1.16 +    if gopts.vals.help:
    1.17 +        gopts.usage()
    1.18 +    if gopts.vals.help or gopts.vals.help_config:
    1.19 +        gopts.load_defconfig(help=1)
    1.20 +    if gopts.vals.help or gopts.vals.help_config:
    1.21          return (None, None)
    1.22  
    1.23 -    if not opts.vals.display:
    1.24 -        opts.vals.display = os.getenv("DISPLAY")
    1.25 +    if not gopts.vals.display:
    1.26 +        gopts.vals.display = os.getenv("DISPLAY")
    1.27  
    1.28      # Process remaining args as config variables.
    1.29      for arg in args:
    1.30          if '=' in arg:
    1.31              (var, val) = arg.strip().split('=', 1)
    1.32              gopts.setvar(var.strip(), val.strip())
    1.33 -    if opts.vals.config:
    1.34 -        config = opts.vals.config
    1.35 +    if gopts.vals.config:
    1.36 +        config = gopts.vals.config
    1.37      else:
    1.38 -        opts.load_defconfig()
    1.39 -        preprocess(opts.vals)
    1.40 -        if not opts.getopt('name') and opts.getopt('defconfig'):
    1.41 -            opts.setopt('name', os.path.basename(opts.getopt('defconfig')))
    1.42 -        config = make_config(opts.vals)
    1.43 +        gopts.load_defconfig()
    1.44 +        preprocess(gopts.vals)
    1.45 +        if not gopts.getopt('name') and gopts.getopt('defconfig'):
    1.46 +            gopts.setopt('name', os.path.basename(gopts.getopt('defconfig')))
    1.47 +        config = make_config(gopts.vals)
    1.48  
    1.49 -    return (opts, config)
    1.50 +    return (gopts, config)
    1.51  
    1.52  
    1.53  def main(argv):
     2.1 --- a/tools/python/xen/xm/opts.py	Mon Nov 21 12:55:51 2005 +0100
     2.2 +++ b/tools/python/xen/xm/opts.py	Mon Nov 21 13:00:36 2005 +0100
     2.3 @@ -60,6 +60,14 @@ class Opt:
     2.4          self.value = None
     2.5          self.set(default)
     2.6  
     2.7 +
     2.8 +    def reset(self):
     2.9 +        self.specified_opt = None
    2.10 +        self.specified_val = None
    2.11 +        self.value = None
    2.12 +        self.set(self.default)
    2.13 +
    2.14 +
    2.15      def __repr__(self):
    2.16          return self.name + '=' + str(self.specified_val)
    2.17  
    2.18 @@ -223,6 +231,14 @@ class Opts:
    2.19          # Option to use for bare words.
    2.20          self.default_opt = None
    2.21  
    2.22 +
    2.23 +    def reset(self):
    2.24 +        self.vals = OptVals()
    2.25 +        self.vars = {}
    2.26 +        for opt in self.options:
    2.27 +            opt.reset()
    2.28 +
    2.29 +
    2.30      def __repr__(self):
    2.31          return '\n'.join(map(str, self.options))
    2.32  
    2.33 @@ -416,22 +432,22 @@ class Opts:
    2.34          are used to set options with the same names.
    2.35          Variables are not used to set options that are already specified.
    2.36          """
    2.37 -        # Create global and lobal dicts for the file.
    2.38 +        # Create global and local dicts for the file.
    2.39          # Initialize locals to the vars.
    2.40          # Use exec to do the standard imports and
    2.41          # define variables we are passing to the script.
    2.42 -        globals = {}
    2.43 -        locals = {}
    2.44 -        locals.update(self.vars)
    2.45 +        globs = {}
    2.46 +        locs = {}
    2.47 +        locs.update(self.vars)
    2.48          cmd = '\n'.join(self.imports + 
    2.49                          [ "from xen.xm.help import Vars",
    2.50                            "xm_file = '%s'" % defconfig,
    2.51                            "xm_help = %d" % help,
    2.52                            "xm_vars = Vars(xm_file, xm_help, locals())"
    2.53                            ])
    2.54 -        exec cmd in globals, locals
    2.55 +        exec cmd in globs, locs
    2.56          try:
    2.57 -            execfile(defconfig, globals, locals)
    2.58 +            execfile(defconfig, globs, locs)
    2.59          except:
    2.60              if not help: raise
    2.61          if help:
    2.62 @@ -444,7 +460,7 @@ class Opts:
    2.63                     types.IntType,
    2.64                     types.FloatType
    2.65                     ]
    2.66 -        for (k, v) in locals.items():
    2.67 +        for (k, v) in locs.items():
    2.68              if self.specified(k): continue
    2.69              if not(type(v) in vtypes): continue
    2.70              self.setopt(k, v)