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

Side by Side Diff: tools_webrtc/presubmit_checks_lib/check_orphan_headers.py

Issue 2872493002: Adding PRESUBMIT check on orphan headers files. (Closed)
Patch Set: adding shebang line 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 unified diff | Download patch
« no previous file with comments | « PRESUBMIT.py ('k') | tools_webrtc/presubmit_checks_lib/check_orphan_headers_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 #
4 # Use of this source code is governed by a BSD-style license
5 # that can be found in the LICENSE file in the root of the source
6 # tree. An additional intellectual property rights grant can be found
7 # in the file PATENTS. All contributing project authors may
8 # be found in the AUTHORS file in the root of the source tree.
9
10 import os
11 import re
12
13
14 # TARGET_RE matches a GN target, and extracts the target name and the contents.
15 TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {'
16 r'(?P<target_contents>.*?)'
17 r'(?P=indent)}',
18 re.MULTILINE | re.DOTALL)
19
20 # SOURCES_RE matches a block of sources inside a GN target.
21 SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]',
22 re.MULTILINE | re.DOTALL)
23
24 SOURCE_FILE_RE = re.compile(r'.*\"(?P<source_file>.*)\"')
25
26
27 class NoBuildGnFoundError(Exception):
28 pass
29
30
31 class WrongFileTypeError(Exception):
32 pass
33
34
35 def _ReadFile(file_path):
36 """Returns the content of file_path in a string.
37
38 Args:
39 file_path: the path of the file to read.
40 Returns:
41 A string with the content of the file.
42 """
43 with open(file_path) as f:
44 return f.read()
45
46
47 def GetBuildGnPathFromFilePath(file_path, file_exists_check, root_dir_path):
48 """Returns the BUILD.gn file responsible for file_path.
49
50 Args:
51 file_path: the absolute path to the .h file to check.
52 file_exists_check: a function that defines how to check if a file exists
53 on the file system.
54 root_dir_path: the absolute path of the root of project.
55
56 Returns:
57 A string with the absolute path to the BUILD.gn file responsible to include
58 file_path in a target.
59 """
60 if not file_path.endswith('.h'):
61 raise WrongFileTypeError(
62 'File {} is not an header file (.h)'.format(file_path))
63 candidate_dir = os.path.dirname(file_path)
64 while candidate_dir.startswith(root_dir_path):
65 candidate_build_gn_path = os.path.join(candidate_dir, 'BUILD.gn')
66 if file_exists_check(candidate_build_gn_path):
67 return candidate_build_gn_path
68 else:
69 candidate_dir = os.path.abspath(os.path.join(candidate_dir,
70 os.pardir))
71 raise NoBuildGnFoundError(
72 'No BUILD.gn file found for file: `{}`'.format(file_path))
73
74
75 def IsHeaderInBuildGn(header_path, build_gn_path):
76 """Returns True if the header is listed in the BUILD.gn file.
77
78 Args:
79 header_path: the absolute path to the header to check.
80 build_gn_path: the absolute path to the header to check.
81
82 Returns:
83 bool: True if the header_path is an header that is listed in
84 at least one GN target in the BUILD.gn file specified by
85 the argument build_gn_path.
86 """
87 target_abs_path = os.path.dirname(build_gn_path)
88 build_gn_content = _ReadFile(build_gn_path)
89 headers_in_build_gn = GetHeadersInBuildGnFileSources(build_gn_content,
90 target_abs_path)
91 return header_path in headers_in_build_gn
92
93
94 def GetHeadersInBuildGnFileSources(file_content, target_abs_path):
95 """Returns a set with all the .h files in the file_content.
96
97 Args:
98 file_content: a string with the content of the BUILD.gn file.
99 target_abs_path: the absolute path to the directory where the
100 BUILD.gn file lives.
101
102 Returns:
103 A set with all the headers (.h file) in the file_content.
104 The set contains absolute paths.
105 """
106 headers_in_sources = set([])
107 for target_match in TARGET_RE.finditer(file_content):
108 target_contents = target_match.group('target_contents')
109 for sources_match in SOURCES_RE.finditer(target_contents):
110 sources = sources_match.group('sources')
111 for source_file_match in SOURCE_FILE_RE.finditer(sources):
112 source_file = source_file_match.group('source_file')
113 if source_file.endswith('.h'):
114 headers_in_sources.add(os.path.join(target_abs_path, source_file))
115 return headers_in_sources
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | tools_webrtc/presubmit_checks_lib/check_orphan_headers_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698