OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 A tool to generate a predetermined resource ids file that can be used as an |
| 8 input to grit via the -p option. This is meant to be run manually every once in |
| 9 a while and its output checked in. See tools/gritsettings/README.md for details. |
| 10 """ |
| 11 |
| 12 import fnmatch |
| 13 import os |
| 14 import re |
| 15 import sys |
| 16 |
| 17 # Regular expressions for parsing the #define macro format. Separate regular |
| 18 # expressions are used for parsing lines with pragma (for builds with |
| 19 # enable_resource_whitelist_generation flag) in windows and non-windows, and for |
| 20 # lines without pragma, For example, |
| 21 # Without generate whitelist flag: |
| 22 # #define IDS_FOO_MESSAGE 1234 |
| 23 # With generate whitelist flag in non-windows: |
| 24 # #define IDS_FOO_MESSAGE _Pragma("whitelisted_resource_1234") 1234 |
| 25 # With generate whitelist flag in windows: |
| 26 # #define IDS_FOO_MESSAGE __pragma(message("whitelisted_resource_1234")) 1234 |
| 27 RESOURCE_EXTRACT_REGEX = re.compile('^#define (\S*) (\d*)$', re.MULTILINE) |
| 28 RESOURCE_EXTRACT_REGEX_PRAGMA = re.compile( |
| 29 '^#define (\S*) _Pragma\("whitelisted_resource_\d*"\) (\d*)$', |
| 30 re.MULTILINE) |
| 31 RESOURCE_EXTRACT_REGEX_PRAGMA_WINDOWS = re.compile( |
| 32 '^#define (\S*) __pragma\(message\("whitelisted_resource_\d*"\)\) (\d*)$', |
| 33 re.MULTILINE) |
| 34 |
| 35 ORDERED_RESOURCE_IDS_REGEX = re.compile('^Resource=(\d*)$', re.MULTILINE) |
| 36 |
| 37 |
| 38 def _GetResourceNameIdPairsIter(string_to_scan): |
| 39 """Gets an iterator of the resource name and id pairs of the given string. |
| 40 |
| 41 Scans the input string for lines of the form "#define NAME ID" and returns |
| 42 an iterator over all matching (NAME, ID) pairs. |
| 43 |
| 44 Args: |
| 45 string_to_scan: The input string to scan. |
| 46 |
| 47 Yields: |
| 48 A tuple of name and id. |
| 49 """ |
| 50 for match in RESOURCE_EXTRACT_REGEX.finditer(string_to_scan): |
| 51 yield match.group(1, 2) |
| 52 for match in RESOURCE_EXTRACT_REGEX_PRAGMA.finditer(string_to_scan): |
| 53 yield match.group(1, 2) |
| 54 for match in RESOURCE_EXTRACT_REGEX_PRAGMA_WINDOWS.finditer(string_to_scan): |
| 55 yield match.group(1, 2) |
| 56 |
| 57 |
| 58 def _ReadOrderedResourceIds(path): |
| 59 """Reads ordered resource ids from the given file. |
| 60 |
| 61 The resources are expected to be of the format produced by running Chrome |
| 62 with --print-resource-ids command line. |
| 63 |
| 64 Args: |
| 65 path: File path to read resource ids from. |
| 66 |
| 67 Returns: |
| 68 An array of ordered resource ids. |
| 69 """ |
| 70 ordered_resource_ids = [] |
| 71 with open(path, "r") as f: |
| 72 for match in ORDERED_RESOURCE_IDS_REGEX.finditer(f.read()): |
| 73 ordered_resource_ids.append(int(match.group(1))) |
| 74 return ordered_resource_ids |
| 75 |
| 76 |
| 77 def GenerateResourceMapping(original_resources, ordered_resource_ids): |
| 78 """Generates a resource mapping from the ordered ids and the original mapping. |
| 79 |
| 80 The returned dict will assign new ids to ordered_resource_ids numerically |
| 81 increasing from 101. |
| 82 |
| 83 Args: |
| 84 original_resources: A dict of original resource ids to resource names. |
| 85 ordered_resource_ids: An array of ordered resource ids. |
| 86 |
| 87 Returns: |
| 88 A dict of resource ids to resource names. |
| 89 """ |
| 90 output_resource_map = {} |
| 91 # 101 is used as the starting value since other parts of GRIT require it to be |
| 92 # the minimum (e.g. rc_header.py) based on Windows resource numbering. |
| 93 next_id = 101 |
| 94 for original_id in ordered_resource_ids: |
| 95 resource_name = original_resources[original_id] |
| 96 output_resource_map[next_id] = resource_name |
| 97 next_id += 1 |
| 98 return output_resource_map |
| 99 |
| 100 |
| 101 def ReadResourceIdsFromFile(file, original_resources): |
| 102 """Reads resource ids from a GRIT-produced header file. |
| 103 |
| 104 Args: |
| 105 file: File to a GRIT-produced header file to read from. |
| 106 original_resources: Dict of resource ids to resource names to add to. |
| 107 """ |
| 108 for resource_name, resource_id in _GetResourceNameIdPairsIter(file.read()): |
| 109 original_resources[int(resource_id)] = resource_name |
| 110 |
| 111 |
| 112 def _ReadOriginalResourceIds(out_dir): |
| 113 """Reads resource ids from GRIT header files in the specified directory. |
| 114 |
| 115 Args: |
| 116 out_dir: A Chrome build output directory (e.g. out/gn) to scan. |
| 117 |
| 118 Returns: |
| 119 A dict of resource ids to resource names. |
| 120 """ |
| 121 original_resources = {} |
| 122 for root, dirnames, filenames in os.walk(out_dir + '/gen'): |
| 123 for filename in filenames: |
| 124 if filename.endswith(('_resources.h', '_settings.h', '_strings.h')): |
| 125 with open(os.path.join(root, filename), "r") as f: |
| 126 ReadResourceIdsFromFile(f, original_resources) |
| 127 return original_resources |
| 128 |
| 129 |
| 130 def _GeneratePredeterminedIdsFile(ordered_resources_file, out_dir): |
| 131 """Generates a predetermined ids file. |
| 132 |
| 133 Args: |
| 134 ordered_resources_file: File path to read ordered resource ids from. |
| 135 out_dir: A Chrome build output directory (e.g. out/gn) to scan. |
| 136 |
| 137 Returns: |
| 138 A dict of resource ids to resource names. |
| 139 """ |
| 140 original_resources = _ReadOriginalResourceIds(out_dir) |
| 141 ordered_resource_ids = _ReadOrderedResourceIds(ordered_resources_file) |
| 142 output_resource_map = GenerateResourceMapping(original_resources, |
| 143 ordered_resource_ids) |
| 144 for res_id in sorted(output_resource_map.keys()): |
| 145 print "{} {}".format(output_resource_map[res_id], res_id) |
| 146 |
| 147 |
| 148 def main(argv): |
| 149 if len(argv) != 2: |
| 150 print("usage: gen_predetermined_ids.py <ordered_resources_file> <out_dir>") |
| 151 sys.exit(1) |
| 152 ordered_resources_file, out_dir = argv[0], argv[1] |
| 153 _GeneratePredeterminedIdsFile(ordered_resources_file, out_dir) |
| 154 |
| 155 |
| 156 if '__main__' == __name__: |
| 157 main(sys.argv[1:]) |
OLD | NEW |