Python从js文件中找到js引用

正则匹配.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() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值