qq-22432832

根据子父节点关系生成血缘关系-py

多个子父节点构成一条或多条关系线,n->n
输入:父节点\t子节点
输出:依赖树上的所有路径:如 A->B->C->D

FILELIST = []
LLIST = []
RLIST = []

LSET = set()
RSET = set()


def jurgeHeadFlag(headNodeLinkSet):
    for headNodeLink in headNodeLinkSet:
        nodeHead = headNodeLink.split('->')[0]
        if nodeHead in LSET:
            return True
    return False
def jurgeTailFlag(headNodeLinkSet):
    for headNodeLink in headNodeLinkSet:
        nodeHead = headNodeLink.split('->')[-1]
        if nodeHead in RSET:
            return True
    return False

# 处理head依赖的节点 输入单节点  输出所有的上游依赖关系 list
def makeHeadDep(headNode):
    headNodeLinkSet = {headNode}
    # 无上游 直接输出本节点
    if not jurgeHeadFlag(headNodeLinkSet): return [headNode]
    # set中存在上游的head
    while jurgeHeadFlag(headNodeLinkSet):
        headNodeLinkSetTemp = set()
        for headNodeLink in headNodeLinkSet:
            # print('headNodeLink==='+headNodeLink)
            nodeHead = headNodeLink.split('->')[0]
            # print('nodeHead==='+nodeHead)
            # 有head的
            if nodeHead in LSET:
                for nodes in FILELIST:
                    head_ = nodes.split('\t')[1]
                    tail_ = nodes.split('\t')[0]
                    if nodeHead == tail_ and head_ not in headNodeLink:
                        if headNodeLink in headNodeLinkSetTemp:
                            headNodeLinkSetTemp.remove(headNodeLink)
                        headNodeLinkSetTemp.add(head_ + '->' + headNodeLink)
                    else:continue
            # 没有的 直接temp输出原依赖串
            else:
                headNodeLinkSetTemp.add(headNodeLink)
        headNodeLinkSet = headNodeLinkSetTemp
    return list(headNodeLinkSet)


# 处理tail被依赖的节点
def makeTailDep(tailNodeL):
    tailNodeLinkSet = {tailNodeL}
    # 无上游 直接输出本节点
    if not jurgeTailFlag(tailNodeLinkSet): return [tailNodeL]
    # set中存在上游的tail
    while jurgeTailFlag(tailNodeLinkSet):
        tailNodeLinkSetTemp = set()
        for tailNodeLink in tailNodeLinkSet:
            nodeTail = tailNodeLink.split('->')[-1]
            # 有tail的
            if nodeTail in RSET:
                for nodes in FILELIST:
                    head_ = nodes.split('\t')[1]
                    tail_ = nodes.split('\t')[0]
                    if nodeTail == head_ and tail_ not in tailNodeLink:
                        if tailNodeLink in tailNodeLinkSetTemp:
                            tailNodeLinkSetTemp.remove(tailNodeLink)
                        tailNodeLinkSetTemp.add(tailNodeLink + '->' + tail_)
                    else:continue
            # 没有的 直接temp输出原依赖串
            else:
                tailNodeLinkSetTemp.add(tailNodeLink)
        tailNodeLinkSet = tailNodeLinkSetTemp
    return list(tailNodeLinkSet)

if __name__ == '__main__':
    dep_res = set()
    with open(r'D:\MyDesk\已迁移的文件依赖.txt', encoding='utf-8', mode='r') as depfile:
        for dep in depfile:
            t = dep.replace('\n', '').split('\t')[0]
            h = dep.replace('\n', '').split('\t')[1]
            if t != h:
                FILELIST.append(dep.replace('\n', ''))
                LLIST.append(t)
                RLIST.append(h)
                LSET.add(t)
                RSET.add(h)
        depfile.close()
    # 每个节点 串一下所有节点
    for node2 in FILELIST:
        head = node2.split('\t')[1]
        tail = node2.split('\t')[0]
        heads = []
        tails = []

        heads = makeHeadDep(head)
        tails = makeTailDep(tail)
        for dependence in [x+'->'+y for x in heads for y in tails]:
            dep_res.add(dependence)
    dep_res_co = dep_res.copy()
    for de in dep_res:
        dep_res_co.remove(de)
        for d in dep_res_co:
            if de.find(d) != -1:
                dep_res.remove(d)
            elif d.find(de) != -1:
                dep_res.remove(de)
    with open(r'D:\MyDesk\依赖结果.txt', encoding='utf-8', mode='a') as dep_resfile:
        for res in dep_res:
            dep_resfile.write(res+'\n')
        dep_resfile.close()

posted on 2023-01-13 20:15  春马与夏  阅读(12)  评论(0编辑  收藏  举报  来源

导航