Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/data_pack/DataPack.py

Issue 17402: Python library for writing data pack files (see base/data_pack*). (Closed)
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
tony 2009/01/08 00:44:53 Nit: this file should be named data_pack.py or som
2 # Copyright (c) 2008 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.
5
6 """A simple utility function to produce data pack files.
7 See base/pack_file* for details.
8 """
9
10 import struct
11
12 version = 1
13
14 def WriteDataPack(resources, output_file):
15 """Write a map of id=>data into output_file as a data pack."""
16 ids = sorted(resources.keys())
17 file = open(output_file, "wb")
18
19 # Write file header.
20 file.write(struct.pack("<II", version, len(ids)))
21 header_length = 2 * 4 # Two uint32s.
22
23 index_length = len(ids) * 3 * 4 # Each entry is 3 uint32s.
24
25 # Write index.
26 data_offset = header_length + index_length
27 for id in ids:
28 file.write(struct.pack("<III", id, data_offset, len(resources[id])))
29 data_offset += len(resources[id])
30
31 # Write data.
32 for id in ids:
33 file.write(resources[id])
34
35 def main():
36 # Just write a simple file.
37 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
tony 2009/01/08 00:44:53 Maybe include some embedded nulls?
38 WriteDataPack(data, "datapack")
39 print "wrote datapack to current directory."
40
41 if __name__ == '__main__':
42 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698