Chromium Code Reviews| Index: chrome/browser/resources/unpack_pak.py |
| diff --git a/chrome/browser/resources/unpack_pak.py b/chrome/browser/resources/unpack_pak.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..904fb8010e6a29d2506004975da2dc7b01f02fc2 |
| --- /dev/null |
| +++ b/chrome/browser/resources/unpack_pak.py |
| @@ -0,0 +1,59 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os |
| +import re |
| +import sys |
| + |
| +_HERE_PATH = os.path.join(os.path.dirname(__file__)) |
| +_SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| +sys.path.append(os.path.join(_SRC_PATH, 'tools', 'grit')) |
| +from grit.format import data_pack |
| + |
| +def unpack(pak_path, out_path): |
| + pak_dir = os.path.dirname(pak_path); |
| + pak_id = os.path.splitext(os.path.basename(pak_path))[0] |
| + |
| + data = data_pack.DataPack.ReadDataPack(pak_path) |
| + |
| + # Associate numerical grit IDs to strings. |
| + # For example 120045 -> 'IDR_SETTINGS_ABOUT_PAGE_HTML' |
| + resource_ids = dict() |
| + resources_path = os.path.join(pak_dir, 'grit', pak_id + '.h') |
| + with open(resources_path) as resources_file: |
| + for line in resources_file: |
| + res = re.match('#define ([^ ]+) (\d+)', line) |
|
dschuyler
2017/01/12 00:58:10
This is cool as-is, but I wanted to offer a tip in
dschuyler
2017/01/12 01:10:55
Whoops, the ^ is redundant with re.match().
(The
|
| + if res: |
| + resource_ids[int(res.group(2))] = res.group(1) |
| + |
| + # Associate numerical string IDs to files. |
| + resource_filenames = dict() |
| + resources_map_path = os.path.join(pak_dir, 'grit', pak_id + '_map.cc') |
| + with open(resources_map_path) as resources_map: |
| + for line in resources_map: |
| + res = re.match(' {"([^"]+)", ([^}]+)', line) |
| + if res: |
| + resource_filenames[res.group(2)] = res.group(1) |
| + |
| + # Extract packed files, while preserving directory structure. |
| + for (resource_id, text) in data.resources.iteritems(): |
| + #print '%s: %s' % (resource_id, text) |
| + |
| + filename = resource_filenames[resource_ids[resource_id]] |
| + dirname = os.path.join(out_path, os.path.dirname(filename)) |
| + if not os.path.exists(dirname): |
| + os.makedirs(dirname) |
| + with open(os.path.join(out_path, filename), 'w') as file: |
| + file.write(text) |
| + |
| +if __name__ == '__main__': |
| + pak_path = os.path.join( |
| + _SRC_PATH, |
| + 'out/gchrome_gn/gen/chrome/browser/resources/settings/' + |
| + 'settings_resources.pak'); |
| + out_path = os.path.join( |
| + _SRC_PATH, |
| + 'out/gchrome_gn/gen/chrome/browser/resources/settings/flattened'); |
| + unpack(pak_path, out_path) |