Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Script that can be used to filter out files from a patch/diff. | |
| 7 | |
| 8 Just pipe the patch contents to stdin and the filtered output will be written | |
| 9 to stdout. | |
| 10 | |
| 11 This script supports the following types of diff entries where each file entry | |
| 12 starts with the following four lines: | |
| 13 | |
| 14 * Subversion patch: | |
| 15 Index: path_and_filename | |
| 16 =================================================================== | |
| 17 --- path_and_filename (revision xxxx) | |
| 18 +++ path_and_filename (working copy) | |
| 19 | |
| 20 | |
| 21 * Git patch: | |
| 22 diff --git path_and_filename path_and_filename | |
| 23 index abcdef..123456 100644 | |
| 24 --- path_and_filename | |
| 25 +++ path_and_filename | |
| 26 """ | |
| 27 | |
| 28 import optparse | |
| 29 import re | |
| 30 import sys | |
| 31 | |
| 32 | |
| 33 class PatchEntry(): | |
| 34 def __init__(self, file_path, contents): | |
| 35 self.file_path = file_path | |
| 36 self.contents = contents | |
| 37 | |
| 38 def __str__(self): | |
| 39 return self.contents | |
| 40 | |
| 41 | |
| 42 GIT_REGEX = ('^diff\s--git\s(.*?)\s(.*?)\n' | |
| 43 '^.*?\n' | |
| 44 '^-{3}\s(.*?)\n' | |
| 45 '^\+{3}\s(.*?)$') | |
|
iannucci
2013/10/29 17:15:02
I would use a raw string here (i.e. r' stuff '). T
kjellander_chromium
2013/11/26 22:11:25
This is remade in Patch set #3.
| |
| 46 SVN_REGEX = ('^Index:\s(.*?)\n' | |
| 47 '^.*?\n' | |
| 48 '^-{3}\s.*?\n' | |
| 49 '^\+{3}\s.*?$') | |
| 50 ITEM_PREFIX_TYPES = (GIT_REGEX, SVN_REGEX) | |
| 51 | |
| 52 def parse_entries(item_prefix_regex, patch_contents): | |
| 53 regex = re.compile(item_prefix_regex, re.MULTILINE | re.DOTALL) | |
| 54 | |
| 55 # List to contain tuples of filename and start position in the patch. | |
| 56 file_and_start_index_list = [] | |
| 57 | |
| 58 start_index = 0 | |
| 59 match_data = regex.search(patch_contents[start_index:]) | |
| 60 while match_data: | |
| 61 file_and_start_index_list.append((match_data.group(1), | |
| 62 start_index + match_data.start())) | |
| 63 start_index += match_data.end() | |
| 64 match_data = regex.search(patch_contents[start_index:]) | |
| 65 | |
| 66 # Build a PatchEntry list from the list of filename and start indexes. | |
| 67 entries = [] | |
| 68 i = iter(file_and_start_index_list) | |
| 69 previous_item = None | |
| 70 try: | |
| 71 while True: | |
| 72 item = i.next() | |
| 73 if previous_item: | |
| 74 entries.append(PatchEntry(previous_item[0], | |
| 75 patch_contents[previous_item[1]:item[1]])) | |
| 76 previous_item = item | |
| 77 except StopIteration: | |
| 78 if previous_item: | |
| 79 entries.append(PatchEntry(previous_item[0], | |
| 80 patch_contents[previous_item[1]:])) | |
| 81 return entries | |
| 82 | |
| 83 def main(): | |
| 84 parser = optparse.OptionParser() | |
| 85 parser.add_option('-f', '--path-filter', | |
| 86 help=('The path filter (UNIX paths) that all file paths ' | |
| 87 'are required to have to pass this filter (no ' | |
| 88 'regexp).')) | |
| 89 | |
| 90 options, args = parser.parse_args() | |
| 91 if args: | |
| 92 parser.error('Unused args: %s' % args) | |
| 93 if not options.path_filter: | |
| 94 parser.error('A path filter must be be specified.') | |
| 95 | |
| 96 patch_contents = sys.stdin.read() | |
| 97 | |
| 98 # Try with Git first, since it's the most likely used patch format. | |
| 99 patch_entries = parse_entries(GIT_REGEX, patch_contents) | |
| 100 if len(patch_entries) == 0: | |
| 101 patch_entries = parse_entries(SVN_REGEX, patch_contents) | |
| 102 | |
| 103 # Only print the patch entries that passes our path filter. | |
| 104 for entry in patch_entries: | |
| 105 if entry.file_path.startswith(options.path_filter): | |
| 106 print entry | |
| 107 | |
| 108 if __name__ == '__main__': | |
| 109 sys.exit(main()) | |
| OLD | NEW |