build_image.py revision 9f8e8db188371cb3787a91a03d193f87ad244ea3
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 71 def copy_prop(src_p, dest_p): 72 if src_p in glob_dict: 73 d[dest_p] = str(glob_dict[src_p]) 74 75 common_props = ( 76 "extfs_sparse_flag", 77 "mkyaffs2_extra_flags", 78 ) 79 for p in common_props: 80 copy_prop(p, p) 81 82 d["mount_point"] = mount_point 83 if mount_point == "system": 84 copy_prop("fs_type", "fs_type") 85 copy_prop("system_size", "partition_size") 86 elif mount_point == "data": 87 copy_prop("fs_type", "fs_type") 88 copy_prop("userdata_size", "partition_size") 89 elif mount_point == "cache": 90 copy_prop("cache_fs_type", "fs_type") 91 copy_prop("cache_size", "partition_size") 92 93 return d 94 95 96def LoadGlobalDict(filename): 97 """Load "name=value" pairs from filename""" 98 d = {} 99 f = open(filename) 100 for line in f: 101 line = line.strip() 102 if not line or line.startswith("#"): 103 continue 104 k, v = line.split("=", 1) 105 d[k] = v 106 f.close() 107 return d 108 109 110def main(argv): 111 if len(argv) != 3: 112 print __doc__ 113 sys.exit(1) 114 115 in_dir = argv[0] 116 glob_dict_file = argv[1] 117 out_file = argv[2] 118 119 glob_dict = LoadGlobalDict(glob_dict_file) 120 image_filename = os.path.basename(out_file) 121 mount_point = "" 122 if image_filename == "system.img": 123 mount_point = "system" 124 elif image_filename == "userdata.img": 125 mount_point = "data" 126 elif image_filename == "cache.img": 127 mount_point = "cache" 128 else: 129 print >> sys.stderr, "error: unknown image file name ", image_filename 130 exit(1) 131 132 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) 133 if not BuildImage(in_dir, image_properties, out_file): 134 print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir) 135 exit(1) 136 137 138if __name__ == '__main__': 139 main(sys.argv[1:]) 140