Changeset ef38b5d in rtems-tools


Ignore:
Timestamp:
May 15, 2017, 1:48:44 AM (2 years ago)
Author:
Chris Johns <chrisj@…>
Branches:
master
Children:
ed80d2c
Parents:
9a8a03c
Message:

rtemstoolkit: Add Python INI configuration support.

Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • tester/rt/check.py

    r9a8a03c ref38b5d  
    4242import pprint
    4343
    44 try:
    45     import configparser
    46 except:
    47     import ConfigParser as configparser
    48 
     44from rtemstoolkit import configuration
    4945from rtemstoolkit import execute
    5046from rtemstoolkit import error
     
    441437                log.output(wrap([' ' * 6, config_cmd], lineend = '\\', width = 75))
    442438
    443 class configuration:
     439class configuration_:
    444440
    445441    def __init__(self):
    446         self.config = configparser.ConfigParser()
    447         self.name = None
    448         self.ini = None
     442        self.config = configuration.configuration()
    449443        self.archs = { }
    450444        self.builds_ = { }
    451445        self.profiles = { }
    452         self.configurations = { }
    453         self.macro_filter = re.compile('\$\{.+\}')
    454446
    455447    def __str__(self):
     
    464456        return s
    465457
    466     def _get_item(self, section, label, err = True):
    467         try:
    468             rec = self.config.get(section, label).replace(os.linesep, ' ')
    469         except:
    470             if err:
    471                 raise error.general('config: no "%s" found in "%s"' % (label, section))
    472             return None
    473         #
    474         # On Python 2.7 there is no extended interpolation so add support here.
    475         # On Python 3 this should happen automatically and so the findall
    476         # should find nothing.
    477         #
    478         for m in self.macro_filter.findall(rec):
    479             if ':' not in m:
    480                 raise error.general('config: interpolation is ${section:value}: %s' % (m))
    481             section_value = m[2:-1].split(':')
    482             if len(section_value) != 2:
    483                 raise error.general('config: interpolation is ${section:value}: %s' % (m))
    484             try:
    485                 ref = self.config.get(section_value[0],
    486                                       section_value[1]).replace(os.linesep, ' ')
    487                 rec = rec.replace(m, ref)
    488             except:
    489                 pass
    490         return rec
    491 
    492     def _get_items(self, section, err = True):
    493         try:
    494             items = [(name, key.replace(os.linesep, ' ')) \
    495                      for name, key in self.config.items(section)]
    496             return items
    497         except:
    498             if err:
    499                 raise error.general('config: section "%s" not found' % (section))
    500         return []
    501 
    502     def _comma_list(self, section, label, err = True):
    503         items = self._get_item(section, label, err)
    504         if items is None:
    505             return []
    506         return sorted(set([a.strip() for a in items.split(',')]))
    507 
    508     def _get_item_names(self, section, err = True):
    509         try:
    510             return [item[0] for item in self.config.items(section)]
    511         except:
    512             if err:
    513                 raise error.general('config: section "%s" not found' % (section))
    514         return []
    515 
    516     def _load(self, name):
    517         #
    518         # Load all the files.
    519         #
    520         self.ini = { 'base'     : path.dirname(name),
    521                      'files'    : [] }
    522         includes = [name]
    523         still_loading = True
    524         while still_loading:
    525             still_loading = False
    526             for include in includes:
    527                 if not path.exists(include):
    528                     rebased_inc = path.join(self.ini['base'],
    529                                             path.basename(include))
    530                     if not path.exists(rebased_inc):
    531                         e = 'config: cannot find configuration: %s' % (include)
    532                         raise error.general(e)
    533                     include = rebased_inc
    534                 if include not in self.ini['files']:
    535                     try:
    536                         self.config.read(include)
    537                     except configparser.ParsingError as ce:
    538                         raise error.general('config: %s' % (ce))
    539                     still_loading = True
    540                     self.ini['files'] += [include]
    541             includes = []
    542             if still_loading:
    543                 for section in self.config.sections():
    544                     includes += self._comma_list(section, 'include', err = False)
    545 
    546458    def _build_options(self, build, nesting = 0):
    547459        if ':' in build:
    548460            section, name = build.split(':', 1)
    549             opts = [self._get_item(section, name)]
     461            opts = [self.config.get_item(section, name)]
    550462            return opts
    551463        builds = self.builds_['builds']
     
    558470            if ':' in option:
    559471                section, name = option.split(':', 1)
    560                 opts = [self._get_item(section, name)]
     472                opts = [self.config.get_item(section, name)]
    561473            else:
    562                 opts = self._options(option, nesting + 1)
     474                opts = self._build_options(option, nesting + 1)
    563475            for opt in opts:
    564476                if opt not in options:
     
    567479
    568480    def load(self, name, build):
    569         self._load(name)
     481        self.config.load(name)
    570482        archs = []
    571         self.profiles['profiles'] = self._comma_list('profiles', 'profiles', err = False)
     483        self.profiles['profiles'] = \
     484            self.config.comma_list('profiles', 'profiles', err = False)
    572485        if len(self.profiles['profiles']) == 0:
    573486            self.profiles['profiles'] = ['tier-%d' % (t) for t in range(1,4)]
     
    575488            profile = {}
    576489            profile['name'] = p
    577             profile['archs'] = self._comma_list(profile['name'], 'archs')
     490            profile['archs'] = self.config.comma_list(profile['name'], 'archs')
    578491            archs += profile['archs']
    579492            for arch in profile['archs']:
    580493                bsps = 'bsps_%s' % (arch)
    581                 profile[bsps] = self._comma_list(profile['name'], bsps)
     494                profile[bsps] = self.config.comma_list(profile['name'], bsps)
    582495            self.profiles[profile['name']] = profile
    583496        for a in set(archs):
    584497            arch = {}
    585498            arch['excludes'] = {}
    586             for exclude in self._comma_list(a, 'exclude', err = False):
     499            for exclude in self.config.comma_list(a, 'exclude', err = False):
    587500                arch['excludes'][exclude] = ['all']
    588             for i in self._get_items(a, False):
     501            for i in self.config.get_items(a, False):
    589502                if i[0].startswith('exclude-'):
    590503                    exclude = i[0][len('exclude-'):]
    591504                    if exclude not in arch['excludes']:
    592505                        arch['excludes'][exclude] = []
    593                     arch['excludes'][exclude] += sorted(set([b.strip() for b in i[1].split(',')]))
    594             arch['bsps'] = self._comma_list(a, 'bsps', err = False)
     506                    arch['excludes'][exclude] += \
     507                        sorted(set([b.strip() for b in i[1].split(',')]))
     508            arch['bsps'] = self.config.comma_list(a, 'bsps', err = False)
    595509            for b in arch['bsps']:
    596510                arch[b] = {}
    597                 arch[b]['bspopts'] = self._comma_list(a, 'bspopts_%s' % (b), err = False)
     511                arch[b]['bspopts'] = \
     512                    self.config.comma_list(a, 'bspopts_%s' % (b), err = False)
    598513            self.archs[a] = arch
    599514        builds = {}
    600         builds['default'] = self._get_item('builds', 'default')
     515        builds['default'] = self.config.get_item('builds', 'default')
    601516        if build is None:
    602517            build = builds['default']
    603518        builds['config'] = { }
    604         for config in self._get_items('config'):
     519        for config in self.config.get_items('config'):
    605520            builds['config'][config[0]] = config[1]
    606521        builds['build'] = build
    607         builds_ = self._get_item_names('builds')
     522        builds_ = self.config.get_item_names('builds')
    608523        builds['builds'] = {}
    609524        for build in builds_:
    610             build_builds = self._comma_list('builds', build)
     525            build_builds = self.config.comma_list('builds', build)
    611526            has_config = False
    612527            has_build = False
     
    682597        s = textbox.line(cols_1, line = '=', marker = '+', indent = 1)
    683598        s1 = ' File(s)'
    684         for f in self.ini['files']:
     599        for f in self.config.files():
    685600            colon = ':'
    686601            for l in textwrap.wrap(f, width = cols_2[1] - 3):
     
    11621077        log.output(command_line())
    11631078
    1164         config = configuration()
     1079        config = configuration_()
    11651080        config.load(config_file, opts.build)
    11661081
Note: See TracChangeset for help on using the changeset viewer.