蔗糖中果糖的含量:在Eclipse中加入Android源码

来源:百度文库 编辑:九乡新闻网 时间:2024/07/07 09:45:16

在我们开发android程序过程中,很多时候 需要查看android的源码是如何实现的。这个时候就需要把android的源码加入到eclipse中,那么在我们通过Git和repo获取到android源码之后,就需要把java文件提取出来,并放到androidSDK子目录source下。如果手工来提取这些java文件是很耗费时间的,所以我们可以写个python脚本来自动提取android源码中的java文件,如下:

from __future__ import with_statement  # for Python < 2.6

import os
import re
import zipfile

# open a zip file
DST_FILE = 'sources.zip'
if os.path.exists(DST_FILE):
  print DST_FILE, "already exists"
  exit(1)
zip = zipfile.ZipFile(DST_FILE, 'w', zipfile.ZIP_DEFLATED)

# some files are duplicated, copy them only once
written = {}

# iterate over all Java files
for dir, subdirs, files in os.walk('.'):
  for file in files:
    if file.endswith('.java'):
      # search package name
      path = os.path.join(dir, file)
      with open(path) as f:
        for line in f:
          match = re.match(r'\s*package\s+([a-zA-Z0-9\._]+);', line)
          if match:
            # copy source into the zip file using the package as path
            zippath = match.group(1).replace('.', '/') + '/' + file
            if zippath not in written:
              written[zippath] = 1
              zip.write(path, zippath)
            break;
        
zip.close()

来自:http://blog.csdn.net/dongfengsun/archive/2009/10/17/4691062.aspx