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

Side by Side Diff: tools/grit/pak_util.py

Issue 2969123002: Add deduplication logic to .pak files (Closed)
Patch Set: sizeof() Created 3 years, 5 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 | « tools/grit/grit/format/data_pack_unittest.py ('k') | ui/base/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2017 The Chromium Authors. All rights reserved. 2 # Copyright 2017 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 tool for interacting with .pak files. 6 """A tool for interacting with .pak files.
7 7
8 For details on the pak file format, see: 8 For details on the pak file format, see:
9 https://dev.chromium.org/developers/design-documents/linuxresourcesandlocalizeds trings 9 https://dev.chromium.org/developers/design-documents/linuxresourcesandlocalizeds trings
10 """ 10 """
(...skipping 15 matching lines...) Expand all
26 pak = data_pack.ReadDataPack(args.pak_file) 26 pak = data_pack.ReadDataPack(args.pak_file)
27 27
28 for resource_id, payload in pak.resources.iteritems(): 28 for resource_id, payload in pak.resources.iteritems():
29 path = os.path.join(args.output_dir, str(resource_id)) 29 path = os.path.join(args.output_dir, str(resource_id))
30 with open(path, 'w') as f: 30 with open(path, 'w') as f:
31 f.write(payload) 31 f.write(payload)
32 32
33 33
34 def _PrintMain(args): 34 def _PrintMain(args):
35 pak = data_pack.ReadDataPack(args.pak_file) 35 pak = data_pack.ReadDataPack(args.pak_file)
36 id_map = {id(v): k for k, v in sorted(pak.resources.items(), reverse=True)}
36 encoding = 'binary' 37 encoding = 'binary'
37 if pak.encoding == 1: 38 if pak.encoding == 1:
38 encoding = 'utf-8' 39 encoding = 'utf-8'
39 elif pak.encoding == 2: 40 elif pak.encoding == 2:
40 encoding = 'utf-16' 41 encoding = 'utf-16'
41 else: 42 else:
42 encoding = '?' + str(pak.encoding) 43 encoding = '?' + str(pak.encoding)
43 print 'Encoding:', encoding 44 print 'Encoding:', encoding
44 45
45 try_decode = encoding.startswith('utf') 46 try_decode = encoding.startswith('utf')
46 # Print IDs in ascending order, since that's the order in which they appear in 47 # Print IDs in ascending order, since that's the order in which they appear in
47 # the file (order is lost by Python dict). 48 # the file (order is lost by Python dict).
48 for resource_id in sorted(pak.resources): 49 for resource_id in sorted(pak.resources):
49 data = pak.resources[resource_id] 50 data = pak.resources[resource_id]
50 desc = '<binary>' 51 desc = '<binary>'
51 if try_decode: 52 if try_decode:
52 try: 53 try:
53 desc = unicode(data, encoding) 54 desc = unicode(data, encoding)
54 if len(desc) > 60: 55 if len(desc) > 60:
55 desc = desc[:60] + u'...' 56 desc = desc[:60] + u'...'
56 desc = desc.replace('\n', '\\n') 57 desc = desc.replace('\n', '\\n')
57 except UnicodeDecodeError: 58 except UnicodeDecodeError:
58 pass 59 pass
59 sha1 = hashlib.sha1(data).hexdigest()[:10] 60 sha1 = hashlib.sha1(data).hexdigest()[:10]
60 line = u'Entry(id={}, len={}, sha1={}): {}'.format( 61 canonical_id = id_map[id(data)]
61 resource_id, len(data), sha1, desc) 62 if resource_id == canonical_id:
63 line = u'Entry(id={}, len={}, sha1={}): {}'.format(
64 resource_id, len(data), sha1, desc)
65 else:
66 line = u'Entry(id={}, alias_of={}): {}'.format(
67 resource_id, canonical_id, desc)
62 print line.encode('utf-8') 68 print line.encode('utf-8')
63 69
64 70
65 def _ListMain(args): 71 def _ListMain(args):
66 resources, _ = data_pack.ReadDataPack(args.pak_file) 72 resources, _ = data_pack.ReadDataPack(args.pak_file)
67 for resource_id in sorted(resources.keys()): 73 for resource_id in sorted(resources.keys()):
68 args.output.write('%d\n' % resource_id) 74 args.output.write('%d\n' % resource_id)
69 75
70 76
71 def main(): 77 def main():
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 elif len(sys.argv) == 2 and sys.argv[1] in actions: 115 elif len(sys.argv) == 2 and sys.argv[1] in actions:
110 parser.parse_args(sys.argv[1:] + ['-h']) 116 parser.parse_args(sys.argv[1:] + ['-h'])
111 sys.exit(1) 117 sys.exit(1)
112 118
113 args = parser.parse_args() 119 args = parser.parse_args()
114 args.func(args) 120 args.func(args)
115 121
116 122
117 if __name__ == '__main__': 123 if __name__ == '__main__':
118 main() 124 main()
OLDNEW
« no previous file with comments | « tools/grit/grit/format/data_pack_unittest.py ('k') | ui/base/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698