debuggers.hg

changeset 20904:2636e5619708

pygrub: improve grub 2 support

* The "default" value can be a quoted string (containing an integer)
so strip the quotes before interpreting.
* The "set" command takes a variable with an arbitrary name so instead
of whitelisting the ones to ignore simply silently accept any set
command with an unknown variable.
* Ignore the echo command.
* Handle the function { ... } syntax. Previously pygrub would error
out with a syntax error on the closing "}" because it thought it was
the closing bracket of a menuentry.

This makes pygrub2 work with the configuration files generated by
Debian Squeeze today.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 26 15:54:40 2010 +0000 (2010-01-26)
parents 66587cc5f1e1
children 2f98b700605f
files tools/pygrub/src/GrubConf.py
line diff
     1.1 --- a/tools/pygrub/src/GrubConf.py	Tue Jan 26 15:54:09 2010 +0000
     1.2 +++ b/tools/pygrub/src/GrubConf.py	Tue Jan 26 15:54:40 2010 +0000
     1.3 @@ -219,6 +219,7 @@ class _GrubConfigFile(object):
     1.4      def _get_default(self):
     1.5          return self._default
     1.6      def _set_default(self, val):
     1.7 +        val = val.strip("\"")
     1.8          if val == "saved":
     1.9              self._default = 0
    1.10          else:
    1.11 @@ -315,6 +316,8 @@ class Grub2Image(_GrubImage):
    1.12                  setattr(self, self.commands[com], arg.strip())
    1.13              else:
    1.14                  logging.info("Ignored image directive %s" %(com,))
    1.15 +        elif com.startswith('set:'):
    1.16 +            pass
    1.17          else:
    1.18              logging.warning("Unknown image directive %s" %(com,))
    1.19  
    1.20 @@ -328,6 +331,7 @@ class Grub2Image(_GrubImage):
    1.21      commands = {'set:root': 'root',
    1.22                  'linux': 'kernel',
    1.23                  'initrd': 'initrd',
    1.24 +                'echo': None,
    1.25                  'insmod': None,
    1.26                  'search': None}
    1.27      
    1.28 @@ -346,6 +350,7 @@ class Grub2ConfigFile(_GrubConfigFile):
    1.29          else:
    1.30              lines = buf.split("\n")
    1.31  
    1.32 +        in_function = False
    1.33          img = None
    1.34          title = ""
    1.35          for l in lines:
    1.36 @@ -356,6 +361,16 @@ class Grub2ConfigFile(_GrubConfigFile):
    1.37              # skip comments
    1.38              if l.startswith('#'):
    1.39                  continue
    1.40 +
    1.41 +            # skip function declarations
    1.42 +            if l.startswith('function'):
    1.43 +                in_function = True
    1.44 +                continue
    1.45 +            if in_function:
    1.46 +                if l.startswith('}'):
    1.47 +                    in_function = False
    1.48 +                continue
    1.49 +
    1.50              # new image
    1.51              title_match = re.match('^menuentry "(.*)" {', l)
    1.52              if title_match:
    1.53 @@ -388,6 +403,8 @@ class Grub2ConfigFile(_GrubConfigFile):
    1.54                      setattr(self, self.commands[com], arg.strip())
    1.55                  else:
    1.56                      logging.info("Ignored directive %s" %(com,))
    1.57 +            elif com.startswith('set:'):
    1.58 +                pass
    1.59              else:
    1.60                  logging.warning("Unknown directive %s" %(com,))
    1.61              
    1.62 @@ -400,11 +417,10 @@ class Grub2ConfigFile(_GrubConfigFile):
    1.63      commands = {'set:default': 'default',
    1.64                  'set:root': 'root',
    1.65                  'set:timeout': 'timeout',
    1.66 -                'set:gfxmode': None,
    1.67 -                'set:menu_color_normal': None,
    1.68 -                'set:menu_color_highlight': None,
    1.69                  'terminal': None,
    1.70                  'insmod': None,
    1.71 +                'load_env': None,
    1.72 +                'save_env': None,
    1.73                  'search': None,
    1.74                  'if': None,
    1.75                  'fi': None,