OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Support for formatting a data pack file used for platform agnostic resource | 6 """Support for formatting a data pack file used for platform agnostic resource |
7 files. | 7 files. |
8 """ | 8 """ |
9 | 9 |
10 import collections | 10 import collections |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return ''.join(ret) | 102 return ''.join(ret) |
103 | 103 |
104 | 104 |
105 def WriteDataPack(resources, output_file, encoding): | 105 def WriteDataPack(resources, output_file, encoding): |
106 """Writes a map of id=>data into output_file as a data pack.""" | 106 """Writes a map of id=>data into output_file as a data pack.""" |
107 content = WriteDataPackToString(resources, encoding) | 107 content = WriteDataPackToString(resources, encoding) |
108 with open(output_file, 'wb') as file: | 108 with open(output_file, 'wb') as file: |
109 file.write(content) | 109 file.write(content) |
110 | 110 |
111 | 111 |
112 def RePack(output_file, input_files, whitelist_file=None): | 112 def RePack(output_file, input_files, whitelist_file=None, |
| 113 suppress_removed_key_output=False): |
113 """Write a new data pack file by combining input pack files. | 114 """Write a new data pack file by combining input pack files. |
114 | 115 |
115 Args: | 116 Args: |
116 output_file: path to the new data pack file. | 117 output_file: path to the new data pack file. |
117 input_files: a list of paths to the data pack files to combine. | 118 input_files: a list of paths to the data pack files to combine. |
118 whitelist_file: path to the file that contains the list of resource IDs | 119 whitelist_file: path to the file that contains the list of resource IDs |
119 that should be kept in the output file or None to include | 120 that should be kept in the output file or None to include |
120 all resources. | 121 all resources. |
| 122 suppress_removed_key_output: allows the caller to suppress the output from |
| 123 RePackFromDataPackStrings. |
121 | 124 |
122 Raises: | 125 Raises: |
123 KeyError: if there are duplicate keys or resource encoding is | 126 KeyError: if there are duplicate keys or resource encoding is |
124 inconsistent. | 127 inconsistent. |
125 """ | 128 """ |
126 input_data_packs = [ReadDataPack(filename) for filename in input_files] | 129 input_data_packs = [ReadDataPack(filename) for filename in input_files] |
127 whitelist = None | 130 whitelist = None |
128 if whitelist_file: | 131 if whitelist_file: |
129 whitelist = util.ReadFile(whitelist_file, util.RAW_TEXT).strip().split('\n') | 132 whitelist = util.ReadFile(whitelist_file, util.RAW_TEXT).strip().split('\n') |
130 whitelist = set(map(int, whitelist)) | 133 whitelist = set(map(int, whitelist)) |
131 resources, encoding = RePackFromDataPackStrings(input_data_packs, whitelist) | 134 resources, encoding = RePackFromDataPackStrings( |
| 135 input_data_packs, whitelist, suppress_removed_key_output) |
132 WriteDataPack(resources, output_file, encoding) | 136 WriteDataPack(resources, output_file, encoding) |
133 | 137 |
134 | 138 |
135 def RePackFromDataPackStrings(inputs, whitelist): | 139 def RePackFromDataPackStrings(inputs, whitelist, |
| 140 suppress_removed_key_output=False): |
136 """Returns a data pack string that combines the resources from inputs. | 141 """Returns a data pack string that combines the resources from inputs. |
137 | 142 |
138 Args: | 143 Args: |
139 inputs: a list of data pack strings that need to be combined. | 144 inputs: a list of data pack strings that need to be combined. |
140 whitelist: a list of resource IDs that should be kept in the output string | 145 whitelist: a list of resource IDs that should be kept in the output string |
141 or None to include all resources. | 146 or None to include all resources. |
| 147 suppress_removed_key_output: Do not print removed keys. |
142 | 148 |
143 Returns: | 149 Returns: |
144 DataPackContents: a tuple containing the new combined data pack and its | 150 DataPackContents: a tuple containing the new combined data pack and its |
145 encoding. | 151 encoding. |
146 | 152 |
147 Raises: | 153 Raises: |
148 KeyError: if there are duplicate keys or resource encoding is | 154 KeyError: if there are duplicate keys or resource encoding is |
149 inconsistent. | 155 inconsistent. |
150 """ | 156 """ |
151 resources = {} | 157 resources = {} |
(...skipping 11 matching lines...) Expand all Loading... |
163 raise exceptions.KeyError('Inconsistent encodings: ' + str(encoding) + | 169 raise exceptions.KeyError('Inconsistent encodings: ' + str(encoding) + |
164 ' vs ' + str(content.encoding)) | 170 ' vs ' + str(content.encoding)) |
165 | 171 |
166 if whitelist: | 172 if whitelist: |
167 whitelisted_resources = dict([(key, content.resources[key]) | 173 whitelisted_resources = dict([(key, content.resources[key]) |
168 for key in content.resources.keys() | 174 for key in content.resources.keys() |
169 if key in whitelist]) | 175 if key in whitelist]) |
170 resources.update(whitelisted_resources) | 176 resources.update(whitelisted_resources) |
171 removed_keys = [key for key in content.resources.keys() | 177 removed_keys = [key for key in content.resources.keys() |
172 if key not in whitelist] | 178 if key not in whitelist] |
173 for key in removed_keys: | 179 if not suppress_removed_key_output: |
174 print 'RePackFromDataPackStrings Removed Key:', key | 180 for key in removed_keys: |
| 181 print 'RePackFromDataPackStrings Removed Key:', key |
175 else: | 182 else: |
176 resources.update(content.resources) | 183 resources.update(content.resources) |
177 | 184 |
178 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16 | 185 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16 |
179 if encoding is None: | 186 if encoding is None: |
180 encoding = BINARY | 187 encoding = BINARY |
181 return DataPackContents(resources, encoding) | 188 return DataPackContents(resources, encoding) |
182 | 189 |
183 | 190 |
184 # Temporary hack for external programs that import data_pack. | 191 # Temporary hack for external programs that import data_pack. |
(...skipping 18 matching lines...) Expand all Loading... |
203 # Just write a simple file. | 210 # Just write a simple file. |
204 data = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} | 211 data = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} |
205 WriteDataPack(data, 'datapack1.pak', UTF8) | 212 WriteDataPack(data, 'datapack1.pak', UTF8) |
206 data2 = {1000: 'test', 5: 'five'} | 213 data2 = {1000: 'test', 5: 'five'} |
207 WriteDataPack(data2, 'datapack2.pak', UTF8) | 214 WriteDataPack(data2, 'datapack2.pak', UTF8) |
208 print 'wrote datapack1 and datapack2 to current directory.' | 215 print 'wrote datapack1 and datapack2 to current directory.' |
209 | 216 |
210 | 217 |
211 if __name__ == '__main__': | 218 if __name__ == '__main__': |
212 main() | 219 main() |
OLD | NEW |