summaryrefslogtreecommitdiffstats
path: root/tools/grit
diff options
context:
space:
mode:
authoradriansc@chromium.org <adriansc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-09 21:31:17 +0000
committeradriansc@chromium.org <adriansc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-09 21:31:17 +0000
commit2aefa3ab2bbaae42aea7033041aa8be497472db3 (patch)
treef5d87fdc50e1b5406d2324c3b8ff5cebd2651854 /tools/grit
parent61368ee58063da1893f10beae41a3be5320a8274 (diff)
downloadchromium_src-2aefa3ab2bbaae42aea7033041aa8be497472db3.zip
chromium_src-2aefa3ab2bbaae42aea7033041aa8be497472db3.tar.gz
chromium_src-2aefa3ab2bbaae42aea7033041aa8be497472db3.tar.bz2
Merged duplicate functions from tools/data_pack into the grit/format/data_pack class.
Marked tools/data_pack directory for removal when the dependency in third_party file WebKit.gyp has been updated to point to grit. BUG=none TEST=builds Review URL: http://codereview.chromium.org/7795057 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100504 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
-rwxr-xr-xtools/grit/grit/format/data_pack.py77
-rw-r--r--tools/grit/grit/format/data_pack_unittest.py3
-rw-r--r--tools/grit/grit/format/repack.py24
3 files changed, 98 insertions, 6 deletions
diff --git a/tools/grit/grit/format/data_pack.py b/tools/grit/grit/format/data_pack.py
index 993ecf6..01c0c9e 100755
--- a/tools/grit/grit/format/data_pack.py
+++ b/tools/grit/grit/format/data_pack.py
@@ -7,16 +7,23 @@
files.
'''
+import exceptions
+import os
import struct
+import sys
+sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from grit.format import interface
from grit.node import include
from grit.node import message
from grit.node import misc
-PACK_FILE_VERSION = 3
+FILE_FORMAT_VERSION = 3
+HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries)
+class WrongFileVersion(Exception):
+ pass
class DataPack(interface.ItemFormatter):
'''Writes out the data pack file format (platform agnostic resource file).'''
@@ -31,7 +38,7 @@ class DataPack(interface.ItemFormatter):
for node in nodes:
id, value = node.GetDataPackPair(lang)
data[id] = value
- return DataPack.WriteDataPack(data)
+ return DataPack.WriteDataPackToString(data)
@staticmethod
def GetDataNodes(item):
@@ -50,14 +57,40 @@ class DataPack(interface.ItemFormatter):
return nodes
@staticmethod
- def WriteDataPack(resources):
+ 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 = {}
+ if num_entries == 0:
+ return resources
+
+ # Read the index and data.
+ data = data[HEADER_LENGTH:]
+ kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32.
+ for _ in range(num_entries):
+ id, offset = struct.unpack("<HI", data[:kIndexEntrySize])
+ data = data[kIndexEntrySize:]
+ next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])
+ resources[id] = original_data[offset:next_offset]
+
+ return resources
+
+ @staticmethod
+ def WriteDataPackToString(resources):
"""Write a map of id=>data into a string in the data pack format and return
it."""
ids = sorted(resources.keys())
ret = []
# Write file header.
- ret.append(struct.pack("<II", PACK_FILE_VERSION, len(ids)))
+ ret.append(struct.pack("<II", FILE_FORMAT_VERSION, len(ids)))
HEADER_LENGTH = 2 * 4 # Two uint32s.
# Each entry is a uint16 + a uint32s. We have one extra entry for the last
@@ -76,3 +109,39 @@ class DataPack(interface.ItemFormatter):
for id in ids:
ret.append(resources[id])
return ''.join(ret)
+
+ @staticmethod
+ def WriteDataPack(resources, output_file):
+ """Write a map of id=>data into output_file as a data pack."""
+ file = open(output_file, "wb")
+ content = DataPack.WriteDataPackToString(resources)
+ file.write(content)
+
+ @staticmethod
+ def RePack(output_file, input_files):
+ """Write a new data pack to |output_file| based on a list of filenames
+ (|input_files|)"""
+ resources = {}
+ for filename in input_files:
+ new_resources = DataPack.ReadDataPack(filename)
+
+ # Make sure we have no duplicates.
+ duplicate_keys = set(new_resources.keys()) & set(resources.keys())
+ if len(duplicate_keys) != 0:
+ raise exceptions.KeyError("Duplicate keys: " +
+ str(list(duplicate_keys)))
+
+ resources.update(new_resources)
+
+ DataPack.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, "datapack1.pak")
+ data2 = { 1000: "test", 5: "five" }
+ WriteDataPack(data2, "datapack2.pak")
+ print "wrote datapack1 and datapack2 to current directory."
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/grit/grit/format/data_pack_unittest.py b/tools/grit/grit/format/data_pack_unittest.py
index aa26a2f..35966639 100644
--- a/tools/grit/grit/format/data_pack_unittest.py
+++ b/tools/grit/grit/format/data_pack_unittest.py
@@ -24,10 +24,9 @@ class FormatDataPackUnittest(unittest.TestCase):
'\x00\x00\x3e\x00\x00\x00' # extra entry for the size of last
'this is id 4this is id 6') # data
input = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
- output = data_pack.DataPack.WriteDataPack(input)
+ output = data_pack.DataPack.WriteDataPackToString(input)
self.failUnless(output == expected)
if __name__ == '__main__':
unittest.main()
-
diff --git a/tools/grit/grit/format/repack.py b/tools/grit/grit/format/repack.py
new file mode 100644
index 0000000..3f312a4
--- /dev/null
+++ b/tools/grit/grit/format/repack.py
@@ -0,0 +1,24 @@
+#!/usr/bin/python
+# Copyright (c) 2011 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+A simple utility function to merge data pack files into a single data pack. See
+http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalizedstrings
+for details about the file format.
+"""
+
+import sys
+
+import data_pack
+
+def main(argv):
+ if len(argv) < 3:
+ print ("Usage:\n %s <output_filename> <input_file1> [input_file2] ... " %
+ argv[0])
+ sys.exit(-1)
+ data_pack.DataPack.RePack(argv[1], argv[2:])
+
+if '__main__' == __name__:
+ main(sys.argv)