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

Unified Diff: tools/traffic_annotation/auditor/traffic_annotation_file_filter.py

Issue 2905263002: Filter added to prune files before applying network annotation extractor. (Closed)
Patch Set: run_tool.py reverted. Created 3 years, 7 months 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
« no previous file with comments | « tools/traffic_annotation/auditor/traffic_annotation_auditor.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/traffic_annotation/auditor/traffic_annotation_file_filter.py
diff --git a/tools/traffic_annotation/auditor/traffic_annotation_file_filter.py b/tools/traffic_annotation/auditor/traffic_annotation_file_filter.py
new file mode 100755
index 0000000000000000000000000000000000000000..f00daf14bfbedee93dd63026be4fa9e4241a3b89
--- /dev/null
+++ b/tools/traffic_annotation/auditor/traffic_annotation_file_filter.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+# Copyright 2017 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.
+
+import os.path
+import re
+import subprocess
+import sys
+
+
+def _GetFilesFromGit():
+ """Gets the list of files in the git repository."""
+ args = []
+ if sys.platform == 'win32':
+ args.append('git.bat')
+ else:
+ args.append('git')
+ args.append('ls-files')
+ command = subprocess.Popen(args, stdout=subprocess.PIPE)
+ output, _ = command.communicate()
+ return [os.path.realpath(p) for p in output.splitlines()]
+
+
+class TrafficAnnotationFileFilter():
+ KEYWORDS = [
+ 'network_traffic_annotation',
+ 'network_traffic_annotation_test_helper',
+ 'NetworkTrafficAnnotationTag',
+ 'PartialNetworkTrafficAnnotationTag',
+ 'DefineNetworkTrafficAnnotation',
+ 'DefinePartialNetworkTrafficAnnotation',
+ 'CompleteNetworkTrafficAnnotation',
+ 'BranchedCompleteNetworkTrafficAnnotation',
+ 'NO_TRAFFIC_ANNOTATION_YET',
+ 'NO_PARTIAL_TRAFFIC_ANNOTATION_YET',
+ 'MISSING_TRAFFIC_ANNOTATION',
+ 'TRAFFIC_ANNOTATION_FOR_TESTS',
+ 'PARTIAL_TRAFFIC_ANNOTATION_FOR_TESTS',
+ 'SSLClientSocket', # SSLClientSocket::
+ 'TCPClientSocket', # TCPClientSocket::
+ 'UDPClientSocket', # UDPClientSocket::
+ 'URLFetcher::Create', # This one is used with class as it's too generic.
+ 'CreateDatagramClientSocket', # ClientSocketFactory::
+ 'CreateSSLClientSocket', # ClientSocketFactory::
+ 'CreateTransportClientSocket', # ClientSocketFactory::
+ 'CreateRequest', # URLRequestContext::
+ ]
+
+ def __init__(self,
+ skip_tests=True):
+ """Creates a new TrafficAnnotationFileFilter.
+
+ Args:
+ skip_tests: bool Flag stating if test files should be returned or not.
+ """
+ assert(all(re.match('^[A-Za-z:_]+$', keyword) for keyword in self.KEYWORDS))
+ self.content_matcher = re.compile('.*(' + '|'.join(self.KEYWORDS) + ').*')
+ self.file_name_matcher = re.compile(
+ '^(?!.*?test)^.*(\.cc|\.mm)$' if skip_tests else
+ '^.*(\.cc|\.mm)$')
+ self.git_files = filter(lambda x: self.FileIsRelevantContent(x),
+ _GetFilesFromGit())
+
+ def FileIsRelevantContent(self, filename):
+ if self.file_name_matcher.match(filename):
+ with open(filename, 'r') as in_file:
+ for line in in_file:
+ if self.content_matcher.match(line):
+ return True
+ return False
+
+
+ def GetFilteredFilesList(self, dir_name='/'):
+ """Returns the list of relevant files in given directory.
+ Args:
+ dir_name: str The directory to search for relevant files, e.g.
+ 'chrome/browser'. All child directories would also be searched.
+
+ Returns:
+ list of str List of relevant files
+ """
+ matcher = re.compile(os.path.abspath(dir_name) + '/.*')
+ return filter(matcher.match, self.git_files)
« no previous file with comments | « tools/traffic_annotation/auditor/traffic_annotation_auditor.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698