| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 """A simple utility function to merge data pack files into a single data pack. | 6 """A simple utility function to merge data pack files into a single data pack. |
| 7 See base/pack_file* for details about the file format. | 7 See base/pack_file* for details about the file format. |
| 8 | 8 |
| 9 TODO(adriansc): Remove this file once the dependency has been updated in WebKit | 9 TODO(adriansc): Remove this file once the dependency has been updated in WebKit |
| 10 to point to grit scripts. | 10 to point to grit scripts. |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import exceptions | 13 import exceptions |
| 14 import struct | 14 import struct |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 import data_pack | 17 import data_pack |
| 18 | 18 |
| 19 def RePack(output_file, input_files): | 19 def RePack(output_file, input_files): |
| 20 """Write a new data pack to |output_file| based on a list of filenames | 20 """Write a new data pack to |output_file| based on a list of filenames |
| 21 (|input_files|)""" | 21 (|input_files|)""" |
| 22 resources = {} | 22 resources = {} |
| 23 encoding = None | |
| 24 for filename in input_files: | 23 for filename in input_files: |
| 25 new_content = data_pack.ReadDataPack(filename) | 24 new_resources = data_pack.ReadDataPack(filename) |
| 26 | 25 |
| 27 # Make sure we have no dups. | 26 # Make sure we have no dups. |
| 28 duplicate_keys = set(new_content.resources.keys()) & set(resources.keys()) | 27 duplicate_keys = set(new_resources.keys()) & set(resources.keys()) |
| 29 if len(duplicate_keys) != 0: | 28 if len(duplicate_keys) != 0: |
| 30 raise exceptions.KeyError("Duplicate keys: " + str(list(duplicate_keys))) | 29 raise exceptions.KeyError("Duplicate keys: " + str(list(duplicate_keys))) |
| 31 | 30 |
| 32 # Make sure encoding is consistent. | 31 resources.update(new_resources) |
| 33 if encoding in (None, data_pack.BINARY): | |
| 34 encoding = new_content.encoding | |
| 35 elif new_content.encoding not in (data_pack.BINARY, encoding): | |
| 36 raise exceptions.KeyError("Inconsistent encodings: " + | |
| 37 str(encoding) + " vs " + | |
| 38 str(new_content.encoding)) | |
| 39 | 32 |
| 40 resources.update(new_content.resources) | 33 data_pack.WriteDataPack(resources, output_file) |
| 41 | |
| 42 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16 | |
| 43 if encoding is None: | |
| 44 encoding = data_pack.BINARY | |
| 45 data_pack.WriteDataPack(resources, output_file, encoding) | |
| 46 | 34 |
| 47 def main(argv): | 35 def main(argv): |
| 48 if len(argv) < 3: | 36 if len(argv) < 3: |
| 49 print ("Usage:\n %s <output_filename> <input_file1> [input_file2] ... " % | 37 print ("Usage:\n %s <output_filename> <input_file1> [input_file2] ... " % |
| 50 argv[0]) | 38 argv[0]) |
| 51 sys.exit(-1) | 39 sys.exit(-1) |
| 52 RePack(argv[1], argv[2:]) | 40 RePack(argv[1], argv[2:]) |
| 53 | 41 |
| 54 if '__main__' == __name__: | 42 if '__main__' == __name__: |
| 55 main(sys.argv) | 43 main(sys.argv) |
| OLD | NEW |