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
4 # found in the LICENSE file.
6 '''Support for formatting a data pack file used for platform agnostic resource
15 if __name__ == '__main__':
16 sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
19 from grit.node import include
20 from grit.node import message
21 from grit.node import structure
22 from grit.node import misc
26 HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and
27 # one uint8 (encoding of text resources)
28 BINARY, UTF8, UTF16 = range(3)
31 class WrongFileVersion(Exception):
35 DataPackContents = collections.namedtuple(
36 'DataPackContents', 'resources encoding')
39 def Format(root, lang='en', output_dir='.'):
40 '''Writes out the data pack file format (platform agnostic resource file).'''
42 for node in root.ActiveDescendants():
44 if isinstance(node, (include.IncludeNode, message.MessageNode,
45 structure.StructureNode)):
46 id, value = node.GetDataPackPair(lang, UTF8)
49 return WriteDataPackToString(data, UTF8)
52 def ReadDataPack(input_file):
53 """Reads a data pack file and returns a dictionary."""
54 data = util.ReadFile(input_file, util.BINARY)
58 version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])
59 if version != PACK_FILE_VERSION:
60 print "Wrong file version in ", input_file
61 raise WrongFileVersion
65 return DataPackContents(resources, encoding)
67 # Read the index and data.
68 data = data[HEADER_LENGTH:]
69 kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32.
70 for _ in range(num_entries):
71 id, offset = struct.unpack("<HI", data[:kIndexEntrySize])
72 data = data[kIndexEntrySize:]
73 next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])
74 resources[id] = original_data[offset:next_offset]
76 return DataPackContents(resources, encoding)
79 def WriteDataPackToString(resources, encoding):
80 """Write a map of id=>data into a string in the data pack format and return
82 ids = sorted(resources.keys())
86 ret.append(struct.pack("<IIB", PACK_FILE_VERSION, len(ids), encoding))
87 HEADER_LENGTH = 2 * 4 + 1 # Two uint32s and one uint8.
89 # Each entry is a uint16 + a uint32s. We have one extra entry for the last
91 index_length = (len(ids) + 1) * (2 + 4)
94 data_offset = HEADER_LENGTH + index_length
96 ret.append(struct.pack("<HI", id, data_offset))
97 data_offset += len(resources[id])
99 ret.append(struct.pack("<HI", 0, data_offset))
103 ret.append(resources[id])
107 def WriteDataPack(resources, output_file, encoding):
108 """Write a map of id=>data into output_file as a data pack."""
109 content = WriteDataPackToString(resources, encoding)
110 with open(output_file, "wb") as file:
114 def RePack(output_file, input_files):
115 """Write a new data pack to |output_file| based on a list of filenames
119 for filename in input_files:
120 new_content = ReadDataPack(filename)
122 # Make sure we have no dups.
123 duplicate_keys = set(new_content.resources.keys()) & set(resources.keys())
124 if len(duplicate_keys) != 0:
125 raise exceptions.KeyError("Duplicate keys: " + str(list(duplicate_keys)))
127 # Make sure encoding is consistent.
128 if encoding in (None, BINARY):
129 encoding = new_content.encoding
130 elif new_content.encoding not in (BINARY, encoding):
131 raise exceptions.KeyError("Inconsistent encodings: " +
132 str(encoding) + " vs " +
133 str(new_content.encoding))
135 resources.update(new_content.resources)
137 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16
140 WriteDataPack(resources, output_file, encoding)
143 # Temporary hack for external programs that import data_pack.
144 # TODO(benrg): Remove this.
145 class DataPack(object):
147 DataPack.ReadDataPack = staticmethod(ReadDataPack)
148 DataPack.WriteDataPackToString = staticmethod(WriteDataPackToString)
149 DataPack.WriteDataPack = staticmethod(WriteDataPack)
150 DataPack.RePack = staticmethod(RePack)
154 if len(sys.argv) > 1:
155 # When an argument is given, read and explode the file to text
156 # format, for easier diffing.
157 data = ReadDataPack(sys.argv[1])
159 for (resource_id, text) in data.resources.iteritems():
160 print "%s: %s" % (resource_id, text)
162 # Just write a simple file.
163 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
164 WriteDataPack(data, "datapack1.pak", UTF8)
165 data2 = { 1000: "test", 5: "five" }
166 WriteDataPack(data2, "datapack2.pak", UTF8)
167 print "wrote datapack1 and datapack2 to current directory."
170 if __name__ == '__main__':