-
-def bad_recipe(filename, linenum, msg):
- """Print error message with the file name and the line number and exit."""
- sys.exit("%s: line %d: error : " % (filename, linenum) + msg)
-
-def parse_recipe(filename):
- """Parse the recipe file and retrieve the config attributes.
-
- This function parses the given recipe file and gets the name,
- the type, and the default value of the target config options.
-
- Arguments:
- filename: path to file to be parsed.
- Returns:
- A list of dictionaris, each of them includes the name,
- the type, and the default value of the target config.
- """
- config_attrs = []
- linenum = 1
-
- for line in open(filename):
- tokens = line.split()
- if len(tokens) != 3:
- bad_recipe(filename, linenum,
- "%d fields in this line. Each line must contain 3 fields"
- % len(tokens))
-
- (config, type, default) = tokens
-
- # prefix the option name with CONFIG_ if missing
- if not config.startswith('CONFIG_'):
- config = 'CONFIG_' + config
-
- # sanity check of default values
- if type == 'bool':
- if not default in ('y', 'n'):
- bad_recipe(filename, linenum,
- "default for bool type must be either y or n")
- elif type == 'tristate':
- if not default in ('y', 'm', 'n'):
- bad_recipe(filename, linenum,
- "default for tristate type must be y, m, or n")
- elif type == 'string':
- if default[0] != '"' or default[-1] != '"':
- bad_recipe(filename, linenum,
- "default for string type must be surrounded by double-quotations")
- elif type == 'int':
- try:
- int(default)
- except:
- bad_recipe(filename, linenum,
- "type is int, but default value is not decimal")
- elif type == 'hex':
- if len(default) < 2 or default[:2] != '0x':
- bad_recipe(filename, linenum,
- "default for hex type must be prefixed with 0x")
- try:
- int(default, 16)
- except:
- bad_recipe(filename, linenum,
- "type is hex, but default value is not hexadecimal")
- else:
- bad_recipe(filename, linenum,
- "unsupported type '%s'. type must be one of bool, tristate, string, int, hex"
- % type)
-
- config_attrs.append({'config': config, 'type': type, 'default': default})
- linenum += 1
-
- return config_attrs