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

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