-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.py
53 lines (43 loc) · 1.19 KB
/
deps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: GPL-2.0
"""
Package tool
~~~~~~~~~~~~
Package tool
File: deps.py
Desc: 依赖关系图
Date: 2023-06-19
Auth: XiangYang<[email protected]>
"""
from graphlib import TopologicalSorter
from typing import Dict, List, Set
class DependenceGraphic:
"""
依赖关系图
"""
deps: Dict[str, List[str]]
def __init__(self, deps: Dict[str, List[str]]):
"""
从依赖map构造
"""
self.deps = deps
def dep_of(self, item: str) -> List[str]:
"""
返回依赖的可行运行顺序
"""
if item not in self.deps:
return [item]
else:
deps = self.deps
# 抓走所有的依赖项
q = [item]
sub_deps: Dict[str, Set[str]] = {}
while len(q) > 0:
name = q.pop()
dep_items = set(deps[name])
if len(dep_items) > 0:
sub_deps.update({name: dep_items})
for dep_item in dep_items:
q.append(dep_item)
# 返回拓扑序
return list(TopologicalSorter(sub_deps).static_order())