build_image.py revision bd93d425aedef907d8fef643b748897e9c7dceeb
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 87 return d 88 89 90def LoadGlobalDict(filename): 91 """Load "name=value" pairs from filename""" 92 d = {} 93 f = open(filename) 94 for line in f: 95 line = line.strip() 96 if not line or line.startswith("#"): 97 continue 98 k, v = line.split("=", 1) 99 d[k] = v 100 f.close() 101 return d 102 103 104def main(argv): 105 if len(argv) != 3: 106 print __doc__ 107 sys.exit(1) 108 109 in_dir = argv[0] 110 glob_dict_file = argv[1] 111 out_file = argv[2] 112 113 glob_dict = LoadGlobalDict(glob_dict_file) 114 image_filename = os.path.basename(out_file) 115 mount_point = "" 116 if image_filename == "system.img": 117 mount_point = "system" 118 elif image_filename == "userdata.img": 119 mount_point = "data" 120 121 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) 122 if not BuildImage(in_dir, image_properties, out_file): 123 print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir) 124 exit(1) 125 126 127if __name__ == '__main__': 128 main(sys.argv[1:]) 129