build_image.py revision 531bdb546af5127ed9c1012871d780930eb990f5
1#!/usr/bin/env python 2# 3# Copyright (C) 2011 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17""" 18Build image output_image_file from input_directory and properties_file. 19 20Usage: build_image input_directory properties_file output_image_file 21 22""" 23import os 24import subprocess 25import sys 26 27 28def BuildImage(in_dir, prop_dict, out_file): 29 """Build an image to out_file from in_dir with property prop_dict. 30 31 Args: 32 in_dir: path of input directory. 33 prop_dict: property dictionary. 34 out_file: path of the output image file. 35 36 Returns: 37 True iff the image is built successfully. 38 """ 39 build_command = [] 40 fs_type = prop_dict.get("fs_type", "") 41 if fs_type.startswith("ext"): 42 build_command = ["mkuserimg.sh"] 43 if "extfs_sparse_flag" in prop_dict: 44 build_command.append(prop_dict["extfs_sparse_flag"]) 45 build_command.extend([in_dir, out_file, fs_type, 46 prop_dict["mount_point"]]) 47 if "partition_size" in prop_dict: 48 build_command.append(prop_dict["partition_size"]) 49 else: 50 build_command = ["mkyaffs2image", "-f"] 51 if prop_dict.get("mkyaffs2_extra_flags", None): 52 build_command.extend(prop_dict["mkyaffs2_extra_flags"].split()) 53 build_command.append(in_dir) 54 build_command.append(out_file) 55 56 print "Running: ", " ".join(build_command) 57 p = subprocess.Popen(build_command); 58 p.communicate() 59 return p.returncode == 0 60 61 62def ImagePropFromGlobalDict(glob_dict, mount_point): 63 """Build an image property dictionary from the global dictionary. 64 65 Args: 66 glob_dict: the global dictionary from the build system. 67 mount_point: such as "system", "data" etc. 68 """ 69 d = {} 70 common_props = ( 71 "fs_type", 72 "extfs_sparse_flag", 73 "mkyaffs2_extra_flags", 74 ) 75 for p in common_props: 76 if p in glob_dict: 77 d[p] = glob_dict[p] 78 79 d["mount_point"] = mount_point 80 if mount_point == "system": 81 if "system_size" in glob_dict: 82 d["partition_size"] = str(glob_dict["system_size"]) 83 elif mount_point == "data": 84 if "userdata_size" in glob_dict: 85 d["partition_size"] = str(glob_dict["userdata_size"]) 86 elif mount_point == "cache": 87 if "cache_size" in glob_dict: 88 d["partition_size"] = str(glob_dict["cache_size"]) 89 90 return d 91 92 93def LoadGlobalDict(filename): 94 """Load "name=value" pairs from filename""" 95 d = {} 96 f = open(filename) 97 for line in f: 98 line = line.strip() 99 if not line or line.startswith("#"): 100 continue 101 k, v = line.split("=", 1) 102 d[k] = v 103 f.close() 104 return d 105 106 107def main(argv): 108 if len(argv) != 3: 109 print __doc__ 110 sys.exit(1) 111 112 in_dir = argv[0] 113 glob_dict_file = argv[1] 114 out_file = argv[2] 115 116 glob_dict = LoadGlobalDict(glob_dict_file) 117 image_filename = os.path.basename(out_file) 118 mount_point = "" 119 if image_filename == "system.img": 120 mount_point = "system" 121 elif image_filename == "userdata.img": 122 mount_point = "data" 123 elif image_filename == "cache.img": 124 mount_point = "cache" 125 126 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) 127 if not BuildImage(in_dir, image_properties, out_file): 128 print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir) 129 exit(1) 130 131 132if __name__ == '__main__': 133 main(sys.argv[1:]) 134