build_csv_file.py
#coding=utf-8
import csv
def build_csv_file(fileName,dataList):
with open(fileName,'a',newline='',encoding='utf_8_sig') as csvfile:
ww=csv.writer(csvfile, dialect='excel')
ww.writerow(dataList)
getPermission.py
#coding=utf-8
import build_csv_file
import collections
import xml.dom.minidom
from xml.dom.minidom import Document
class Excel_to_android_stringxml_obj:
def __init__(self):
self.xmlStringdict=collections.OrderedDict()
def getXmlNodeValue(self, childNodes):
value = ''
for childNode in childNodes:
if childNode.nodeName == '#text':
value = value + childNode.wholeText
# return childNode.wholeText
value_key = ''
if childNode.nodeName != '#text' and childNode.nodeName != '#comment':
attributeDict = childNode._attrs
if attributeDict != None:
for k, v in attributeDict.items():
value_key = value_key + ' %s="%s" ' % (k, childNode.getAttribute(k))
text = self.getXmlNodeValue(childNode.childNodes)
# print (text)
value = value + "<%s%s>%s%s>" % (childNode.nodeName, value_key, text, childNode.nodeName)
# print(value)
return value
def getXmlNodeValue(self, childNodes):
value = ''
for childNode in childNodes:
if childNode.nodeName == '#text':
value = value + childNode.wholeText
# return childNode.wholeText
value_key = ''
if childNode.nodeName != '#text' and childNode.nodeName != '#comment':
attributeDict = childNode._attrs
if attributeDict != None:
for k, v in attributeDict.items():
value_key = value_key + ' %s="%s" ' % (k, childNode.getAttribute(k))
text = self.getXmlNodeValue(childNode.childNodes)
# print (text)
value = value + "<%s%s>%s%s>" % (childNode.nodeName, value_key, text, childNode.nodeName)
# print(value)
return value
# 解析xml
def parse_strings_xml_file(self, xml_fname):
try:
DOMTree = xml.dom.minidom.parse(xml_fname)
collection = DOMTree.documentElement
stringList = collection.getElementsByTagName("string")
for string in stringList:
if string.hasAttribute("name"):
key = string.getAttribute("name")
# print("name: %s" % key)
value = self.getXmlNodeValue(string.childNodes)
# print("value: %s" % value)
self.xmlStringdict[key] = value
return True
except Exception as e:
print(e)
return False
# 解析xml
def parse_AndroidManifest_xml_file(self, xml_fname):
try:
DOMTree = xml.dom.minidom.parse(xml_fname)
collection = DOMTree.documentElement
stringList = collection.getElementsByTagName("permission")
for string in stringList:
if string.hasAttribute("android:name"):
android_name= string.getAttribute("android:name")
print("android_name: %s" % android_name)
else:
android_name=''
if string.hasAttribute("android:permissionGroup"):
android_permissionGroup= string.getAttribute("android:permissionGroup")
print("android_permissionGroup: %s" % android_permissionGroup)
else:
android_permissionGroup=''
if string.hasAttribute("android:protectionLevel"):
android_protectionLevel= string.getAttribute("android:protectionLevel")
print("android_protectionLevel: %s" % android_protectionLevel)
else:
android_protectionLevel=''
if string.hasAttribute("android:label"):
android_label = string.getAttribute("android:label")[8:]
android_label_str=self.xmlStringdict[android_label]
print("android_label: %s" % android_label_str)
else:
android_label_str = ''
if string.hasAttribute("android:description"):
android_description = string.getAttribute("android:description")[8:]
android_description_str = self.xmlStringdict[android_description]
print("android_description_str: %s" % android_description_str)
else:
android_description_str = ''
dataList=[android_name,android_permissionGroup,android_protectionLevel,android_label_str,android_description_str]
build_csv_file.build_csv_file("3.csv", dataList)
return True
except Exception as e:
print(e)
return False
obj = Excel_to_android_stringxml_obj()
build_csv_file.build_csv_file("3.csv", ['权限','权限组',' 权限级别','标签','描述'])
obj.parse_strings_xml_file(r'frameworks\base\core\res\res\values-zh-rCN\strings.xml')
obj.parse_AndroidManifest_xml_file(r"frameworks\base\core\res\AndroidManifest.xml")