OLD | NEW |
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 Loading... |
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)} | |
37 encoding = 'binary' | 36 encoding = 'binary' |
38 if pak.encoding == 1: | 37 if pak.encoding == 1: |
39 encoding = 'utf-8' | 38 encoding = 'utf-8' |
40 elif pak.encoding == 2: | 39 elif pak.encoding == 2: |
41 encoding = 'utf-16' | 40 encoding = 'utf-16' |
42 else: | 41 else: |
43 encoding = '?' + str(pak.encoding) | 42 encoding = '?' + str(pak.encoding) |
44 print 'Encoding:', encoding | 43 print 'Encoding:', encoding |
45 | 44 |
46 try_decode = encoding.startswith('utf') | 45 try_decode = encoding.startswith('utf') |
47 # Print IDs in ascending order, since that's the order in which they appear in | 46 # Print IDs in ascending order, since that's the order in which they appear in |
48 # the file (order is lost by Python dict). | 47 # the file (order is lost by Python dict). |
49 for resource_id in sorted(pak.resources): | 48 for resource_id in sorted(pak.resources): |
50 data = pak.resources[resource_id] | 49 data = pak.resources[resource_id] |
51 desc = '<binary>' | 50 desc = '<binary>' |
52 if try_decode: | 51 if try_decode: |
53 try: | 52 try: |
54 desc = unicode(data, encoding) | 53 desc = unicode(data, encoding) |
55 if len(desc) > 60: | 54 if len(desc) > 60: |
56 desc = desc[:60] + u'...' | 55 desc = desc[:60] + u'...' |
57 desc = desc.replace('\n', '\\n') | 56 desc = desc.replace('\n', '\\n') |
58 except UnicodeDecodeError: | 57 except UnicodeDecodeError: |
59 pass | 58 pass |
60 sha1 = hashlib.sha1(data).hexdigest()[:10] | 59 sha1 = hashlib.sha1(data).hexdigest()[:10] |
61 canonical_id = id_map[id(data)] | 60 line = u'Entry(id={}, len={}, sha1={}): {}'.format( |
62 if resource_id == canonical_id: | 61 resource_id, len(data), sha1, desc) |
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) | |
68 print line.encode('utf-8') | 62 print line.encode('utf-8') |
69 | 63 |
70 | 64 |
71 def _ListMain(args): | 65 def _ListMain(args): |
72 resources, _ = data_pack.ReadDataPack(args.pak_file) | 66 resources, _ = data_pack.ReadDataPack(args.pak_file) |
73 for resource_id in sorted(resources.keys()): | 67 for resource_id in sorted(resources.keys()): |
74 args.output.write('%d\n' % resource_id) | 68 args.output.write('%d\n' % resource_id) |
75 | 69 |
76 | 70 |
77 def main(): | 71 def main(): |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 elif len(sys.argv) == 2 and sys.argv[1] in actions: | 109 elif len(sys.argv) == 2 and sys.argv[1] in actions: |
116 parser.parse_args(sys.argv[1:] + ['-h']) | 110 parser.parse_args(sys.argv[1:] + ['-h']) |
117 sys.exit(1) | 111 sys.exit(1) |
118 | 112 |
119 args = parser.parse_args() | 113 args = parser.parse_args() |
120 args.func(args) | 114 args.func(args) |
121 | 115 |
122 | 116 |
123 if __name__ == '__main__': | 117 if __name__ == '__main__': |
124 main() | 118 main() |
OLD | NEW |