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