Skip to content

Instantly share code, notes, and snippets.

@sosoyososo
Created September 18, 2013 15:21
Show Gist options
  • Save sosoyososo/6610758 to your computer and use it in GitHub Desktop.
Save sosoyososo/6610758 to your computer and use it in GitHub Desktop.
python : 遍历一个目录下所有.h/.m/.mm 文件,并整理出来类的继承关系
#1.遍历某个路径的子目录
#2.按行读取头文件和源文件
#3.找到对应关系行
#4.判断是否在注释范围内
#5.分析并存储关系
import os, os.path, re, sys
_g_relations = {}
_g_graphRelations = {}
def skipNote(strContent, isInNote):
if isInNote :
end = strContent.find("*/")
strContent = strContent[end+2:]
while strContent.find("/*")>0:
start = strContent.find("/*")
strContent = strContent[start:]
end = strContent.find("*/")
if end >= 0:
strContent = strContent[end+2:]
else :
return True, strContent
start = strContent.find("//")
if start >= 0 :
strContent = strContent[:start]
return False, strContent
def handleRelations(relations):
str = relations[10:]
mid = str.find(":")
child = str[:mid].strip()
parent = str[mid+1:].strip()
if parent in _g_relations:
childs = _g_relations[parent]
childs[0:0] = [child]
else:
childs = [child]
_g_relations[parent] = childs
def findClassRelation(strContent):
patten = re.compile("@interface(\s)+[A-Za-z0-9$_]+(\s)*:(\s)*[A-Za-z0-9$_]+")
match = patten.match(strContent)
if match :
return (match.group())
# findClassRelation("@interface GPUImageRawDataOutput : NSObject <GPUImageInput> { @interface GPUImageRawDataOutput : NSObject <GPUImageInput>")
def handleFile(fileName) :
fh = open(fileName)
isInNote = False
for line in fh.readlines():
isInNote, str = skipNote(line, isInNote)
if len(str) > 0:
relations = findClassRelation(str)
if relations:
handleRelations(relations)
# handleFile("/Users/Karsa/Desktop/code/python/test.h")
def checkAndHandleFile(fileName):
extension = os.path.splitext(fileName)[1]
# print (extension)
if (extension == ".mm" or extension == ".m" or extension == ".h"):
handleFile(fileName)
def walk_dir(dir,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
checkAndHandleFile(root + "/" + name)
def printRelations() :
if _g_relations :
for key in _g_relations.keys() :
print (key + " has children : " + ' '.join(_g_relations[key]))
class ClassRelation:
name = None
parent = None
children = None
def __init__(self,name):
self.name = name
def getName(self):
return self.name
def setParent(self,parent):
self.parent = parent
return
def getParent(self) :
return self.parent
def setChildren(self,childRen):
self.children = childRen
return
def getChildren(self):
return self.children
def makeRelationGraph():
if _g_relations:
for key in _g_relations.keys() :
parent = key
children = _g_relations[parent]
p = None
if parent in _g_graphRelations:
p = _g_graphRelations[parent]
else :
p = ClassRelation(parent)
_g_graphRelations[parent] = p
childs = []
for child in children :
c = None
if child in _g_graphRelations:
c = _g_graphRelations[child]
if not c :
c = ClassRelation(child)
c.setParent(p)
childs[0:0] = [c]
# print (c.getName())
_g_graphRelations[child] = c
p.setChildren(childs)
def getParentsForClass(className) :
parents = []
if className in _g_graphRelations:
c = _g_graphRelations[className]
p = c.getParent()
while p:
parents[0:0] = [p]
p = p.getParent()
return parents
length = len(sys.argv)
if length >= 2:
path = sys.argv[1]
walk_dir(path)
makeRelationGraph()
# l = getParentsForClass("GPUImageHardLightBlendFilter")
# for node in l:
# print (node.getName())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment