transitive_dependencies.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Buildgen transitive dependencies
  15. This takes the list of libs, node_modules, and targets from our
  16. yaml dictionary, and adds to each the transitive closure
  17. of the list of dependencies.
  18. """
  19. def transitive_deps(lib_map, node):
  20. """Returns a list of transitive dependencies from node.
  21. Recursively iterate all dependent node in a depth-first fashion and
  22. list a result using a topological sorting.
  23. """
  24. result = []
  25. seen = set()
  26. start = node
  27. def recursive_helper(node):
  28. if node is None:
  29. return
  30. for dep in node.get("deps", []):
  31. if dep not in seen:
  32. seen.add(dep)
  33. next_node = lib_map.get(dep)
  34. recursive_helper(next_node)
  35. if node is not start:
  36. result.insert(0, node["name"])
  37. recursive_helper(node)
  38. return result
  39. def mako_plugin(dictionary):
  40. """The exported plugin code for transitive_dependencies.
  41. Iterate over each list and check each item for a deps list. We add a
  42. transitive_deps property to each with the transitive closure of those
  43. dependency lists. The result list is sorted in a topological ordering.
  44. """
  45. lib_map = {lib['name']: lib for lib in dictionary.get('libs')}
  46. for target_name, target_list in list(dictionary.items()):
  47. for target in target_list:
  48. if isinstance(target, dict):
  49. if 'deps' in target or target_name == 'libs':
  50. if not 'deps' in target:
  51. # make sure all the libs have the "deps" field populated
  52. target['deps'] = []
  53. target['transitive_deps'] = transitive_deps(lib_map, target)
  54. python_dependencies = dictionary.get('python_dependencies')
  55. python_dependencies['transitive_deps'] = transitive_deps(
  56. lib_map, python_dependencies)