blob: 9f6d2b71e0cb47fbf6ef2434dd19036ad761ca6e [file] [log] [blame]
Yu-Ping Wud71b4452020-06-16 11:00:26 +08001#!/usr/bin/env python
Daisuke Nojirid853d132015-11-04 12:39:06 -08002# 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 Wud6a7abb2021-04-14 15:36:31 +08005"""
Daisuke Nojirid853d132015-11-04 12:39:06 -08006Usage:
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 Wud6a7abb2021-04-14 15:36:31 +080014"""
Daisuke Nojirid853d132015-11-04 12:39:06 -080015
16from collections import defaultdict
17import getopt
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070018import glob
Daisuke Nojirid853d132015-11-04 12:39:06 -080019import os
20import subprocess
21import sys
Daisuke Nojirid853d132015-11-04 12:39:06 -080022
Chun-Ta Lin0b578472020-09-27 12:08:12 +080023LOCALE_DIR = 'locale'
24LOCALE_RO_DIR = os.path.join(LOCALE_DIR, 'ro')
25LOCALE_RW_DIR = os.path.join(LOCALE_DIR, 'rw')
26
27
Daisuke Nojirid853d132015-11-04 12:39:06 -080028def archive_images(archiver, output, name, files):
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080029 """Archives files.
Daisuke Nojirid853d132015-11-04 12:39:06 -080030
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080031 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 Nojirid853d132015-11-04 12:39:06 -080041
Chun-Ta Lin0b578472020-09-27 12:08:12 +080042
43def archive_base(archiver, output):
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080044 """Archives base (locale-independent) images.
Daisuke Nojirid853d132015-11-04 12:39:06 -080045
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080046 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 Nojiri87f18e02016-08-30 15:49:23 -070051
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080052 # create archive of base images
53 archive_images(archiver, output, 'vbgfx.bin', base_images)
Daisuke Nojirid853d132015-11-04 12:39:06 -080054
Chun-Ta Lin0b578472020-09-27 12:08:12 +080055
56def archive_localized(archiver, output, pattern):
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080057 """Archives localized images.
Chun-Ta Lin0b578472020-09-27 12:08:12 +080058
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080059 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 Lin0b578472020-09-27 12:08:12 +080065
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080066 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 Nojiri87f18e02016-08-30 15:49:23 -070071
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080072 # create archives of localized images
73 for locale, images in locale_images.items():
74 archive_images(archiver, output, pattern % locale, images)
Chun-Ta Lin0b578472020-09-27 12:08:12 +080075
Daisuke Nojirid853d132015-11-04 12:39:06 -080076
77def main(args):
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080078 """Archives images."""
79 opts, args = getopt.getopt(args, 'a:d:')
80 archiver = ''
81 output = ''
Daisuke Nojirid853d132015-11-04 12:39:06 -080082
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080083 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 Nojirid853d132015-11-04 12:39:06 -080092
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +080093 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 Lin0b578472020-09-27 12:08:12 +0800102
Daisuke Nojirid853d132015-11-04 12:39:06 -0800103
104if __name__ == '__main__':
Yu-Ping Wud6a7abb2021-04-14 15:36:31 +0800105 main(sys.argv[1:])