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

Side by Side Diff: tools/traffic_annotation/auditor/annotation_relevent_filter.py

Issue 2905263002: Filter added to prune files before applying network annotation extractor. (Closed)
Patch Set: Created 3 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2017 The Chromium Authors. All rights reserved.
msramek 2017/05/31 11:31:36 nit: We don't use (c) anymore.
Ramin Halavati 2017/05/31 12:28:25 Done.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import codecs
7 import os.path
8 import posixpath
9 import re
10 import subprocess
11 import sys
12
13
14 def NormalizePath(path):
msramek 2017/05/31 11:31:35 nit: make this also private?
Ramin Halavati 2017/05/31 12:28:25 Done.
15 """Returns a path normalized to how we write DEPS rules and compare paths."""
16 return os.path.normcase(path).replace(os.path.sep, posixpath.sep)
17
18
19 def _GitSourceDirectories(base_directory):
20 """Returns set of normalized paths to subdirectories containing sources
msramek 2017/05/31 11:31:35 nit: the set
Ramin Halavati 2017/05/31 12:28:24 Done.
21 managed by git."""
22 base_dir_norm = NormalizePath(base_directory)
23 git_source_directories = set([base_dir_norm])
24
25 git_cmd = 'git.bat' if os.name == 'nt' else 'git'
msramek 2017/05/31 11:31:35 Where does this come from? Can you add a comment w
Ramin Halavati 2017/05/31 12:28:24 Comment added in docstring.
msramek 2017/05/31 14:55:24 I hoped to be able to point to some documentation
Ramin Halavati 2017/05/31 19:19:13 Function Replaced!
26 git_ls_files_cmd = [git_cmd, 'ls-files']
27 popen = subprocess.Popen(git_ls_files_cmd,
28 stdout=subprocess.PIPE,
29 cwd=base_directory)
30 try:
31 try:
32 for line in popen.stdout:
33 dir_path = os.path.join(base_directory, os.path.dirname(line))
34 dir_path_norm = NormalizePath(dir_path)
35 # Add the directory as well as all the parent directories,
36 # stopping once we reach an already-listed directory.
37 while dir_path_norm not in git_source_directories:
38 git_source_directories.add(dir_path_norm)
39 dir_path_norm = posixpath.dirname(dir_path_norm)
40 finally:
41 popen.stdout.close()
42 finally:
43 popen.wait()
44
45 return git_source_directories
46
47
48 class NetworkTrafficAnnotationFileFilter():
49 KEYWORDS = [
50 'network_traffic_annotation.h',
51 'network_traffic_annotation_test_helper.h',
52 'NetworkTrafficAnnotationTag',
53 'PartialNetworkTrafficAnnotationTag',
54 'DefineNetworkTrafficAnnotation',
55 'DefinePartialNetworkTrafficAnnotation',
56 'CompleteNetworkTrafficAnnotation',
57 'BranchedCompleteNetworkTrafficAnnotation',
58 'NO_TRAFFIC_ANNOTATION_YET',
59 'NO_PARTIAL_TRAFFIC_ANNOTATION_YET',
60 'MISSING_TRAFFIC_ANNOTATION',
61 'TRAFFIC_ANNOTATION_FOR_TESTS',
62 'PARTIAL_TRAFFIC_ANNOTATION_FOR_TESTS',
63 'SSLClientSocket', # SSLClientSocket::
msramek 2017/05/31 11:31:35 style: two spaces before comments
Ramin Halavati 2017/05/31 12:28:25 Done.
64 'TCPClientSocket', # TCPClientSocket::
65 'UDPClientSocket', # UDPClientSocket::
66 'URLFetcher::Create', # <----
67 'CreateDatagramClientSocket',# ClientSocketFactory::
68 'CreateSSLClientSocket', # ClientSocketFactory::
69 'CreateTransportClientSocket', # ClientSocketFactory::
70 'CreateRequest', # URLRequestContext::
71 ]
72
73 def __init__(self,
74 base_directory=None,
75 skip_tests=True):
76 """Creates a new NetworkTrafficAnnotationFileFilter.
77
78 Args:
79 base_directory: str Local path to root of checkout, e.g. C:\chr\src.
msramek 2017/05/31 11:31:36 The description says local path, but the example i
Ramin Halavati 2017/05/31 12:28:24 Done.
80 skip_tests: bool Flag stating if test files should be returned or not.
81 """
82 base_directory = (base_directory or
83 os.path.join(os.path.dirname(__file__),
msramek 2017/05/31 11:31:35 Can you document this part (i.e. what happens if b
Ramin Halavati 2017/05/31 12:28:25 Done.
84 os.path.pardir, os.path.pardir))
msramek 2017/05/31 11:31:35 style: should be offset +4 from the beginning of t
Ramin Halavati 2017/05/31 12:28:24 Done.
85 self.base_directory = os.path.abspath(base_directory)
86 self._git_source_directories = None
87
88 if os.path.exists(os.path.join(base_directory, '.git')):
89 self.is_git = True
90 elif os.path.exists(os.path.join(base_directory, '.svn')):
msramek 2017/05/31 11:31:35 Why do we need to support SVN?
Ramin Halavati 2017/05/31 12:28:25 This part is also copied fro builddeps.py. Is it e
msramek 2017/05/31 14:55:24 Chromium used to support both SVN and GIT back in
Ramin Halavati 2017/05/31 19:19:13 Acknowledged.
91 self.is_git = False
92 else:
93 raise "%s is not a repository root" % base_directory
msramek 2017/05/31 11:31:36 Ditto - please be consistent with the single and d
Ramin Halavati 2017/05/31 12:28:24 Done.
94
95 self.content_matcher = re.compile(".*(" + "|".join(self.KEYWORDS) + ").*")
msramek 2017/05/31 11:31:35 The keywords contain "." which will be interpreted
Ramin Halavati 2017/05/31 12:28:24 Done.
96 self.file_name_matcher = re.compile(
97 '(?!.*?test)^.*(\.cc|\.mm)' if skip_tests else
98 '.*(\.cc|\.mm)')
msramek 2017/05/31 11:31:35 nit: Maybe add ^$? This will currently match "file
Ramin Halavati 2017/05/31 12:28:24 $ added, ^ is required?
msramek 2017/05/31 14:55:24 I'm aware that re.match() starts matching from the
Ramin Halavati 2017/05/31 19:19:13 Done.
99
100
101 def FileHasReleventContent(self, filename):
msramek 2017/05/31 11:31:35 s/relevent/relevant/ here and elsewhere, includin
Ramin Halavati 2017/05/31 12:28:24 Done.
102 with open(filename, 'r') as in_file:
103 for line in in_file:
104 if self.content_matcher.match(line):
105 return True
106 return False
107
108
109 def GetFilteredFilesList(self, dir_name):
110 """Returns the list of relevent files in given directory.
111
112 Args:
113 dir_name: str The directory to search for relevent files. All child
114 directories would also be serached.
msramek 2017/05/31 11:31:36 typo: searched
Ramin Halavati 2017/05/31 12:28:25 Done.
115
116 Returns:
117 list of str List of relevent files.
118 """
119 if self.is_git and self._git_source_directories is None:
120 self._git_source_directories = _GitSourceDirectories(self.base_directory)
121
122 # Collect a list of all files and directories to check.
123 files_to_check = []
124 if dir_name and not os.path.isabs(dir_name):
125 dir_name = os.path.join(self.base_directory, dir_name)
126 dirs_to_check = [dir_name or self.base_directory]
127 while dirs_to_check:
128 current_dir = dirs_to_check.pop()
129
130 # Check that this directory is part of the source repository. This
131 # prevents us from descending into third-party code or directories
132 # generated by the build system.
133 if self.is_git:
134 if NormalizePath(current_dir) not in self._git_source_directories:
135 continue
136 elif not os.path.exists(os.path.join(current_dir, '.svn')):
137 continue
138
139 current_dir_contents = sorted(os.listdir(current_dir))
140 file_names = []
141 sub_dirs = []
142 for file_name in current_dir_contents:
143 full_name = os.path.join(current_dir, file_name)
144 if os.path.isdir(full_name):
145 sub_dirs.append(full_name)
146 else:
147 if self.file_name_matcher.match(full_name) and \
148 self.FileHasReleventContent(full_name):
149 file_names.append(full_name)
150 dirs_to_check.extend(reversed(sub_dirs))
151 files_to_check += file_names
152 return files_to_check
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698