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

     1  #!/usr/bin/env python
     2  # encoding: utf-8
     3  
     4  ## stdlib imports
     5  import os
     6  from glob import glob
     7  
     8  ## 3rd party
     9  from . import pkgconf
    10  from . import envmunge
    11  from . import features as featmod
    12  from . import util
    13  
    14  ## waf imports
    15  import waflib.Logs as msg
    16  import waflib.Context as context
    17  
    18  # NOT from the waf book.  The waf book example for depends_on doesn't work
    19  # from waflib import TaskGen
    20  # @TaskGen.feature('*') 
    21  # @TaskGen.before_method('process_rule')
    22  # def post_the_other(self):
    23  #     deps = getattr(self, 'depends_on', []) 
    24  #     for name in self.to_list(deps):
    25  #         msg.debug('orch: DEPENDS_ON: %s %s' % ( self.name, name ))
    26  #         other = self.bld.get_tgen_by_name(name) 
    27  #         other.post()
    28  #         for ot in other.tasks:
    29  #             msg.debug('orch: OTHER TASK: %s before: %s' % (ot, ot.before))
    30  #             ot.before.append(self.name)
    31  
    32  
    33  # waf entries
    34  def options(opt):
    35      opt.add_option('--orch-config', action = 'store', default = 'orch.cfg',
    36                     help='Give an orchestration configuration file.')
    37      opt.add_option('--orch-start', action = 'store', default = 'start',
    38                     help='Set the section to start the orchestration')
    39  
    40  def bind_functions(ctx):
    41      from pprint import PrettyPrinter
    42      pp = PrettyPrinter(indent=2)
    43      ctx.orch_dump = lambda : pp.pprint({'packages': ctx.env.orch_package_list,
    44                                          'groups': ctx.env.orch_group_list})
    45  
    46  
    47  def configure(cfg):
    48      msg.debug('orch: CONFIG CALLED')
    49  
    50      if not cfg.options.orch_config:
    51          raise RuntimeError('No Orchestration configuration file given (--orch-config)')
    52      orch_config = []
    53      for lst in util.string2list(cfg.options.orch_config):
    54          lst = lst.strip()
    55          orch_config += glob(lst)
    56      okay = True
    57      for maybe in orch_config:
    58          if os.path.exists(maybe):
    59              continue
    60          msg.error('No such file: %s' % maybe)
    61          okay = False
    62      if not okay or not orch_config:
    63          raise ValueError('missing configuration files')
    64              
    65      cfg.msg('Orch configuration files', '"%s"' % '", "'.join(orch_config))
    66  
    67      extra = dict(cfg.env)
    68      extra['top'] = cfg.path.abspath()
    69      out = cfg.bldnode.abspath() # usually {top}/tmp
    70      assert out, 'No out dir defined'
    71      extra['out'] = out
    72      extra['DESTDIR'] = getattr(cfg.options, 'destdir', '')
    73      msg.debug('top="{top}" out="{out}" DESTDIR="{DESTDIR}"'.format(**extra))
    74      suite = pkgconf.load(orch_config, start = cfg.options.orch_start, **extra)
    75  
    76      envmunge.decompose(cfg, suite)
    77  
    78      cfg.msg('Orch configure envs', '"%s"' % '", "'.join(cfg.all_envs.keys()))
    79      bind_functions(cfg)
    80      return
    81  
    82  def build(bld):
    83      msg.debug ('orch: BUILD CALLED')
    84  
    85      bind_functions(bld)
    86  
    87      import orch.features
    88      feature_funcs, feature_configs = orch.features.load()
    89      msg.info('Supported features: "%s"' % '", "'.join(sorted(feature_funcs.keys())))
    90  
    91      msg.debug('orch: Build envs: %s' % ', '.join(sorted(bld.all_envs.keys())))
    92  
    93      pfi_list = list()
    94      to_recurse = []
    95  
    96      for grpname in bld.env.orch_group_list:
    97          msg.debug('orch: Adding group: "%s"' % grpname)
    98          bld.add_group(grpname)
    99          group = bld.env.orch_group_dict[grpname]
   100          for package in group['packages']:
   101              pkgname = package['package']
   102  
   103              # delegate package to another wscript file?
   104              other_wscript = os.path.join(bld.launch_dir, pkgname, 'wscript')
   105              if os.path.exists(other_wscript):
   106                  msg.info('orch: delegating to %s' % other_wscript)
   107                  to_recurse.append(pkgname)
   108                  continue
   109  
   110              pkgcfg = bld.env.orch_package_dict[pkgname]
   111              featlist = util.string2list(pkgcfg.get('features'))
   112              msg.debug('orch: features for %s: "%s"' % (pkgname, '", "'.join(featlist)))
   113              for feat in featlist:
   114                  try:
   115                      feat_func = feature_funcs[feat]
   116                  except KeyError:
   117                      msg.error('No method for feature: "%s", package: "%s"'%(feat,pkgname))
   118                      raise
   119                  msg.debug('orch: feature: "%s" for package: "%s"' % (feat, pkgname))
   120                  pfi = feat_func(bld, pkgcfg)
   121                  pfi_list.append(pfi)
   122  
   123      if to_recurse:
   124          bld.recurse(to_recurse)
   125  
   126      for pfi in pfi_list:
   127          pfi.register_dependencies()
   128  
   129      msg.debug ('orch: BUILD CALLED [done]')