diff options
Diffstat (limited to 'chromium/chrome/browser/resources/unpack_pak.py')
-rwxr-xr-x | chromium/chrome/browser/resources/unpack_pak.py | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/chromium/chrome/browser/resources/unpack_pak.py b/chromium/chrome/browser/resources/unpack_pak.py index d0c6ef69332..24b1d5f29d6 100755 --- a/chromium/chrome/browser/resources/unpack_pak.py +++ b/chromium/chrome/browser/resources/unpack_pak.py @@ -24,18 +24,31 @@ from grit.format import data_pack def ParseLine(line): return re.match(' {"([^"]+)", ([^},]+)', line) -def GetFileAndDirName(out_path, pak_dir, resource_path): - dirname = os.path.dirname(resource_path) - # When files are generated, |dirname| becomes - # @out_folder@/<gen_path>/path_to_resource. To make the structure look as if - # this file was not generated, remove @out_folder@ and <gen_path>. - if ('@out_folder@' in dirname): - dirname = os.path.relpath(dirname, os.path.join('@out_folder@', pak_dir)) - filename = os.path.basename(resource_path) - dirname = os.path.join(out_path, dirname) - return (filename, dirname) - -def Unpack(pak_path, out_path): +def UnpackResource(root_dir, out_path, excludes, resource_path, resource_text): + dirname = os.path.dirname(resource_path) + # When files are generated, |dirname| becomes + # @out_folder@/<gen_path>/path_to_resource. To make the structure look as if + # this file was not generated, remove @out_folder@ and <gen_path>. + if ('@out_folder@' in dirname): + dirname = os.path.relpath(dirname, os.path.join('@out_folder@', root_dir)) + filename = os.path.basename(resource_path) + resource_path = os.path.join(dirname, filename).replace('\\', '/') + if (resource_path in excludes): + return + + out_dir = os.path.normpath( + os.path.join(out_path, dirname)).replace('\\', '/') + assert out_dir.startswith(out_path), \ + 'Cannot unpack files to locations not in %s. %s should be removed ' \ + 'from the pak file or excluded from unpack.' \ + % (out_path, resource_path) + + if not os.path.exists(out_dir): + os.makedirs(out_dir) + with open(os.path.join(out_dir, filename), 'w') as file: + file.write(resource_text) + +def Unpack(pak_path, out_path, pak_base_dir, excludes): pak_dir = os.path.dirname(pak_path) pak_id = os.path.splitext(os.path.basename(pak_path))[0] @@ -62,23 +75,25 @@ def Unpack(pak_path, out_path): resource_filenames[res.group(2)] = res.group(1) assert resource_filenames + root_dir = pak_base_dir if pak_base_dir else pak_dir # Extract packed files, while preserving directory structure. for (resource_id, text) in data.resources.iteritems(): - (filename, dirname) = GetFileAndDirName( - out_path, pak_dir, resource_filenames[resource_ids[resource_id]]) - if not os.path.exists(dirname): - os.makedirs(dirname) - with open(os.path.join(dirname, filename), 'w') as file: - file.write(text) - + UnpackResource(root_dir, out_path, excludes or [], + resource_filenames[resource_ids[resource_id]], text) def main(): parser = argparse.ArgumentParser() parser.add_argument('--pak_file') parser.add_argument('--out_folder') + # The expected reference point/root path for files appearing in the pak file. + # If this argument is not provided, the location of the pak file will be used + # by default. + parser.add_argument('--pak_base_dir') + # Resources in the pak file which should not be unpacked. + parser.add_argument('--excludes', nargs='*') args = parser.parse_args() - Unpack(args.pak_file, args.out_folder) + Unpack(args.pak_file, args.out_folder, args.pak_base_dir, args.excludes) timestamp_file_path = os.path.join(args.out_folder, _TIMESTAMP_FILENAME) with open(timestamp_file_path, 'a'): |