summaryrefslogtreecommitdiffstats
path: root/tools/data_pack/data_pack.py
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-15 01:15:48 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-15 01:15:48 +0000
commit7cb4af565c1017625df2e55d581c1f62ed4c4451 (patch)
tree2cec53ebe8736f7a3e8e52e3cb030b306ba75c39 /tools/data_pack/data_pack.py
parente9d6398c5de33b6b13405717b85f3c31961816fe (diff)
downloadchromium_src-7cb4af565c1017625df2e55d581c1f62ed4c4451.zip
chromium_src-7cb4af565c1017625df2e55d581c1f62ed4c4451.tar.gz
chromium_src-7cb4af565c1017625df2e55d581c1f62ed4c4451.tar.bz2
add a repack utility for combining .pak files generated
by grit into a single .pak file. Review URL: http://codereview.chromium.org/18254 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8063 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/data_pack/data_pack.py')
-rwxr-xr-xtools/data_pack/data_pack.py38
1 files changed, 32 insertions, 6 deletions
diff --git a/tools/data_pack/data_pack.py b/tools/data_pack/data_pack.py
index c269f21..fde9483 100755
--- a/tools/data_pack/data_pack.py
+++ b/tools/data_pack/data_pack.py
@@ -9,7 +9,32 @@ See base/pack_file* for details.
import struct
-version = 1
+FILE_FORMAT_VERSION = 1
+HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries)
+
+class WrongFileVersion(Exception):
+ pass
+
+def ReadDataPack(input_file):
+ """Reads a data pack file and returns a dictionary."""
+ data = open(input_file, "rb").read()
+ original_data = data
+
+ # Read the header.
+ version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH])
+ if version != FILE_FORMAT_VERSION:
+ raise WrongFileVersion
+
+ resources = {}
+ # Read the index and data.
+ data = data[HEADER_LENGTH:]
+ kIndexEntrySize = 3 * 4 # Each entry is 3 uint32s.
+ for _ in range(num_entries):
+ id, offset, length = struct.unpack("<III", data[:kIndexEntrySize])
+ data = data[kIndexEntrySize:]
+ resources[id] = original_data[offset:offset + length]
+
+ return resources
def WriteDataPack(resources, output_file):
"""Write a map of id=>data into output_file as a data pack."""
@@ -17,13 +42,12 @@ def WriteDataPack(resources, output_file):
file = open(output_file, "wb")
# Write file header.
- file.write(struct.pack("<II", version, len(ids)))
- header_length = 2 * 4 # Two uint32s.
+ file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids)))
index_length = len(ids) * 3 * 4 # Each entry is 3 uint32s.
# Write index.
- data_offset = header_length + index_length
+ data_offset = HEADER_LENGTH + index_length
for id in ids:
file.write(struct.pack("<III", id, data_offset, len(resources[id])))
data_offset += len(resources[id])
@@ -35,8 +59,10 @@ def WriteDataPack(resources, output_file):
def main():
# Just write a simple file.
data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
- WriteDataPack(data, "datapack")
- print "wrote datapack to current directory."
+ WriteDataPack(data, "datapack1.pak")
+ data2 = { 1000: "test", 5: "five" }
+ WriteDataPack(data2, "datapack2.pak")
+ print "wrote datapack1 and datapack2 to current directory."
if __name__ == '__main__':
main()