场景:在写接口时,通常先在pch定义一个宏或者static常量,然后在调用些接口,那么问题来了,一个接口会被反复调用,反复重写,反复抄代码。为了解决这种维护成功,应该放一处,调用,传入不同的参数,统一处理一样的事件,再block出去。
所以我们要统计一下,每个宏的引用次数,然后看看,比较多的宏,进行处理。
代码写得太新手了,纯解决问题,满足日常小需求。
# coding=utf-8
import os
import sys
import re
import linecache
import json
from operator import itemgetter
Macros = []
MacroWithCountDicts = []
sourcePath = '源码文件夹路径'
def getiOSURLs(MacroWithCountDicts):
iOSUrlFilePath = 'pch路径'
iOSUrlFile_object = open(iOSUrlFilePath, 'rU')
try:
for lineIdx,line in enumerate(iOSUrlFile_object):
isNotBaseURLStringLine = \
line.count('requestIP') > 0 or \
line.count('H5BaseIP') > 0 or \
line.count('swiftidIP') > 0 or \
line.count('URL_TRACE_MONITOR') > 0 or \
line.count('codeNum') > 0 or \
line.count('WeexBase') > 0 or \
line.count('dynamicMaint') > 0 or \
line.count('gatewayRequestUrl') > 0 or \
line.count('dcRequestUrl') > 0 or \
line.count('baseMessage_MONITOR')>0 or\
line.count('CFBundleDisplayName') > 0 or\
line.count('CFBundleShortVersionString') > 0 or\
line.count('CFBundleVersion') > 0 or\
line.count('CFBundleDisplayName') > 0 or\
line.count('WJExceptionHandler_crash') > 0 or\
line.count('__FUNCTION__') > 0 or\
line.count('__FILE__') > 0 or \
line.count('#pragma mark') > 0 or \
line.count('/') <= 0
#判断是否是空行或注释行
if not len(line) or line.startswith('/*') or line.startswith(' *') or line.startswith('>>>'):
continue
elif (line.startswith('#define ') or line.startswith('static')) and not isNotBaseURLStringLine :
# 去除/t空格之类的
rightLine = "".join(line.split())
lineStrings = rightLine.split('@');
if len(lineStrings) > 1:
sub1Str = lineStrings[0]
sub2Str = sub1Str.replace("\"", "")
sub3Str = sub2Str.replace('\";', "")
sub4Str = sub3Str.replace('\'', "")
sub5Str = sub4Str.replace(';', "")
urlString = sub5Str.replace('\';', "")
findStr2 = urlString
if findStr2.strip()=="" or findStr2.count('#define') >0:
firstStr = rightLine.split('@')[0];
findStr2 = firstStr.replace('#define', '')
# findStr2 = rightLine.split(' ')[1];
urlKeyString = "".join(findStr2.split())
urlKeyString2 = urlKeyString.replace('/**','')
urlKeyString21 = urlKeyString2.replace('/*','')
urlKeyString212 = urlKeyString21.replace('-','')
urlKeyString3 = urlKeyString212.replace('*/','')
Macro = urlKeyString3
if Macros.count(Macro)>0:
pass
else:
Macros.append(Macro)
MacroWithCountDicts.append({Macro : 0})
# print json.dumps(MacroWithCountDicts, ensure_ascii=False, encoding='UTF-8')
print '抓取完毕后,总宏个数:'+str(len(MacroWithCountDicts))
finally:
iOSUrlFile_object.close()
getiOSURLs(MacroWithCountDicts)
path = sourcePath+''
for dirpath, dirname, filenames in os.walk(path):
for filename in filenames:
if filename.count('.m') > 0:
filePath = os.path.join(dirpath, filename)
file_object = open(filePath, 'rU')
try:
for lineIdx,line in enumerate(file_object):
if line.startswith('#import') or line.startswith('@interface') or line.startswith('@protocol') or \
line.startswith('@required') or line.startswith('@end') or line.startswith('@protocol') or \
line.startswith('/*') or line.startswith('//') or \
line.count('@param')>0 or line.count('@return')>0:
continue
elif not line.startswith('//'):
for dict in MacroWithCountDicts:
key = next(iter(dict))
if line.count(key) > 0:
dict[key] = dict[key] + 1;
finally:
file_object.close()
print json.dumps(MacroWithCountDicts, ensure_ascii=False, encoding='UTF-8')
然后解析下
http://tool.oschina.net/codeformat/json