defpackage_dependencies(dependencies):
result = []
visiting = set([])
visited = set([])
defdfs(node):
if node in visited:
return
visiting.add(node)
for dependency in node.dependencies:
if dependency in visiting:
raiseException("Have cycle in dependency graph.")
if dependency notin visited:
dfs(dependency)
visiting.remove(node)
visited.add(node)
result.add(node)
for node in dependencies:
dfs(node)
return result