python—android编译中build_image.py分析
最近在学习python,基础知识学习了一遍。我的学习习惯是:了解了基础语法后,去阅读开源代码(掌握优秀程序员的一些写代码习惯和手法),最后结合项目实践,实践的过程对某些细节进行深化。
想起android编译系统中有一些python脚本,下面对build_image.py进行简单分析。
生成系统镜像的target在build\core\Makefile中,
#----------build\core\Makefile-------------# $(1): output filedefine build-systemimage-target @echo "Target system fs image: $(1)" @mkdir -p $(dir $(1)) $(systemimage_intermediates) && rm -rf $(systemimage_intermediates)/system_image_info.txt $(call generate-userimage-prop-dictionary, $(systemimage_intermediates)/system_image_info.txt, skip_fsck=true) $(hide) PATH=$(foreach p,$(INTERNAL_USERIMAGES_BINARY_PATHS),$(p):)$$PATH \ ./build/tools/releasetools/build_image.py \ $(TARGET_OUT) $(systemimage_intermediates)/system_image_info.txt $(1) endef
其中,可以看出来主要是借助了build_image.py去生成镜像,/build/tools/releasetools/build_image.py $(TARGET_OUT) $(systemimage_intermediates)/system_image_info.txt $(1) , 有三个参数,一个是作为输入的文件目录,system_image_info.txt,和最终生成的镜像名,例如system.img。
build_image.py分析由于上面makefile中是在shell中直接调用build_image.py(不是import 模块),所以__name__为__main__,会直接调用main函数,
#参数格式必须为3个# $(TARGET_OUT) system_image_info.txt output_filedef main(argv): if len(argv) != 3: print __doc__ sys.exit(1) in_dir = argv[0] glob_dict_file = argv[1] out_file = argv[2] #将system_image_info.txt中的内容保存到字典中,例如{'fs_type':'ext4',.....} glob_dict = LoadGlobalDict(glob_dict_file) #basename获取文件名(不包括路径),这里为system.img image_filename = os.path.basename(out_file) mount_point = "" if image_filename == "system.img": mount_point = "system" elif image_filename == "userdata.img": mount_point = "data" elif image_filename == "cache.img": mount_point = "cache" elif image_filename == "vendor.img": mount_point = "vendor" else: print >> sys.stderr, "error: unknown image file name ", image_filename exit(1) #print >> #the first expression after the >> must evaluate to a “file-like” object, #输入为system_image_info.txt内容字典和system image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) if not BuildImage(in_dir, image_properties, out_file): print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir) exit(1)#当被直接python build_image.py执行时 __name__为__main__#当被其他import时,__name__是模块的名字,build_image#argv[0]是脚本名if __name__ == '__main__': main(sys.argv[1:])
其中,system_image_info.txt一个例子如下所示,一些参数及其值,
fs_type=ext4system_size=1288491008userdata_size=5835325440cache_fs_type=ext4cache_size=268435456extfs_sparse_flag=-sselinux_fc=out/target/product/msm8916_64/root/file_contextsverity=trueverity_key=build/target/product/security/verityverity_signer_cmd=out/host/linux-x86/bin/verity_signersystem_verity_block_device=/dev/block/bootdevice/by-name/systemskip_fsck=true
LoadGlobalDict函数如下,将system_image_info.txt中的内容保存到字典中,例如{‘fs_type’:’ext4’,…..}
def LoadGlobalDict(filename): """Load "name=value" pairs from filename""" d = {} f = open(filename) #对文件对象的迭代 #文件中#不处理,是注释 #strip去除字符串两头,不包括内部的空格 #split将字符串分割成序列 str.split(sep=None, maxsplit=-1) #If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). #>>>'1,2,3'.split(',', maxsplit=1) #['1', '2,3'] #所以用=分割,只有=前面和后面2个元素 for line in f: line = line.strip() if not line or line.startswith("#"): continue k, v = line.split("=", 1) d[k] = v f.close() return d
ImagePropFromGlobalDict函数,从全局字典(system_image_info.txt中有些东西不是都有用)取出需要的东西,返回一个新字典。
def ImagePropFromGlobalDict(glob_dict, mount_point): """Build an image property dictionary from the global dictionary. Args: glob_dict: the global dictionary from the build system. mount_point: such as "system", "data" etc. """ d = {} def copy_prop(src_p, dest_p): if src_p in glob_dict: d[dest_p] = str(glob_dict[src_p]) #extfs_sparse_flag=-s #skip_fsck=true #selinux_fc=out/target/product/msm8916_64/root/file_contexts #将common_props属性中的值保存到新建的字典d中, common_props = ( "extfs_sparse_flag", "mkyaffs2_extra_flags", "selinux_fc", "skip_fsck", ) for p in common_props: copy_prop(p, p) #添加'mount_point':'system' d["mount_point"] = mount_point if mount_point == "system": #'fs_type':'ext4' #'partition_size':'1288491008' copy_prop("fs_type", "fs_type") copy_prop("system_size", "partition_size") elif mount_point == "data": copy_prop("fs_type", "fs_type") copy_prop("userdata_size", "partition_size") elif mount_point == "cache": copy_prop("cache_fs_type", "fs_type") copy_prop("cache_size", "partition_size") elif mount_point == "vendor": copy_prop("vendor_fs_type", "fs_type") copy_prop("vendor_size", "partition_size") return d
最后调用BuildImage函数,其实就是组了一个命令,然后执行该命令(mkuserimg.sh -s in_dir out_file ext4 system 1288491008 out/target/product/msm8916_64/root/file_contexts)。
def BuildImage(in_dir, prop_dict, out_file): """Build an image to out_file from in_dir with property prop_dict. Args: in_dir: path of input directory. prop_dict: property dictionary. out_file: path of the output image file. Returns: True iff the image is built successfully. """ build_command = [] #字典的get方法,如果键不存在,没有任何异常,而得到None fs_type = prop_dict.get("fs_type", "") run_fsck = False #文件系统以ext开头 #这里是ext4,走这里,其实就是组一个要执行的命令 mkuserimg.sh -s in_dir out_file ext4 system 1288491008 out/target/product/msm8916_64/root/file_contexts if fs_type.startswith("ext"): build_command = ["mkuserimg.sh"] if "extfs_sparse_flag" in prop_dict: build_command.append(prop_dict["extfs_sparse_flag"]) run_fsck = True build_command.extend([in_dir, out_file, fs_type, prop_dict["mount_point"]]) if "partition_size" in prop_dict: build_command.append(prop_dict["partition_size"]) if "selinux_fc" in prop_dict: build_command.append(prop_dict["selinux_fc"]) else: build_command = ["mkyaffs2image", "-f"] #split用于将字符串分割成list,没有参数表示用空格分割 if prop_dict.get("mkyaffs2_extra_flags", None): build_command.extend(prop_dict["mkyaffs2_extra_flags"].split()) build_command.append(in_dir) build_command.append(out_file) if "selinux_fc" in prop_dict: build_command.append(prop_dict["selinux_fc"]) build_command.append(prop_dict["mount_point"]) #执行该命令mkuserimg.sh -s in_dir out_file ext4 system exit_code = RunCommand(build_command) if exit_code != 0: return False# >>> os.path.dirname('c:\\Python\\a.txt') 返回目录# 'c:\\Python'# >>> os.path.basename('c:\\Python\\a.txt') 返回文件名# 'a.txt'# os.path.join连接目录与文件名或目录 #skip_fsck=true,不走这里 if run_fsck and prop_dict.get("skip_fsck") != "true": # Inflate the sparse image unsparse_image = os.path.join( os.path.dirname(out_file), "unsparse_" + os.path.basename(out_file)) inflate_command = ["simg2img", out_file, unsparse_image] exit_code = RunCommand(inflate_command) #This function is semantically identical to unlink(). #remove和unlink一样 if exit_code != 0: os.remove(unsparse_image) return False # Run e2fsck on the inflated image file e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] exit_code = RunCommand(e2fsck_command) os.remove(unsparse_image) return exit_code == 0
其中,RunCommand其实就类似于c语言中的system(),执行一段命令,获取返回值,用popen实现的。
def RunCommand(cmd): """ Echo and run the given command Args: cmd: the command represented as a list of strings. Returns: The exit code. """ print "Running: ", " ".join(cmd) #创建子进程 p = subprocess.Popen(cmd) #等待子进程执行完 p.communicate() return p.returncode
最终其实是调用了mkuserimg.sh脚本,脚本核心是调用make_ext4fs 应用程序
#system\extras\ext4_utils\mkuserimg.shfunction usage() { cat<<EOT Usage: mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE [FILE_CONTEXTS] EOT }echo "in mkuserimg.sh PATH=$PATH"ENABLE_SPARSE_IMAGE=if [ "$1" = "-s" ]; then ENABLE_SPARSE_IMAGE="-s" shiftfiif [ $# -ne 5 -a $# -ne 6 ]; then usage exit 1fiSRC_DIR=$1if [ ! -d $SRC_DIR ]; then echo "Can not find directory $SRC_DIR!" exit 2fiOUTPUT_FILE=$2EXT_VARIANT=$3MOUNT_POINT=$4SIZE=$5FC=$6case $EXT_VARIANT in ext4) ;; *) echo "Only ext4 is supported!"; exit 3 ;;esacif [ -z $MOUNT_POINT ]; then echo "Mount point is required" exit 2fiif [ -z $SIZE ]; then echo "Need size of filesystem" exit 2fiif [ -n "$FC" ]; then FCOPT="-S $FC"fi#核心是make_ext4fs 应用程序MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE $FCOPT -l $SIZE -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR"echo $MAKE_EXT4FS_CMD$MAKE_EXT4FS_CMDif [ $? -ne 0 ]; then exit 4fi
而make_ext4fs 应用程序具体在system\extras\ext4_utils中生成,makefile如下,其中makefile中有两个make_ext4fs的module,一个是host(BUILD_HOST_EXECUTABLE),一个是target的(BUILD_EXECUTABLE),这个命令是在编译机调用,也就是host上而不是target上。
include $(CLEAR_VARS)LOCAL_SRC_FILES := make_ext4fs_main.cLOCAL_MODULE := make_ext4fsLOCAL_STATIC_LIBRARIES += \ libext4_utils_host \ libsparse_host \ libz ifeq ($(HOST_OS),windows) LOCAL_LDLIBS += -lws2_32else LOCAL_STATIC_LIBRARIES += libselinux LOCAL_CFLAGS := -DHOSTendifinclude $(BUILD_HOST_EXECUTABLE)
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
为什么我建议你学《Python数据分析师》?
大数据火了好多年至今热度不减,也衍生了数据分析师这个热门高薪职业,这让不少IT人好奇、向往。奇猫也常常被问到:数据分析师薪资究竟如何?好找工作吗?要会哪些技能?未来发展怎么样? 于是,奇猫抓取了一份数据分析师的岗位信息来进行解答。 数据分析师行业薪资如何? 首先从招聘网站上,可以看到数据分析的薪资: 统计思维敏感的你,可能会质疑了:截图中的只是部分岗位,无法说明整体行业情况。 那么整体行业如何呢?做数据分析奇猫是专业的!下面就跟我来分析一下招聘网站上整体的行业薪资数据,奇猫抓取了北京,上海,深圳,南京,武汉,广州,南京,杭州,成都,重庆10个核心城市的的数据分析师职位数据(爬取过程略)。 对职位薪资进行基本分析:平均值为10000+,中位数为11000左右,不同薪资范围对应的职位数量如下图: 从图中可以看到:数据分析相关职位薪资主要分布在10~30K之间;总体看,数据分析起步薪资还是比较高的,有较好的发展空间。 ▼▼▼ 随着从事数据分析的人越来越多,对于初学者,你可能有更多的疑问: 1:数据分析具体做什么?2:什么是指标体系与指标?3:数据分析岗位与薪资的地域分布如何?4:数据分析师具...
- 下一篇
天罗地网——Python爬虫初初初探
环境准备 Python 我们使用Python2.7进行开发,注意配置好环境变量。 IDE 我们使用Pycharm进行开发,它和大名鼎鼎的Android Studio、IDEA同出一门——Jet Brains。 关于破解,很无耻的贴两个: 用户名:yueting3527 注册码:=====LICENSEBEGIN=====93347-12042010 00001FMHemWIs"6wozMZnat3IgXKXJ 2!nV2I6kSO48hgGLa9JNgjQ5oKz1Us FFR8k"nGzJHzjQT6IBG!1fbQZn9!Vi=====LICENSEEND===== 用户名:yueting3527 注册码:=====LICENSEBEGIN=====93347-12042010 00001FMHemWIs"6wozMZnat3IgXKXJ 2!nV2I6kSO48hgGLa9JNgjQ5oKz1Us FFR8k"nGzJHzjQT6IBG!1fbQZn9!Vi=====LICENSEEND===== Requests模块 Requests模块是一个用于替代Python URLLib...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6