Commit 0e1e6eb3 authored by yavanosta's avatar yavanosta Committed by Commit Bot

Use cpp file map to unpack gzipped resources

We can get gzipped flag from gzipped_resource_map_source or
gzipped_resource_file_map_source and use it to unpack gzipped resources
with unpack.py.

Bug: 738243
Change-Id: I1bf56126f702fa6efbd2b3e8806ade9bdf904d1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1518520
Commit-Queue: Dmitry Guketlev <yavanosta@yandex-team.ru>
Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#640738}
parent 4bf11150
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
# found in the LICENSE file. # found in the LICENSE file.
import argparse import argparse
import collections
import cStringIO
import gzip
import os import os
import re import re
import sys import sys
...@@ -20,9 +23,16 @@ _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) ...@@ -20,9 +23,16 @@ _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..'))
sys.path.insert(1, os.path.join(_SRC_PATH, 'tools', 'grit')) sys.path.insert(1, os.path.join(_SRC_PATH, 'tools', 'grit'))
from grit.format import data_pack from grit.format import data_pack
ResourceFile = collections.namedtuple('ResourceFile',
['path', 'gzipped'])
def UngzipString(data):
# Ungzipping using Python's built in gzip.
with gzip.GzipFile(fileobj=cStringIO.StringIO(data)) as gzip_file:
return gzip_file.read()
def ParseLine(line): def ParseLine(line):
return re.match(' {"([^"]+)", ([^},]+)', line) return re.match(' {"([^"]+)", ([^},]+)(?:, ([^},]+))?', line)
def Unpack(pak_path, out_path): def Unpack(pak_path, out_path):
...@@ -43,23 +53,29 @@ def Unpack(pak_path, out_path): ...@@ -43,23 +53,29 @@ def Unpack(pak_path, out_path):
assert resource_ids assert resource_ids
# Associate numerical string IDs to files. # Associate numerical string IDs to files.
resource_filenames = dict() resource_files = dict()
resources_map_path = os.path.join(pak_dir, 'grit', pak_id + '_map.cc') resources_map_path = os.path.join(pak_dir, 'grit', pak_id + '_map.cc')
with open(resources_map_path) as resources_map: with open(resources_map_path) as resources_map:
for line in resources_map: for line in resources_map:
res = ParseLine(line) res = ParseLine(line)
if res: if res:
resource_filenames[res.group(2)] = res.group(1) resource_files[res.group(2)] = ResourceFile(
assert resource_filenames path=res.group(1),
gzipped=res.group(3) == 'true')
assert resource_files
# Extract packed files, while preserving directory structure. # Extract packed files, while preserving directory structure.
for (resource_id, text) in data.resources.iteritems(): for (resource_id, text) in data.resources.iteritems():
filename = resource_filenames[resource_ids[resource_id]] resource_file = resource_files[resource_ids[resource_id]]
dirname = os.path.join(out_path, os.path.dirname(filename)) file_path = resource_file.path
if not os.path.exists(dirname): file_gzipped = resource_file.gzipped
os.makedirs(dirname) file_dir = os.path.join(out_path, os.path.dirname(file_path))
with open(os.path.join(out_path, filename), 'w') as file: if not os.path.exists(file_dir):
file.write(text) os.makedirs(file_dir)
if file_gzipped:
text = UngzipString(text)
with open(os.path.join(out_path, file_path), 'w') as f:
f.write(text)
def main(): def main():
......
...@@ -15,6 +15,13 @@ class UnpackPakTest(unittest.TestCase): ...@@ -15,6 +15,13 @@ class UnpackPakTest(unittest.TestCase):
self.assertTrue(unpack_pak.ParseLine(' {"path.js", IDR_PATH, false}')) self.assertTrue(unpack_pak.ParseLine(' {"path.js", IDR_PATH, false}'))
self.assertTrue(unpack_pak.ParseLine(' {"path.js", IDR_PATH, true}')) self.assertTrue(unpack_pak.ParseLine(' {"path.js", IDR_PATH, true}'))
def testUngzipString(self):
self.assertEqual(
unpack_pak.UngzipString(
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xcbH\xcd\xc9\xc9W' +
'(\xcf/\xcaI\x01\x00\x85\x11J\r\x0b\x00\x00\x00'),
'hello world')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment