blob: 25409a54973f7457c83bd2e002b712d669c834d7 [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.
5
6'''
7Usage:
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
17from collections import defaultdict
18import getopt
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070019import glob
Daisuke Nojirid853d132015-11-04 12:39:06 -080020import os
21import subprocess
22import sys
Daisuke Nojirid853d132015-11-04 12:39:06 -080023
Chun-Ta Lin0b578472020-09-27 12:08:12 +080024
25LOCALE_DIR = 'locale'
26LOCALE_RO_DIR = os.path.join(LOCALE_DIR, 'ro')
27LOCALE_RW_DIR = os.path.join(LOCALE_DIR, 'rw')
28
29
Daisuke Nojirid853d132015-11-04 12:39:06 -080030def 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 Wu462f4cc2019-12-19 09:14:56 +080040 args = ' '.join(files)
Daisuke Nojirid853d132015-11-04 12:39:06 -080041 command = '%s %s create %s' % (archiver, archive, args)
Yu-Ping Wue85f9782019-12-25 16:47:14 +080042 subprocess.check_call(command, shell=True)
Daisuke Nojirid853d132015-11-04 12:39:06 -080043
Chun-Ta Lin0b578472020-09-27 12:08:12 +080044
45def archive_base(archiver, output):
46 """Archive base (locale-independent) images.
Daisuke Nojirid853d132015-11-04 12:39:06 -080047
48 Args:
49 archiver: path to the archive tool
50 output: path to the output directory
51 """
Chun-Ta Lin0b578472020-09-27 12:08:12 +080052 base_images = glob.glob(os.path.join(output, '*.bmp'))
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070053
54 # create archive of base images
Daisuke Nojirid853d132015-11-04 12:39:06 -080055 archive_images(archiver, output, 'vbgfx.bin', base_images)
56
Chun-Ta Lin0b578472020-09-27 12:08:12 +080057
58def 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 Wu462f4cc2019-12-19 09:14:56 +080066 locale_images = defaultdict(lambda: [])
Chun-Ta Lin0b578472020-09-27 12:08:12 +080067 dirs = glob.glob(os.path.join(output, '*'))
68
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070069 for dir in dirs:
Chun-Ta Lin0b578472020-09-27 12:08:12 +080070 files = glob.glob(os.path.join(dir, '*.bmp'))
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070071 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 Wud71b4452020-06-16 11:00:26 +080076 for locale, images in locale_images.items():
Chun-Ta Lin0b578472020-09-27 12:08:12 +080077 archive_images(archiver, output, pattern % locale, images)
78
Daisuke Nojirid853d132015-11-04 12:39:06 -080079
80def 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 Lin0b578472020-09-27 12:08:12 +080095 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 Nojirid853d132015-11-04 12:39:06 -0800105
106if __name__ == '__main__':
107 main(sys.argv[1:])