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

     1  #!/usr/bin/env python
     2  '''
     3  Features to apply a patch to an unpacked source directory.
     4  
     5  It requires the 'unpack' step and provides the 'patch' step.
     6  '''
     7  from waflib.TaskGen import feature
     8  import waflib.Logs as msg
     9  
    10  from orch.util import urlopen, get_unpacker
    11  from orch.wafutil import exec_command
    12  
    13  import orch.features
    14  orch.features.register_defaults(
    15      'patch',
    16      patch_urlfile = '{urlfile_dir}/{package}-{version}.patch.url',
    17      patch_file = '{package}-{version}.patch',
    18      patch_file_path = '{patch_dir}/{patch_file}',
    19      patch_checksum = '',
    20      patch_cmd = 'patch -i',
    21      patch_cmd_std_opts = '',
    22      patch_cmd_options = '',
    23      )
    24  
    25  @feature('patch')
    26  def feature_patch(tgen):
    27      '''
    28      Apply a patch
    29      '''
    30      tgen.step('urlpatch',
    31                rule = "echo '%s' > %s" % (tgen.worch.patch_url, tgen.worch.patch_urlfile),
    32                update_outputs = True,
    33                target = tgen.worch.patch_urlfile)
    34  
    35      # fixme: this is mostly a cut-and-paste from feature_tarball.
    36      # Both should be factored out to common spot....
    37      def dl_task(task):
    38          src = task.inputs[0]
    39          tgt = task.outputs[0]
    40          url = src.read().strip()
    41          try:
    42              web = urlopen(url)
    43              tgt.write(web.read(),'wb')
    44          except Exception:
    45              import traceback
    46              traceback.print_exc()
    47              msg.error(tgen.worch.format("[{package}_dlpatch] problem downloading [{patch_urlfile}]"))
    48              raise
    49  
    50          checksum = tgen.worch.patch_checksum
    51          if not checksum:
    52              return
    53          hasher_name, ref = checksum.split(":")
    54          import hashlib, os
    55          # FIXME: check the hasher method exists. check for typos.
    56          hasher = getattr(hashlib, hasher_name)()
    57          hasher.update(tgt.read('rb'))
    58          data= hasher.hexdigest()
    59          if data != ref:
    60              msg.error(tgen.worch.format("[{package}_dlpatch] invalid checksum:\nref: %s\nnew: %s" %\
    61                                          (ref, data)))
    62              try:
    63                  os.remove(tgt.abspath())
    64              except IOError: 
    65                  pass
    66              return 1
    67          return
    68      tgen.step('dlpatch',
    69                rule = dl_task,
    70                source = tgen.worch.patch_urlfile,
    71                target = tgen.worch.patch_file)
    72  
    73      def apply_patch(task):
    74          src = task.inputs[0].abspath()
    75          tgt = task.outputs[0].abspath()
    76          cmd = "%s %s %s" % ( tgen.worch.patch_cmd, src, tgen.worch.patch_cmd_options )
    77          ret = exec_command(task, cmd)
    78          return ret
    79  
    80      after =  tgen.worch.package + '_unpack'
    81      before = tgen.worch.package + '_prepare'
    82  
    83      tgen.step('patch',
    84                rule = apply_patch,
    85                source = [tgen.worch.patch_file, tgen.control_node('unpack')],
    86                target = [tgen.control_node('patch')],
    87                depends_on = [after],
    88                after = [after], before = [before])
    89  
    90