Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Unified Diff: scripts/slave/patch_path_filter.py

Issue 27575002: Patch path filtering script. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: Now using patch.py and its test data Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: scripts/slave/patch_path_filter.py
diff --git a/scripts/slave/patch_path_filter.py b/scripts/slave/patch_path_filter.py
new file mode 100755
index 0000000000000000000000000000000000000000..24b7281bf05b9ed2ad88aba08ee688569fe5be18
--- /dev/null
+++ b/scripts/slave/patch_path_filter.py
@@ -0,0 +1,97 @@
+#!/usr/bin/python
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Script that can be used to filter out files from a patch/diff.
+
+Just pipe the patch contents to stdin and the filtered output will be written
+to stdout.
+"""
+
+import optparse
+import os
+import re
+import sys
+
+from depot_tools import patch
+
+_GIT_PREFIX = 'diff --git '
+_SVN_PREFIX = 'Index: '
+
+# The Git patches generated from depot_tools/git_cl.py has the a/ prefix for
+# the source files stripped out. That's why it's optional non-capturing group
+# below (to support both scenarios).
+_GIT_FILENAME_REGEX = r'^diff \-\-git (?:a/)?(.*?) .*\n$'
+_SVN_FILENAME_REGEX = r'^Index: ([^\t]+).*\n$'
+
+
+def parse_git_patch_set(patch_contents):
+ return _parse_patch_set(_GIT_PREFIX, _GIT_FILENAME_REGEX, patch_contents)
+
+def parse_svn_patch_set(patch_contents):
+ return _parse_patch_set(_SVN_PREFIX, _SVN_FILENAME_REGEX, patch_contents)
+
+def _parse_patch_set(prefix, filename_pattern, patch_contents):
+ # Parse into chunks using the prefix.
+ patch_chunks = []
+ current_chunk = []
+ for line in patch_contents.splitlines(True):
+ if line.startswith(prefix) and current_chunk:
+ patch_chunks.insert(0, current_chunk)
+ current_chunk = [line]
+ else:
+ current_chunk.append(line)
+ if current_chunk:
+ patch_chunks.insert(0, current_chunk)
+
+ # Parse filename for each patch chunk and create FilePatchDiff objects
+ filename_regex = re.compile(filename_pattern)
+ patches = []
+ for chunk in patch_chunks:
+ match = filename_regex.match(chunk[0])
+ if not match:
+ raise Exception('Did not find any filename in %s' % chunk[0])
+ filename = match.group(1).replace('\\', '/')
+ diff = ''.join(chunk)
+ patches.append(patch.FilePatchDiff(filename=filename, diff=diff,
+ svn_properties=[]))
+ return patch.PatchSet(patches)
+
+def main():
+ usage = '%s -f <path-filter> [-r <root-dir>]' % os.path.basename(sys.argv[0])
+ parser = optparse.OptionParser(usage=usage)
+ parser.add_option('-f', '--path-filter',
+ help=('The path filter (UNIX paths) that all file paths '
+ 'are required to have to pass this filter (no '
+ 'regexp).'))
+ parser.add_option('-r', '--root-dir',
+ help=('The patch root dir in which to apply the patch. If '
+ 'specified, it will be prepended to the filename '
+ 'for each patch entry before the filter is applied.'))
+
+ options, args = parser.parse_args()
+ if args:
+ parser.error('Unused args: %s' % args)
+ if not options.path_filter:
+ parser.error('A path filter must be be specified.')
+
+ patch_contents = sys.stdin.read()
+
+ # Find out if it's a Git or Subversion patch set.
+ is_git = any(l.startswith(_GIT_PREFIX) for l in patch_contents.splitlines())
kjellander_chromium 2013/12/10 20:46:02 Turns out this is actually not needed if we assume
+ if is_git:
+ patchset = parse_git_patch_set(patch_contents)
+ else:
+ patchset = parse_svn_patch_set(patch_contents)
+
+ # Only print the patch entries that passes our path filter.
+ for patch_entry in patchset:
+ filename = patch_entry.filename
+ if options.root_dir:
+ filename = os.path.join(options.root_dir, filename)
+ if filename.startswith(options.path_filter):
+ print patch_entry.get(for_git=is_git)
+
+if __name__ == '__main__':
+ sys.exit(main())

Powered by Google App Engine
This is Rietveld 408576698