Android的build文件findleaves.py分析

来源:互联网 发布:淘宝上的图片怎么保存 编辑:程序博客网 时间:2024/06/07 02:30
subdir_makefiles := \

        $(shell build/tools/findleaves.py --prune=$(OUT_DIR) --prune=.repo --prune=.git $(subdirs) Android.mk)

可以看出传输的参数是--prune=$(OUT_DIR)   

--prune=.repo

--prune=.git $(subdirs) Android.mk)

通过findleaves.py可以知道“--”是分隔符

第一次分析python:一些基础知识写先


一:#!/usr/bin/env python  

解释:

脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单

#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器;
#!/usr/bin/env python这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
#!/usr/bin/python相当于写死了python路径;
#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法

二:包含的类

系统相关的信息模块 import sys 
    sys.argv是一个list,包含所有的命令行参数. 
    sys.stdout sys.stdin sys.stderr 分别表示标准输入输出,错误输出的文件对象. 
    sys.stdin.readline() 从标准输入读一行 sys.stdout.write("a") 屏幕输出a 
    sys.exit(exit_code) 退出程序 
    sys.modules 是一个dictionary,表示系统中所有可用的module 
    sys.platform 得到运行的操作系统环境 
    sys.path 是一个list,指明所有查找module,package的路径. 
    
  操作系统相关的调用和操作 import os 
    os.environ 一个dictionary 包含环境变量的映射关系 os.environ["HOME"] 可以得到环境变量HOME的值 
    os.chdir(dir) 改变当前目录 os.chdir('d:\\outlook') 注意windows下用到转义 
    os.getcwd() 得到当前目录 
    os.getegid() 得到有效组id  os.getgid() 得到组id 
    os.getuid() 得到用户id  os.geteuid() 得到有效用户id 
    os.setegid os.setegid() os.seteuid() os.setuid() 
    os.getgruops() 得到用户组名称列表 
    os.getlogin() 得到用户登录名称 
    os.getenv 得到环境变量 
    os.putenv 设置环境变量 
    os.umask 设置umask 
    os.system(cmd) 利用系统调用,运行cmd命令 


我们现在main函数看起:


#!/usr/bin/env python  

#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


#
# Finds files with the specified name under a particular directory, stopping
# the search in a given subdirectory when the file is found.
#


import os  
import sys


def perform_find(mindepth, prune, dirlist, filename):
  result = []
  pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))   lambda:点击打开链接   map:点击打开链接
  for rootdir in dirlist:
    rootdepth = rootdir.count("/")
    for root, dirs, files in os.walk(rootdir, followlinks=True):
      # prune
      check_prune = False
      for d in dirs:
        if d in pruneleaves:
          check_prune = True
          break
      if check_prune:
        i = 0
        while i < len(dirs):
          if dirs[i] in prune:
            del dirs[i]
          else:
            i += 1
      # mindepth
      if mindepth > 0:
        depth = 1 + root.count("/") - rootdepth
        if depth < mindepth:
          continue
      # match
      if filename in files:
        result.append(os.path.join(root, filename))
        del dirs[:]
  return result


def usage():
  sys.stderr.write("""Usage: %(progName)s [<options>] <dirlist> <filename>
Options:
   --mindepth=<mindepth>
       Both behave in the same way as their find(1) equivalents.
   --prune=<dirname>
       Avoids returning results from inside any directory called <dirname>
       (e.g., "*/out/*"). May be used multiple times.
""" % {
      "progName": os.path.split(sys.argv[0])[1],
    })
  sys.exit(1)


def main(argv):
  mindepth = -1
  prune = []   //定义一个list   列表[]  :有序 可重复 list的操作看链接文档:点击打开链接
  i=1

 //取出第一个给mindepth,之后的加入列表prune
  while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":   //当参数个数大于1&参数长度大于2  argv[i][0:2]用了切片  限定取值符从0开始取2个
    arg = argv[i]
    if arg.startswith("--mindepth="):   //startswith是sys类的一个方法,查找是否包含()参数的内容
      try:
        mindepth = int(arg[len("--mindepth="):])  //如果包含了mindepth取arg的“--mindepth=”之后的内容,赋值给mindepth
      except ValueError:
        usage()
    elif arg.startswith("--prune=")://否则之后的参数,是否“--prune=”
      p = arg[len("--prune="):]     //p的内容是取arg的“--prune=”之后的内容
      if len(p) == 0:  如果p的长度为0  
        usage() //打出错误
      prune.append(p)  //加入列表
    else:
      usage()
    i += 1
  if len(argv)-i < 2: # need both <dirlist> and <filename>
    usage()
  dirlist = argv[i:-1]   #argv[2] --》dirlist
  filename = argv[-1]  #最后一个给filename
  results = list(set(perform_find(mindepth, prune, dirlist, filename)))  #调用perform_find方法遍历所有.mk文件   set操作:点击打开链接
  results.sort()  #排序
  for r in results:
    print r


if __name__ == "__main__":            //类的主方法接口  标准写法
  main(sys.argv)                                   //调用main,传入写入参数

原创粉丝点击