按照依赖关系排序
import copy
import utils.functool
@utils.functool.settimeout(timeout=15)
def dependsOnSort(testFunNames, dependRelation):
"""
有个问题就是如果出现A依赖B,⽽B⼜依赖A就会出现问题
:param testFunNames: ⼀个列表,被排序的列表[a,b,c,d,...]
:param dependRelaton: 依赖关系[(a,b),(c,b),...],其中元祖中的第⼀个值代表被依赖项,第⼆个值代表依赖项    :return: 返回按照依赖关系排序后的数组
"""
# 排序后的执⾏函数顺序
target = []
tmp = copy.deepcopy(testFunNames)
while True:
# ⽤来记录依赖项,即低优先级执⾏,可能有重复需要set去重
dependings = []
# ⽤来记录删除的依赖关系中的依赖项,也是下次再次⽤来去除依赖项的数据源
# depended = []
if False not in map(lambda x: x is None, dependRelaton):
break
# 第⼀步,从RL中提取出依赖项
for _ in dependRelaton:
if _ is None:
continue
depend, depending = _
dependings.append(depending)
# 移除依赖的函数名,剩下的是需要被优先执⾏的
for _ in set(dependings):
# 第⼆步,从函数依赖关系中,删除被优先执⾏的关系项
for _ in dependRelation:
if _ is None:
continue
depend, depending = _
if depend in tmp:
index = dependRelation.index(_)
dependRelation.pop(index)
dependRelation.insert(index, None)
# 第三步, 将本次计算的可以先执⾏的函数,添加到上⼀次的计算出来的后⾯
target += tmp
# 补集,下⼀次需要排序函数名
tmp = list(set(testFunNames) - set(target))
# +未被依赖的函数名,直接放在队尾
return target + (list(set(testFunNames) - set(target)))
if __name__ == "__main__":
testFunNames = ["test_01", "test_02", "test_03", "test_04", "test_05", "test_06"]
dependRelaton = [("test_01", "test_02"),
("test_01", "test_04"),
("test_02", "test_03"),
("test_04", "test_03"),
("test_05", "test_06")
]
print(dependsOnSort(testFunNames, dependRelaton))
pass
>sort命令排序