2022-09-22 01:17:05 +02:00
|
|
|
def topological_sort(source):
|
|
|
|
pending = [(name, set(deps)) for name, deps in source]
|
|
|
|
emitted = []
|
|
|
|
while pending:
|
|
|
|
next_pending = []
|
|
|
|
next_emitted = []
|
|
|
|
for entry in pending:
|
|
|
|
name, deps = entry
|
2022-09-22 01:51:47 +02:00
|
|
|
deps.difference_update(set((name,)), emitted) # <-- pop self from dep, req Py2.6
|
2022-09-22 01:17:05 +02:00
|
|
|
if deps:
|
|
|
|
next_pending.append(entry)
|
|
|
|
else:
|
|
|
|
yield name
|
2022-09-22 01:51:47 +02:00
|
|
|
emitted.append(name) # <-- not required, but preserves original order
|
2022-09-22 01:17:05 +02:00
|
|
|
next_emitted.append(name)
|
|
|
|
if not next_emitted:
|
|
|
|
raise ValueError("cyclic dependancy detected: %s %r" % (name, (next_pending,)))
|
|
|
|
pending = next_pending
|
|
|
|
emitted = next_emitted
|
|
|
|
return emitted
|
|
|
|
|
2022-09-22 01:51:47 +02:00
|
|
|
|
2022-09-22 01:17:05 +02:00
|
|
|
def sort_libs_by_dependencies(selected_libs):
|
2022-09-22 01:51:47 +02:00
|
|
|
deplist = []
|
2022-09-22 01:17:05 +02:00
|
|
|
for lib in selected_libs:
|
|
|
|
name = lib['name']
|
|
|
|
dependencies = set(lib['dependencies'].split(','))
|
|
|
|
if dependencies == {''}:
|
2022-09-22 01:51:47 +02:00
|
|
|
dependencies = {}
|
|
|
|
deplist.append([name, dependencies])
|
2022-09-22 01:17:05 +02:00
|
|
|
sorted_deplist = topological_sort(deplist)
|
|
|
|
sorted = []
|
|
|
|
for entry in sorted_deplist:
|
|
|
|
for lib in selected_libs:
|
|
|
|
if lib['name'] == entry:
|
|
|
|
sorted.append(lib)
|
|
|
|
return sorted
|