| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import optparse | 6 import optparse |
| 7 import subprocess | 7 import subprocess |
| 8 import sys | 8 import sys |
| 9 | 9 |
| 10 | 10 |
| 11 def main(): | 11 def main(): |
| 12 parser = optparse.OptionParser() | 12 parser = optparse.OptionParser() |
| 13 parser.add_option('-p', '--patch-url', | 13 parser.add_option('-p', '--patch-url', |
| 14 help='The SVN URL to download the patch from.') | 14 help='The SVN URL to download the patch from.') |
| 15 parser.add_option('-r', '--root-dir', | 15 parser.add_option('-r', '--root-dir', |
| 16 help='The root dir in which to apply patch.') | 16 help='The root dir in which to apply patch.') |
| 17 parser.add_option('', '--path-filter-script', |
| 18 help=('A script that filter out contents of a patch. The ' |
| 19 'script must use stdin for input and stdout for ' |
| 20 'output.')) |
| 21 parser.add_option('', '--path-filter', |
| 22 help=('The paths that shall be allowed (must specify the ' |
| 23 'path filter script to use this).')) |
| 24 parser.add_option('', '--strip-level', type='int', default=0, |
| 25 help=('The number of components to be stripped from the ' |
| 26 'filenames in the patch. Default: %default.')) |
| 17 | 27 |
| 18 options, args = parser.parse_args() | 28 options, args = parser.parse_args() |
| 19 if args: | 29 if args: |
| 20 parser.error('Unused args: %s' % args) | 30 parser.error('Unused args: %s' % args) |
| 21 if not (options.patch_url and options.root_dir): | 31 if not (options.patch_url and options.root_dir): |
| 22 parser.error('A patch URL and root directory should be specified.') | 32 parser.error('A patch URL and root directory should be specified.') |
| 33 if ((options.path_filter_script and not options.path_filter) or |
| 34 (options.path_filter and not options.path_filter_script)): |
| 35 parser.error('You must specify both path filter and a path filter script ' |
| 36 'in order to use path filtering for patches.') |
| 23 | 37 |
| 24 svn_cat = subprocess.Popen(['svn', 'cat', options.patch_url], | 38 svn_cat = subprocess.Popen(['svn', 'cat', options.patch_url], |
| 25 stdout=subprocess.PIPE) | 39 stdout=subprocess.PIPE) |
| 26 patch = subprocess.Popen(['patch', '-t', '-p', '0', '-d', options.root_dir], | 40 patch_input = svn_cat.stdout |
| 27 stdin=svn_cat.stdout) | 41 if options.path_filter_script: |
| 42 filtering = subprocess.Popen(sys.executable, [options.path_filter_script, |
| 43 '-f', options.path_filter], |
| 44 stdin=svn_cat.stdout, stdout=subprocess.PIPE) |
| 45 patch_input = filtering.stdout |
| 46 patch = subprocess.Popen(['patch', '-t', '-p', options.strip_level, |
| 47 '-d', options.root_dir], |
| 48 stdin=patch_input) |
| 28 | 49 |
| 29 _, err = patch.communicate() | 50 _, err = patch.communicate() |
| 30 return err or None | 51 return err or None |
| 31 | 52 |
| 32 | 53 |
| 33 if __name__ == '__main__': | 54 if __name__ == '__main__': |
| 34 sys.exit(main()) | 55 sys.exit(main()) |
| OLD | NEW |