Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 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 | |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 24 | |
| 25 | LOCALE_DIR = 'locale' |
| 26 | LOCALE_RO_DIR = os.path.join(LOCALE_DIR, 'ro') |
| 27 | LOCALE_RW_DIR = os.path.join(LOCALE_DIR, 'rw') |
| 28 | |
| 29 | |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 30 | def archive_images(archiver, output, name, files): |
| 31 | """Archive files. |
| 32 | |
| 33 | Args: |
| 34 | archiver: path to the archive tool |
| 35 | output: path to the output directory |
| 36 | name: name of the archive file |
| 37 | files: list of files to be archived |
| 38 | """ |
| 39 | archive = os.path.join(output, name) |
Yu-Ping Wu | 462f4cc | 2019-12-19 09:14:56 +0800 | [diff] [blame] | 40 | args = ' '.join(files) |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 41 | command = '%s %s create %s' % (archiver, archive, args) |
Yu-Ping Wu | e85f978 | 2019-12-25 16:47:14 +0800 | [diff] [blame] | 42 | subprocess.check_call(command, shell=True) |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 43 | |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 44 | |
| 45 | def archive_base(archiver, output): |
| 46 | """Archive base (locale-independent) images. |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 47 | |
| 48 | Args: |
| 49 | archiver: path to the archive tool |
| 50 | output: path to the output directory |
| 51 | """ |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 52 | base_images = glob.glob(os.path.join(output, '*.bmp')) |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 53 | |
| 54 | # create archive of base images |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 55 | archive_images(archiver, output, 'vbgfx.bin', base_images) |
| 56 | |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 57 | |
| 58 | def archive_localized(archiver, output, pattern): |
| 59 | """Archive localized images. |
| 60 | |
| 61 | Args: |
| 62 | archiver: path to the archive tool |
| 63 | output: path to the output directory |
| 64 | pattern: filename with a '%s' to fill in the locale code |
| 65 | """ |
Yu-Ping Wu | 462f4cc | 2019-12-19 09:14:56 +0800 | [diff] [blame] | 66 | locale_images = defaultdict(lambda: []) |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 67 | dirs = glob.glob(os.path.join(output, '*')) |
| 68 | |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 69 | for dir in dirs: |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 70 | files = glob.glob(os.path.join(dir, '*.bmp')) |
Daisuke Nojiri | 87f18e0 | 2016-08-30 15:49:23 -0700 | [diff] [blame] | 71 | locale = os.path.basename(dir) |
| 72 | for file in files: |
| 73 | locale_images[locale].append(file) |
| 74 | |
| 75 | # create archives of localized images |
Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 76 | for locale, images in locale_images.items(): |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 77 | archive_images(archiver, output, pattern % locale, images) |
| 78 | |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 79 | |
| 80 | def main(args): |
| 81 | opts, args = getopt.getopt(args, 'a:d:') |
| 82 | archiver = '' |
| 83 | output = '' |
| 84 | |
| 85 | for opt, arg in opts: |
| 86 | if opt == '-a': |
| 87 | archiver = arg |
| 88 | elif opt == '-d': |
| 89 | output = arg |
| 90 | else: |
| 91 | assert False, 'Invalid option' |
| 92 | if args or not archiver or not output: |
| 93 | assert False, 'Invalid usage' |
| 94 | |
Chun-Ta Lin | 0b57847 | 2020-09-27 12:08:12 +0800 | [diff] [blame^] | 95 | print('Archiving vbfgx.bin', file=sys.stderr, flush=True) |
| 96 | archive_base(archiver, output) |
| 97 | print('Archiving locales for RO', file=sys.stderr, flush=True) |
| 98 | ro_locale_dir = os.path.join(output, LOCALE_RO_DIR) |
| 99 | rw_locale_dir = os.path.join(output, LOCALE_RW_DIR) |
| 100 | archive_localized(archiver, ro_locale_dir, 'locale_%s.bin') |
| 101 | if os.path.exists(rw_locale_dir): |
| 102 | print('Archiving locales for RW', file=sys.stderr, flush=True) |
| 103 | archive_localized(archiver, rw_locale_dir, 'rw_locale_%s.bin') |
| 104 | |
Daisuke Nojiri | d853d13 | 2015-11-04 12:39:06 -0800 | [diff] [blame] | 105 | |
| 106 | if __name__ == '__main__': |
| 107 | main(sys.argv[1:]) |