Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | |
|
kjellander_webrtc
2017/05/09 09:17:35
You're missing the Python shebang here:
https://go
mbonadei
2017/05/09 11:16:40
It does. Thanks a lot!
| |
| 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 SOURCE_FILE_RE = re.compile(r'.*\"(?P<source_file>.*)\"') | |
| 24 | |
| 25 | |
| 26 class NoBuildGnFoundError(Exception): | |
| 27 pass | |
| 28 | |
| 29 | |
| 30 class WrongFileTypeError(Exception): | |
| 31 pass | |
| 32 | |
| 33 | |
| 34 def _ReadFile(file_path): | |
| 35 """Returns the content of file_path in a string. | |
| 36 | |
| 37 Args: | |
| 38 file_path: the path of the file to read. | |
| 39 Returns: | |
| 40 A string with the content of the file. | |
| 41 """ | |
| 42 with open(file_path) as f: | |
| 43 return f.read() | |
| 44 | |
| 45 | |
| 46 def GetBuildGnPathFromFilePath(file_path, file_exists_check, root_dir_path): | |
| 47 """Returns the BUILD.gn file responsible for file_path. | |
| 48 | |
| 49 Args: | |
| 50 file_path: the absolute path to the .h file to check. | |
| 51 file_exists_check: a function that defines how to check if a file exists | |
| 52 on the file system. | |
| 53 root_dir_path: the absolute path of the root of project. | |
| 54 | |
| 55 Returns: | |
| 56 A string with the absolute path to the BUILD.gn file responsible to include | |
| 57 file_path in a target. | |
| 58 """ | |
| 59 if not file_path.endswith('.h'): | |
| 60 raise WrongFileTypeError( | |
| 61 'File {} is not an header file (.h)'.format(file_path)) | |
| 62 candidate_dir = os.path.dirname(file_path) | |
| 63 while candidate_dir.startswith(root_dir_path): | |
| 64 candidate_build_gn_path = os.path.join(candidate_dir, 'BUILD.gn') | |
| 65 if file_exists_check(candidate_build_gn_path): | |
| 66 return candidate_build_gn_path | |
| 67 else: | |
| 68 candidate_dir = os.path.abspath(os.path.join(candidate_dir, | |
| 69 os.pardir)) | |
| 70 raise NoBuildGnFoundError( | |
| 71 'No BUILD.gn file found for file: `{}`'.format(file_path)) | |
| 72 | |
| 73 | |
| 74 def IsHeaderInBuildGn(header_path, build_gn_path): | |
| 75 """Returns True if the header is listed in the BUILD.gn file. | |
| 76 | |
| 77 Args: | |
| 78 header_path: the absolute path to the header to check. | |
| 79 build_gn_path: the absolute path to the header to check. | |
| 80 | |
| 81 Returns: | |
| 82 bool: True if the header_path is an header that is listed in | |
| 83 at least one GN target in the BUILD.gn file specified by | |
| 84 the argument build_gn_path. | |
| 85 """ | |
| 86 target_abs_path = os.path.dirname(build_gn_path) | |
| 87 build_gn_content = _ReadFile(build_gn_path) | |
| 88 headers_in_build_gn = GetHeadersInBuildGnFileSources(build_gn_content, | |
| 89 target_abs_path) | |
| 90 return header_path in headers_in_build_gn | |
| 91 | |
| 92 | |
| 93 def GetHeadersInBuildGnFileSources(file_content, target_abs_path): | |
| 94 """Returns a set with all the .h files in the file_content. | |
| 95 | |
| 96 Args: | |
| 97 file_content: a string with the content of the BUILD.gn file. | |
| 98 target_abs_path: the absolute path to the directory where the | |
| 99 BUILD.gn file lives. | |
| 100 | |
| 101 Returns: | |
| 102 A set with all the headers (.h file) in the file_content. | |
| 103 The set contains absolute paths. | |
| 104 """ | |
| 105 headers_in_sources = set([]) | |
| 106 for target_match in TARGET_RE.finditer(file_content): | |
| 107 target_contents = target_match.group('target_contents') | |
| 108 for sources_match in SOURCES_RE.finditer(target_contents): | |
| 109 sources = sources_match.group('sources') | |
| 110 for source_file_match in SOURCE_FILE_RE.finditer(sources): | |
| 111 source_file = source_file_match.group('source_file') | |
| 112 if source_file.endswith('.h'): | |
| 113 headers_in_sources.add(os.path.join(target_abs_path, source_file)) | |
| 114 return headers_in_sources | |
| OLD | NEW |