github.com/hwaf/hwaf@v0.0.0-20140814122253-5465f73b20f1/py-hwaftools/orch/envmunge.py (about)

     1  #!/usr/bin/env python
     2  '''
     3  
     4  This module primarily provides the decompose() function.
     5  
     6  '''
     7  
     8  from . import mungers
     9  
    10  
    11  def packages_in_group(pkglist, group_name):
    12      '''
    13      Given a list of all packages, return a list of those in the named group.
    14      '''
    15      ret = []
    16      for pkg in pkglist:
    17          if pkg.get('group') == group_name:
    18              ret.append(pkg)
    19      return ret
    20  
    21  def resolve_packages(all_packages, desclist):
    22      '''Resolve packages given a description list.  Each element of the
    23      list is like <what>:<name> where <what> is "group" or "package"
    24      and <name> is the group or package name.  The list can be in the
    25      form of a sequence of descriptors or as a single comma-separated
    26      string.
    27      '''
    28      if not desclist:
    29          return list()
    30  
    31      if isinstance(desclist, type("")):
    32          desclist = [x.strip() for x in desclist.split(',')]
    33      ret = []
    34  
    35      for req in desclist:
    36          what,name = req.split(':')
    37          if what == 'package':
    38              pkg = all_packages[name]
    39              ret.append(pkg)
    40              continue
    41          if what == 'group':
    42              for pkg in packages_in_group(all_packages.values(), name):
    43                  if pkg in ret:
    44                      continue
    45                  ret.append(pkg)
    46          else:
    47              raise ValueError('Unknown descriptor: "%s:%s"' % (what, name))
    48          continue
    49      return ret
    50  
    51          
    52  def make_envmungers(pkg, all_packages):
    53      '''Make a environment munger that will apply the export_VARIABLE
    54      settings from all dependency packages indicated by the
    55      "environment" package variable (and the export_VAR from 'depends'
    56      packages) and any specified by buildenv_VARIABLE in the package
    57      itself.  Note, that the export_* variables from a given package
    58      are explicitly NOT applied to the package itself.
    59      '''
    60      autoenv = []
    61      deps = pkg.get('depends') or []
    62      if isinstance(deps, type("")): deps = deps.split()
    63      
    64      for dep in deps:
    65          _, step = dep.split(':')
    66          what = step.split('_')[0]
    67          if what in all_packages:
    68              #print 'autoenv: package: "%s"' % what
    69              autoenv.append('package:%s' % what)
    70          else: # FIXME: assume this is a group, then.
    71              #print 'autoenv: group: "%s"' % what
    72              autoenv.append('group:%s' % what)
    73  
    74      if pkg.get('environment'):
    75          en = pkg.get('environment')
    76          #print 'autoenv: environment: "%s"' % en
    77          autoenv.extend([x.strip() for x in en.split(',')])
    78          
    79      ret = list()
    80      for other_pkg in resolve_packages(all_packages, autoenv):
    81          ret += mungers.construct('export_', **other_pkg)
    82  
    83      ret += mungers.construct('buildenv_', **pkg)
    84  
    85      # Do NOT append export_ mungers for the current pkg.
    86  
    87      return ret
    88  
    89  def decompose(cfg, suite):
    90      '''Decompose suite into packages and groups of packages.  
    91  
    92      For every group in the suite there is one added to the waf <cfg> context.  
    93  
    94      Every package has an env of the same name added to <cfg> which
    95      contains variables defined either through its "environment"
    96      variable or through any variables with names beginning with
    97      "buildenv_".
    98  
    99      The waf env is given a "munged_env" element which holds the result
   100      of any environment munging applied to the current process
   101      environment (os.environ).
   102      '''
   103      base_env = cfg.env
   104  
   105      # fixme: should use ordered dict
   106      gl,gd = [], {}
   107      pl,pd = [], {}
   108      for group in suite['groups']:
   109          group_name = group['group']
   110          gl.append(group_name)
   111          gd[group_name] = group
   112          for package in group['packages']:
   113              package_name = package['package']
   114              pl.append(package_name)
   115              pd[package_name] = package
   116  
   117      base_env.orch_group_list = gl
   118      base_env.orch_group_dict = gd
   119      base_env.orch_package_list = pl
   120      base_env.orch_package_dict = pd
   121  
   122      for pkg_name, pkg in pd.items():
   123  
   124          mlist = make_envmungers(pkg, pd)
   125          environ = cfg.env.env or dict()
   126          menv = mungers.apply(mlist, **environ)
   127  
   128          new_env = base_env.derive()
   129          new_env.munged_env = menv
   130          cfg.setenv(pkg_name, new_env)
   131  
   132      return
   133