1# Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights 2# reserved. Use of this source code is governed by a BSD-style license that 3# can be found in the LICENSE file. 4 5from __future__ import absolute_import 6import pickle 7from optparse import OptionParser 8import os 9import sys 10from file_util import * 11from git_util import git_apply_patch_file 12 13# Cannot be loaded as a module. 14if __name__ != "__main__": 15 sys.stdout.write('This file cannot be loaded as a module!') 16 sys.exit() 17 18# The CEF root directory is the parent directory of _this_ script. 19cef_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) 20cef_patch_dir = os.path.join(cef_dir, 'patch') 21src_dir = os.path.abspath(os.path.join(cef_dir, os.pardir)) 22 23 24def write_note(type, note): 25 separator = '-' * 79 + '\n' 26 sys.stdout.write(separator) 27 sys.stdout.write('!!!! %s: %s\n' % (type, note)) 28 sys.stdout.write(separator) 29 30 31def apply_patch_file(patch_file, patch_dir): 32 ''' Apply a specific patch file in optional patch directory. ''' 33 patch_path = os.path.join(cef_patch_dir, 'patches', patch_file + '.patch') 34 35 if patch_dir is None or len(patch_dir) == 0: 36 patch_dir = src_dir 37 else: 38 if not os.path.isabs(patch_dir): 39 # Apply patch relative to the Chromium 'src' directory. 40 patch_dir = os.path.join(src_dir, patch_dir) 41 patch_dir = os.path.abspath(patch_dir) 42 43 result = git_apply_patch_file(patch_path, patch_dir) 44 if result == 'fail': 45 write_note('ERROR', 46 'This patch failed to apply. Your build will not be correct.') 47 return result 48 49 50def apply_patch_config(): 51 ''' Apply patch files based on a configuration file. ''' 52 config_file = os.path.join(cef_patch_dir, 'patch.cfg') 53 if not os.path.isfile(config_file): 54 raise Exception('Patch config file %s does not exist.' % config_file) 55 56 # Parse the configuration file. 57 scope = {} 58 exec (compile(open(config_file, "rb").read(), config_file, 'exec'), scope) 59 patches = scope["patches"] 60 61 results = {'apply': 0, 'skip': 0, 'fail': 0} 62 63 for patch in patches: 64 patch_file = patch['name'] 65 dopatch = True 66 67 if 'condition' in patch: 68 # Check that the environment variable is set. 69 if patch['condition'] not in os.environ: 70 sys.stdout.write('\nSkipping patch file %s\n' % patch_file) 71 dopatch = False 72 73 if dopatch: 74 result = apply_patch_file(patch_file, patch['path'] 75 if 'path' in patch else None) 76 results[result] += 1 77 78 if 'note' in patch: 79 write_note('NOTE', patch['note']) 80 else: 81 results['skip'] += 1 82 83 sys.stdout.write('\n%d patches total (%d applied, %d skipped, %d failed)\n' % \ 84 (len(patches), results['apply'], results['skip'], results['fail'])) 85 86 if results['fail'] > 0: 87 sys.stdout.write('\n') 88 write_note('ERROR', 89 '%d patches failed to apply. Your build will not be correct.' % 90 results['fail']) 91 sys.exit(1) 92 93 94# Parse command-line options. 95disc = """ 96This utility applies patch files. 97""" 98 99parser = OptionParser(description=disc) 100parser.add_option( 101 '--patch-file', dest='patchfile', metavar='FILE', help='patch source file') 102parser.add_option( 103 '--patch-dir', 104 dest='patchdir', 105 metavar='DIR', 106 help='patch target directory') 107(options, args) = parser.parse_args() 108 109if not options.patchfile is None: 110 apply_patch_file(options.patchfile, options.patchdir) 111else: 112 apply_patch_config() 113