blob: f9cc709b3a71c3157f01547cd9a2bada9f07a955 [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
19import os
20import subprocess
21import sys
22import yaml
23
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):
40 """Archive language indepdent and depndent images.
41
42 Args:
43 archiver: path to the archive tool
44 output: path to the output directory
45 """
46 # Everything comes from DEFAULT.yaml
47 default_yaml = os.path.join(output, 'DEFAULT.yaml')
48 with open(default_yaml, 'r') as yaml_file:
49 config = yaml.load(yaml_file)
50
51 # image section contains list of images used by the board
52 config_images = config['images']
53 base_images = []
54 locale_images = defaultdict(lambda: [])
55 for name, path in config_images.iteritems():
56 dir = os.path.dirname(path)
57 base = os.path.basename(path)
58 if not dir:
59 # language independent files are placed at root dir
60 base_images.append(base)
61 else:
62 # assume everything else is language dependent files
63 lang = os.path.basename(dir)
64 locale_images[lang].append(path)
65
66 # create archive for base (language independent) images
67 archive_images(archiver, output, 'vbgfx.bin', base_images)
68
69 # create archives for language dependent files
70 for lang, images in locale_images.iteritems():
71 archive_images(archiver, output, 'locale_%s.bin' % lang, images)
72
73def main(args):
74 opts, args = getopt.getopt(args, 'a:d:')
75 archiver = ''
76 output = ''
77
78 for opt, arg in opts:
79 if opt == '-a':
80 archiver = arg
81 elif opt == '-d':
82 output = arg
83 else:
84 assert False, 'Invalid option'
85 if args or not archiver or not output:
86 assert False, 'Invalid usage'
87
88 archive(archiver, output)
89
90if __name__ == '__main__':
91 main(sys.argv[1:])