Created
May 10, 2015 05:25
-
-
Save scue/0ad950803038d9eab634 to your computer and use it in GitHub Desktop.
Check android app project string values miss or not, usefully under android source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import os, sys, getopt | |
import xml.dom.minidom | |
import subprocess | |
from xml.dom.minidom import Node | |
# 判断是否是App项目依据 | |
Axml='AndroidManifest.xml' | |
res_en_string="res/values/strings.xml" | |
res_cn_string="res/values-zh-rCN/strings.xml" | |
# 检查资源文件列表 | |
res_string_files=[res_en_string, res_cn_string] | |
# Java调用字符串资源列表 | |
find_string_called_by_java='''find . -name .repo -prune -o -name .git -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n -o 'R.string[0-9A-Za-z_.-]\+'|awk -F':' '{print $3}'|sort|uniq|xargs echo''' | |
def _check_string_res(path): | |
"""检查字符串资源调用情况 | |
:path: TODO | |
:returns: TODO | |
""" | |
os.chdir(path) | |
if not os.path.exists(Axml): | |
return | |
# 输出提示 | |
print "\n### Processing Project: %s ..\n" % path | |
# 获得字符串资源调用情况 | |
find_string_called_by_java_array = subprocess.Popen(find_string_called_by_java, shell=True, stdout=subprocess.PIPE).stdout.read().split(' ') | |
# 逐个检查资源文件(目前检查中文、英文) | |
for res_string_file in res_string_files: | |
print ">>> Checking %s file .." % res_string_file | |
# 解析xml文件,并保存已有资源到 names_had | |
doc = xml.dom.minidom.parse(res_string_file) | |
strings = doc.getElementsByTagName('string') | |
names_had = [] | |
for string in strings: | |
name = string.getAttribute('name') | |
names_had.append(name) | |
# 逐个检查被调用的字符串资源,不存在此资源时报Warning | |
for check in find_string_called_by_java_array: | |
c=check[9:].strip() | |
if c not in names_had: | |
print " - Warning: string name '%s' not found!!!" % c | |
def usage(exitval=0): | |
print "\nUsage: %s project_dir1 project_dir2 ..\n" % sys.argv[0] | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
if os.path.isfile(Axml): | |
_check_string_res(os.path.abspath('.')) | |
else: | |
usage() | |
elif len(sys.argv) > 1: | |
for path in sys.argv[1:]: | |
if os.path.isdir(path): | |
_check_string_res(os.path.abspath(path)) | |
else: | |
print "### %s Not a directory, ignored." % path | |
else: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment