2014-04-23 20:51:22 +02:00
|
|
|
# Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
|
|
|
|
# reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
# can be found in the LICENSE file.
|
|
|
|
|
2015-12-08 20:13:20 +01:00
|
|
|
from optparse import Option, OptionParser, OptionValueError
|
2014-04-23 20:51:22 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
from exec_util import exec_cmd
|
|
|
|
import git_util as git
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
def msg(message):
|
|
|
|
""" Output a message. """
|
|
|
|
sys.stdout.write('--> ' + message + "\n")
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
def warn(message):
|
|
|
|
""" Output a warning. """
|
|
|
|
sys.stdout.write('-' * 80 + "\n")
|
|
|
|
sys.stdout.write('!!!! WARNING: ' + message + "\n")
|
|
|
|
sys.stdout.write('-' * 80 + "\n")
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
def extract_paths(file):
|
|
|
|
""" Extract the list of modified paths from the patch file. """
|
|
|
|
paths = []
|
|
|
|
fp = open(file)
|
|
|
|
for line in fp:
|
|
|
|
if line[:4] != '+++ ':
|
|
|
|
continue
|
|
|
|
match = re.match('^([^\t]+)', line[4:])
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
paths.append(match.group(1).strip())
|
|
|
|
return paths
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
# Cannot be loaded as a module.
|
|
|
|
if __name__ != "__main__":
|
|
|
|
sys.stderr.write('This file cannot be loaded as a module!')
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# Parse command-line options.
|
|
|
|
disc = """
|
|
|
|
This utility updates existing patch files.
|
|
|
|
"""
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2015-12-08 20:13:20 +01:00
|
|
|
# Support options with multiple arguments.
|
|
|
|
class MultipleOption(Option):
|
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
values.ensure_value(dest, []).append(value)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
|
|
|
parser = OptionParser(option_class=MultipleOption, description=disc)
|
|
|
|
parser.add_option(
|
|
|
|
'--resave',
|
|
|
|
action='store_true',
|
|
|
|
dest='resave',
|
|
|
|
default=False,
|
|
|
|
help='re-save existing patch files to pick up manual changes')
|
2018-04-19 17:44:42 +02:00
|
|
|
parser.add_option(
|
|
|
|
'--reapply',
|
|
|
|
action='store_true',
|
|
|
|
dest='reapply',
|
|
|
|
default=False,
|
|
|
|
help='reapply the patch without first reverting changes')
|
2017-05-28 15:04:18 +02:00
|
|
|
parser.add_option(
|
|
|
|
'--revert',
|
|
|
|
action='store_true',
|
|
|
|
dest='revert',
|
|
|
|
default=False,
|
|
|
|
help='revert all changes from existing patch files')
|
|
|
|
parser.add_option(
|
|
|
|
'--patch',
|
|
|
|
action='extend',
|
|
|
|
dest='patch',
|
|
|
|
type='string',
|
|
|
|
default=[],
|
|
|
|
help='optional patch name to process (multiples allowed)')
|
2014-04-23 20:51:22 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2014-08-25 16:01:13 +02:00
|
|
|
if options.resave and options.revert:
|
|
|
|
print 'Invalid combination of options.'
|
|
|
|
parser.print_help(sys.stderr)
|
|
|
|
sys.exit()
|
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
# The CEF root directory is the parent directory of _this_ script.
|
|
|
|
cef_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
|
2017-04-27 03:59:52 +02:00
|
|
|
src_dir = os.path.abspath(os.path.join(cef_dir, os.pardir))
|
2014-04-23 20:51:22 +02:00
|
|
|
|
|
|
|
# Determine the type of Chromium checkout.
|
2015-03-17 00:34:35 +01:00
|
|
|
if not git.is_checkout(src_dir):
|
2014-04-23 20:51:22 +02:00
|
|
|
raise Exception('Not a valid checkout: %s' % src_dir)
|
|
|
|
|
|
|
|
patch_dir = os.path.join(cef_dir, 'patch')
|
|
|
|
patch_cfg = os.path.join(patch_dir, 'patch.cfg')
|
|
|
|
if not os.path.isfile(patch_cfg):
|
|
|
|
raise Exception('File does not exist: %s' % patch_cfg)
|
|
|
|
|
|
|
|
# Read the patch configuration file.
|
|
|
|
msg('Reading patch config %s' % patch_cfg)
|
|
|
|
scope = {}
|
|
|
|
execfile(patch_cfg, scope)
|
|
|
|
patches = scope["patches"]
|
|
|
|
|
|
|
|
# Read each individual patch file.
|
|
|
|
patches_dir = os.path.join(patch_dir, 'patches')
|
|
|
|
for patch in patches:
|
2015-12-08 20:13:20 +01:00
|
|
|
# If specific patch names are specified only process those patches.
|
|
|
|
if options.patch and not patch['name'] in options.patch:
|
|
|
|
continue
|
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
sys.stdout.write('\n')
|
2017-05-28 15:04:18 +02:00
|
|
|
patch_file = os.path.join(patches_dir, patch['name'] + '.patch')
|
2014-04-23 20:51:22 +02:00
|
|
|
|
|
|
|
if os.path.isfile(patch_file):
|
|
|
|
msg('Reading patch file %s' % patch_file)
|
2017-04-27 03:59:52 +02:00
|
|
|
if 'path' in patch:
|
|
|
|
patch_root_abs = os.path.abspath(os.path.join(src_dir, patch['path']))
|
|
|
|
else:
|
|
|
|
patch_root_abs = src_dir
|
2014-04-23 20:51:22 +02:00
|
|
|
|
|
|
|
# Retrieve the list of paths modified by the patch file.
|
|
|
|
patch_paths = extract_paths(patch_file)
|
|
|
|
|
2014-09-09 16:51:22 +02:00
|
|
|
# List of paths added by the patch file.
|
|
|
|
added_paths = []
|
|
|
|
|
2014-04-23 20:51:22 +02:00
|
|
|
if not options.resave:
|
2018-04-19 17:44:42 +02:00
|
|
|
if not options.reapply:
|
|
|
|
# Revert any changes to existing files in the patch.
|
|
|
|
for patch_path in patch_paths:
|
|
|
|
patch_path_abs = os.path.abspath(os.path.join(patch_root_abs, \
|
|
|
|
patch_path))
|
|
|
|
if os.path.exists(patch_path_abs):
|
|
|
|
msg('Reverting changes to %s' % patch_path_abs)
|
|
|
|
cmd = 'git checkout -- %s' % (patch_path_abs)
|
|
|
|
result = exec_cmd(cmd, patch_root_abs)
|
|
|
|
if result['err'] != '':
|
|
|
|
msg('Failed to revert file: %s' % result['err'])
|
|
|
|
msg('Deleting file %s' % patch_path_abs)
|
|
|
|
os.remove(patch_path_abs)
|
|
|
|
added_paths.append(patch_path_abs)
|
|
|
|
if result['out'] != '':
|
|
|
|
sys.stdout.write(result['out'])
|
|
|
|
else:
|
|
|
|
msg('Skipping non-existing file %s' % patch_path_abs)
|
2014-09-09 16:51:22 +02:00
|
|
|
added_paths.append(patch_path_abs)
|
2014-04-23 20:51:22 +02:00
|
|
|
|
2014-08-25 16:01:13 +02:00
|
|
|
if not options.revert:
|
|
|
|
# Apply the patch file.
|
|
|
|
msg('Applying patch to %s' % patch_root_abs)
|
2017-04-27 03:59:52 +02:00
|
|
|
patch_string = open(patch_file, 'rb').read()
|
|
|
|
result = exec_cmd('patch -p0', patch_root_abs, patch_string)
|
2014-08-25 16:01:13 +02:00
|
|
|
if result['err'] != '':
|
|
|
|
raise Exception('Failed to apply patch file: %s' % result['err'])
|
|
|
|
sys.stdout.write(result['out'])
|
|
|
|
if result['out'].find('FAILED') != -1:
|
|
|
|
warn('Failed to apply %s, fix manually and run with --resave' % \
|
|
|
|
patch['name'])
|
|
|
|
continue
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
if not options.revert and not options.reapply:
|
2014-08-25 16:01:13 +02:00
|
|
|
msg('Saving changes to %s' % patch_file)
|
2015-03-17 00:34:35 +01:00
|
|
|
if added_paths:
|
2014-09-09 16:51:22 +02:00
|
|
|
# Inform git of the added paths so they appear in the patch file.
|
|
|
|
cmd = 'git add -N %s' % ' '.join(added_paths)
|
|
|
|
result = exec_cmd(cmd, patch_root_abs)
|
|
|
|
if result['err'] != '' and result['err'].find('warning:') != 0:
|
|
|
|
raise Exception('Failed to add paths: %s' % result['err'])
|
|
|
|
|
|
|
|
# Re-create the patch file.
|
2014-08-25 16:01:13 +02:00
|
|
|
patch_paths_str = ' '.join(patch_paths)
|
2018-04-19 17:44:42 +02:00
|
|
|
cmd = 'git diff --no-prefix --relative --ignore-space-at-eol %s' % patch_paths_str
|
2014-08-25 16:01:13 +02:00
|
|
|
result = exec_cmd(cmd, patch_root_abs)
|
|
|
|
if result['err'] != '' and result['err'].find('warning:') != 0:
|
|
|
|
raise Exception('Failed to create patch file: %s' % result['err'])
|
|
|
|
f = open(patch_file, 'wb')
|
|
|
|
f.write(result['out'])
|
|
|
|
f.close()
|
2014-04-23 20:51:22 +02:00
|
|
|
else:
|
|
|
|
raise Exception('Patch file does not exist: %s' % patch_file)
|