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 if "selinux_fc" in prop_dict: 50 build_command.append(prop_dict["selinux_fc"]) 51 else: 52 build_command = ["mkyaffs2image", "-f"] 53 if prop_dict.get("mkyaffs2_extra_flags", None): 54 build_command.extend(prop_dict["mkyaffs2_extra_flags"].split()) 55 build_command.append(in_dir) 56 build_command.append(out_file) 57 if "selinux_fc" in prop_dict: 58 build_command.append(prop_dict["selinux_fc"]) 59 build_command.append(prop_dict["mount_point"]) 60 61 print "Running: ", " ".join(build_command) 62 p = subprocess.Popen(build_command); 63 p.communicate() 64 return p.returncode == 0 65 66 67def ImagePropFromGlobalDict(glob_dict, mount_point): 68 """Build an image property dictionary from the global dictionary. 69 70 Args: 71 glob_dict: the global dictionary from the build system. 72 mount_point: such as "system", "data" etc. 73 """ 74 d = {} 75 76 def copy_prop(src_p, dest_p): 77 if src_p in glob_dict: 78 d[dest_p] = str(glob_dict[src_p]) 79 80 common_props = ( 81 "extfs_sparse_flag", 82 "mkyaffs2_extra_flags", 83 "selinux_fc", 84 ) 85 for p in common_props: 86 copy_prop(p, p) 87 88 d["mount_point"] = mount_point 89 if mount_point == "system": 90 copy_prop("fs_type", "fs_type") 91 copy_prop("system_size", "partition_size") 92 elif mount_point == "data": 93 copy_prop("fs_type", "fs_type") 94 copy_prop("userdata_size", "partition_size") 95 elif mount_point == "cache": 96 copy_prop("cache_fs_type", "fs_type") 97 copy_prop("cache_size", "partition_size") 98 99 return d 100 101 102def LoadGlobalDict(filename): 103 """Load "name=value" pairs from filename""" 104 d = {} 105 f = open(filename) 106 for line in f: 107 line = line.strip() 108 if not line or line.startswith("#"): 109 continue 110 k, v = line.split("=", 1) 111 d[k] = v 112 f.close() 113 return d 114 115 116def main(argv): 117 if len(argv) != 3: 118 print __doc__ 119 sys.exit(1) 120 121 in_dir = argv[0] 122 glob_dict_file = argv[1] 123 out_file = argv[2] 124 125 glob_dict = LoadGlobalDict(glob_dict_file) 126 image_filename = os.path.basename(out_file) 127 mount_point = "" 128 if image_filename == "system.img": 129 mount_point = "system" 130 elif image_filename == "userdata.img": 131 mount_point = "data" 132 elif image_filename == "cache.img": 133 mount_point = "cache" 134 else: 135 print >> sys.stderr, "error: unknown image file name ", image_filename 136 exit(1) 137 138 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) 139 if not BuildImage(in_dir, image_properties, out_file): 140 print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir) 141 exit(1) 142 143 144if __name__ == '__main__': 145 main(sys.argv[1:]) 146