OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 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 import os |
| 7 import re |
| 8 import sys |
| 9 |
| 10 |
| 11 _HERE_PATH = os.path.join(os.path.dirname(__file__)) |
| 12 |
| 13 |
| 14 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| 15 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'grit')) |
| 16 from grit.format import data_pack |
| 17 |
| 18 |
| 19 def unpack(pak_path, out_path): |
| 20 pak_dir = os.path.dirname(pak_path) |
| 21 pak_id = os.path.splitext(os.path.basename(pak_path))[0] |
| 22 |
| 23 data = data_pack.DataPack.ReadDataPack(pak_path) |
| 24 |
| 25 # Associate numerical grit IDs to strings. |
| 26 # For example 120045 -> 'IDR_SETTINGS_ABOUT_PAGE_HTML' |
| 27 resource_ids = dict() |
| 28 resources_path = os.path.join(pak_dir, 'grit', pak_id + '.h') |
| 29 with open(resources_path) as resources_file: |
| 30 for line in resources_file: |
| 31 res = re.match('#define ([^ ]+) (\d+)', line) |
| 32 if res: |
| 33 resource_ids[int(res.group(2))] = res.group(1) |
| 34 assert resource_ids |
| 35 |
| 36 # Associate numerical string IDs to files. |
| 37 resource_filenames = dict() |
| 38 resources_map_path = os.path.join(pak_dir, 'grit', pak_id + '_map.cc') |
| 39 with open(resources_map_path) as resources_map: |
| 40 for line in resources_map: |
| 41 res = re.match(' {"([^"]+)", ([^}]+)', line) |
| 42 if res: |
| 43 resource_filenames[res.group(2)] = res.group(1) |
| 44 assert resource_filenames |
| 45 |
| 46 # Extract packed files, while preserving directory structure. |
| 47 for (resource_id, text) in data.resources.iteritems(): |
| 48 filename = resource_filenames[resource_ids[resource_id]] |
| 49 dirname = os.path.join(out_path, os.path.dirname(filename)) |
| 50 if not os.path.exists(dirname): |
| 51 os.makedirs(dirname) |
| 52 with open(os.path.join(out_path, filename), 'w') as file: |
| 53 file.write(text) |
OLD | NEW |