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

     1  #!/usr/bin/env python
     2  '''
     3  Features to download a file.
     4  
     5  It requires no previous steps.  It provides the 'download_seturl' and
     6  'download' steps
     7  '''
     8  
     9  from waflib.TaskGen import feature
    10  import waflib.Logs as msg
    11  
    12  from orch.util import urlopen
    13  
    14  import orch.features
    15  orch.features.register_defaults(
    16      'dlrun', 
    17      dlrun_urlfile = '{package}-{version}.url',
    18      dlrun_url = None,
    19      dlrun_target = None,
    20      dlrun_checksum = '',
    21      dlrun_dir = '.',
    22      dlrun_cmd_options = '',
    23      dlrun_cmd_target = None,
    24  )
    25  
    26  @feature('dlrun')
    27  def feature_dlrun(tgen):
    28      '''
    29      Download a file.
    30      '''
    31      work_dir = tgen.make_node(tgen.worch.dlrun_dir)
    32  
    33      tgen.step('dlrun_seturl',
    34                rule = "echo '%s' > %s" % (tgen.worch.dlrun_url,
    35                                           tgen.worch.dlrun_urlfile),
    36                update_outputs = True,
    37                target = tgen.worch.dlrun_urlfile)
    38  
    39      def dl_task(task):
    40          src = task.inputs[0]
    41          tgt = task.outputs[0]
    42          url = src.read().strip()
    43          try:
    44              web = urlopen(url)
    45              tgt.write(web.read(),'wb')
    46          except Exception:
    47              import traceback
    48              traceback.print_exc()
    49              msg.error(tgen.worch.format("error downloading {dlrun_url}"))
    50              raise
    51  
    52          checksum = tgen.worch.dlrun_checksum
    53          if not checksum:
    54              return
    55          hasher_name, ref = checksum.split(":")
    56          import hashlib, os
    57          # FIXME: check the hasher method exists. check for typos.
    58          hasher = getattr(hashlib, hasher_name)()
    59          hasher.update(tgt.read('rb'))
    60          data= hasher.hexdigest()
    61          if data != ref:
    62              msg.error(tgen.worch.format("invalid checksum:\nref: %s\nnew: %s" %\
    63                                              (ref, data)))
    64              try:
    65                  os.remove(tgt.abspath())
    66              except IOError: 
    67                  pass
    68              return 1
    69          return
    70  
    71      script = work_dir.make_node(tgen.worch.dlrun_target)
    72      tgen.step('dldownload',
    73                rule = dl_task,
    74                source = tgen.worch.dlrun_urlfile, 
    75                target = script,
    76                cwd = work_dir.abspath())
    77  
    78      cmd_rule = tgen.worch.format('chmod +x {dlrun_cmd} && ./{dlrun_cmd} {dlrun_cmd_options}')
    79      tgen.step('dlcommand',
    80                rule = cmd_rule,
    81                source = script,
    82                target = map(work_dir.make_node, tgen.to_list(tgen.worch.dlrun_cmd_target)),
    83                cwd = work_dir.abspath())
    84      return