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