Skip to content

Commit

Permalink
pak_util.py: Make decompress files when extracting
Browse files Browse the repository at this point in the history
Useful when trying to inspect pak files now that many entries are
compressed.

Bug: 1271380
Change-Id: I83bb25f3d87b02686fa72deca01cb2c9ec92429c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3295870
Auto-Submit: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Reviewed-by: Peter Wen <wnwen@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#944705}
  • Loading branch information
agrieve authored and Chromium LUCI CQ committed Nov 23, 2021
1 parent a733af2 commit e172ced
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions testing/scripts/run_isolated_script_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
'run_blinkpy_tests.py',
'run_mac_signing_tests.py',
'run_mini_installer_tests.py',
'test_suite_all.py', # //tools/grit:grit_python_unittests
}

KNOWN_TYP_VPYTHON3_TEST_RUNNERS = {
'monochrome_python_tests.py',
'run_polymer_tools_tests.py',
'test_suite_all.py', # //tools/grit:grit_python_unittests
}

class IsolatedScriptTestAdapter(common.BaseIsolatedScriptArgsAdapter):
Expand Down
33 changes: 33 additions & 0 deletions tools/grit/pak_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import hashlib
import os
import shutil
import subprocess
import sys
import tempfile

Expand All @@ -25,8 +26,11 @@

import six

from grit import constants
from grit.format import data_pack

_GZIP_HEADER = b'\x1f\x8b'


def _RepackMain(args):
output_info_filepath = args.output_pak_file + '.info'
Expand All @@ -52,6 +56,26 @@ def _RepackMain(args):
shutil.copyfileobj(temp_outfile, outgz)


def _MaybeDecompress(payload, brotli_path=None):
if payload.startswith(_GZIP_HEADER):
return gzip.decompress(payload)
if payload.startswith(constants.BROTLI_CONST):
shell = brotli_path is None
brotli_path = brotli_path or 'brotli'
# Size is 6 bytes. We don't need it, so skip it.
try:
result = subprocess.run([brotli_path, '--decompress', '--stdout'],
shell=shell,
input=payload[8:],
stdout=subprocess.PIPE,
check=True)
except subprocess.CalledProcessError as e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
return result.stdout
return payload


def _ExtractMain(args):
pak = data_pack.ReadDataPack(args.pak_file)
if args.textual_id:
Expand All @@ -62,6 +86,8 @@ def _ExtractMain(args):
if args.textual_id else str(resource_id))
path = os.path.join(args.output_dir, filename)
with open(path, 'wb') as f:
if not args.raw:
payload = _MaybeDecompress(payload, args.brotli)
f.write(payload)


Expand Down Expand Up @@ -174,6 +200,13 @@ def main():
sub_parser.add_argument('pak_file')
sub_parser.add_argument('-o', '--output-dir', default='.',
help='Directory to extract to.')
sub_parser.add_argument('--raw',
action='store_true',
help='Do not decompress when extracting.')
sub_parser.add_argument('--brotli',
help='Path to brotli executable. Needed only to '
'decompress brotli-compressed entries. For a '
'chromium checkout, find in your output directory')
sub_parser.add_argument(
'-t',
'--textual-id',
Expand Down

0 comments on commit e172ced

Please sign in to comment.