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)} |
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 main(): | 71 def main(): |
66 parser = argparse.ArgumentParser( | 72 parser = argparse.ArgumentParser( |
67 description=__doc__, formatter_class=argparse.RawTextHelpFormatter) | 73 description=__doc__, formatter_class=argparse.RawTextHelpFormatter) |
68 sub_parsers = parser.add_subparsers() | 74 sub_parsers = parser.add_subparsers() |
69 | 75 |
70 sub_parser = sub_parsers.add_parser('repack', | 76 sub_parser = sub_parsers.add_parser('repack', |
71 help='Combines several .pak files into one.') | 77 help='Combines several .pak files into one.') |
(...skipping 23 matching lines...) Expand all Loading... |
95 elif len(sys.argv) == 2 and sys.argv[1] in actions: | 101 elif len(sys.argv) == 2 and sys.argv[1] in actions: |
96 parser.parse_args(sys.argv[1:] + ['-h']) | 102 parser.parse_args(sys.argv[1:] + ['-h']) |
97 sys.exit(1) | 103 sys.exit(1) |
98 | 104 |
99 args = parser.parse_args() | 105 args = parser.parse_args() |
100 args.func(args) | 106 args.func(args) |
101 | 107 |
102 | 108 |
103 if __name__ == '__main__': | 109 if __name__ == '__main__': |
104 main() | 110 main() |
OLD | NEW |