正则匹配.js并记录
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os
import importlib,sys
importlib.reload(sys)
# sys.setdefaultencoding('utf8')
print (sys.getdefaultencoding())
def listFiles(dirPath):
fileList=[]
for root,dirs,files in os.walk(dirPath):
for fileObj in files:
fileList.append(os.path.join(root,fileObj))
return fileList
def main():
fileDir = os.getcwd() + "/old_js"
fileList = listFiles(fileDir)
logData = open("./log.txt", 'w+')
open("./findPng.js", 'w+')
fData = open("findPng.js", "r+", encoding='UTF-8', errors='ignore')
fData.seek(0, 0)
fData.truncate()
for fileObj in fileList:
strCounter = 1
f = open(fileObj,'r+', encoding='UTF-8', errors='ignore')
print("\nopen file :" + os.path.basename(fileObj), file=logData)
fileName = os.path.basename(fileObj).split('.')[0]
#print fileName
all_the_lines=f.readlines()
f.close()
numStrTable = []
numStrTable.append(fileName+' = {\n')
if ("protobuf" in fileObj) or (os.path.basename(fileObj).split('.')[1] != "js" and os.path.basename(fileObj).split('.')[1] != "txt") or (fileName == "findPng") :
print("do nothing ... ... ...", file=logData)
continue
print("analysising ... ... ...", file=logData)
for line in all_the_lines:
regex1 = re.compile(u"(.*\.js[\"\'])")
regex2 = re.compile(u"HelpTool\.log")
regex3 = re.compile(u"cc\.log")
regex4 = re.compile(u"console\.log")
regex5 = re.compile("^(\s)*(\/)+")
line11 = line.encode('utf8') # temp line
line1 = line11.decode('utf8')
# ingore print and cclog
results1 = regex2.findall(line1)
length1 = len(results1)
results2 = regex3.findall(line1)
length2 = len(results2)
results3 = regex4.findall(line1)
length3 = len(results3)
results4 = regex5.findall(line1)
length4 = len(results4)
results5 = regex1.findall(line1)
length5 = len(results5)
if length1 + length2 + length3 + length4 == 0 :
# iterate over all found quotes pairs
if length5 > 0 :
counter = 0
for match in regex1.finditer(line1):
start = match.start()
end = match.end()
temp = line1[start:end]
counter += 1
numStrTable.append(' '+temp.strip()+',\n')
strCounter += 1
print('find ------>' + line1, file=logData)
numStrTable.append('},\n')
if strCounter > 1 :
# write Localization data
for lines2 in numStrTable :
fData.write(lines2)
fData.close()
if __name__=='__main__':
main()