Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium OS 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 | ''' |
| 7 | Usage: |
| 8 | ./archive_images.py -a path_to_archiver -d input_output_dir |
| 9 | |
| 10 | input_output_dir should points to the directory where images are created by |
| 11 | build_images.py. The script outputs archives to input_output_dir. |
| 12 | |
| 13 | path_to_archiver should points to the tool which bundles files into a blob, |
| 14 | which can be unpacked by Depthcharge. |
| 15 | ''' |
| 16 | |
| 17 | from collections import defaultdict |
| 18 | import getopt |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 19 | import glob |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 20 | import os |
| 21 | import subprocess |
| 22 | import sys |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 23 | |
| 24 | def archive_images(archiver, output, name, files): |
| 25 | """Archive files. |
| 26 | |
| 27 | Args: |
| 28 | archiver: path to the archive tool |
| 29 | output: path to the output directory |
| 30 | name: name of the archive file |
| 31 | files: list of files to be archived |
| 32 | """ |
| 33 | archive = os.path.join(output, name) |
Yu-Ping Wu | 462f4cc | 2019-12-19 09:14:56 +0800 | [diff] [blame^] | 34 | args = ' '.join(files) |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 35 | command = '%s %s create %s' % (archiver, archive, args) |
| 36 | subprocess.call(command, shell=True) |
| 37 | |
| 38 | def archive(archiver, output): |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 39 | """Archive base images and localized images |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 40 | |
| 41 | Args: |
| 42 | archiver: path to the archive tool |
| 43 | output: path to the output directory |
| 44 | """ |
Yu-Ping Wu | 462f4cc | 2019-12-19 09:14:56 +0800 | [diff] [blame^] | 45 | base_images = glob.glob(os.path.join(output, "*.bmp")) |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 46 | |
| 47 | # create archive of base images |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 48 | archive_images(archiver, output, 'vbgfx.bin', base_images) |
| 49 | |
Yu-Ping Wu | 462f4cc | 2019-12-19 09:14:56 +0800 | [diff] [blame^] | 50 | locale_images = defaultdict(lambda: []) |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 51 | dirs = glob.glob(os.path.join(output, "locale/*")) |
| 52 | for dir in dirs: |
| 53 | files = glob.glob(os.path.join(dir, "*.bmp")) |
| 54 | locale = os.path.basename(dir) |
| 55 | for file in files: |
| 56 | locale_images[locale].append(file) |
| 57 | |
| 58 | # create archives of localized images |
| 59 | for locale, images in locale_images.iteritems(): |
| 60 | archive_images(archiver, output, 'locale_%s.bin' % locale, images) |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 61 | |
| 62 | def main(args): |
| 63 | opts, args = getopt.getopt(args, 'a:d:') |
| 64 | archiver = '' |
| 65 | output = '' |
| 66 | |
| 67 | for opt, arg in opts: |
| 68 | if opt == '-a': |
| 69 | archiver = arg |
| 70 | elif opt == '-d': |
| 71 | output = arg |
| 72 | else: |
| 73 | assert False, 'Invalid option' |
| 74 | if args or not archiver or not output: |
| 75 | assert False, 'Invalid usage' |
| 76 | |
| 77 | archive(archiver, output) |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | main(sys.argv[1:]) |