blob: 1c81cb34c5e535ded06b1c171bdcc95659b0911b [file] [log] [blame]
Daisuke Nojirid853d132015-11-04 12:39:06 -08001#!/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'''
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
24def 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)
34 paths = [ os.path.join(output, x) for x in files]
35 args = ' '.join(paths)
36 command = '%s %s create %s' % (archiver, archive, args)
37 subprocess.call(command, shell=True)
38
39def archive(archiver, output):
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070040 """Archive base images and localized images
Daisuke Nojirid853d132015-11-04 12:39:06 -080041
42 Args:
43 archiver: path to the archive tool
44 output: path to the output directory
45 """
Daisuke Nojirid853d132015-11-04 12:39:06 -080046 base_images = []
47 locale_images = defaultdict(lambda: [])
Daisuke Nojirid853d132015-11-04 12:39:06 -080048
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070049 files = glob.glob(os.path.join(output, "*.bmp"))
50 for file in files:
51 base = os.path.basename(file)
52 base_images.append(base)
53
54 # create archive of base images
Daisuke Nojirid853d132015-11-04 12:39:06 -080055 archive_images(archiver, output, 'vbgfx.bin', base_images)
56
Daisuke Nojiri87f18e02016-08-30 15:49:23 -070057 dirs = glob.glob(os.path.join(output, "locale/*"))
58 for dir in dirs:
59 files = glob.glob(os.path.join(dir, "*.bmp"))
60 locale = os.path.basename(dir)
61 for file in files:
62 locale_images[locale].append(file)
63
64 # create archives of localized images
65 for locale, images in locale_images.iteritems():
66 archive_images(archiver, output, 'locale_%s.bin' % locale, images)
Daisuke Nojirid853d132015-11-04 12:39:06 -080067
68def main(args):
69 opts, args = getopt.getopt(args, 'a:d:')
70 archiver = ''
71 output = ''
72
73 for opt, arg in opts:
74 if opt == '-a':
75 archiver = arg
76 elif opt == '-d':
77 output = arg
78 else:
79 assert False, 'Invalid option'
80 if args or not archiver or not output:
81 assert False, 'Invalid usage'
82
83 archive(archiver, output)
84
85if __name__ == '__main__':
86 main(sys.argv[1:])