brpkgutil.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2010-2013 Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  2. # Copyright (C) 2019 Yann E. MORIN <yann.morin.1998@free.fr>
  3. import json
  4. import logging
  5. import os
  6. import subprocess
  7. from collections import defaultdict
  8. # This function returns a tuple of four dictionaries, all using package
  9. # names as keys:
  10. # - a dictionary which values are the lists of packages that are the
  11. # dependencies of the package used as key;
  12. # - a dictionary which values are the lists of packages that are the
  13. # reverse dependencies of the package used as key;
  14. # - a dictionary which values are the type of the package used as key;
  15. # - a dictionary which values are the version of the package used as key,
  16. # 'virtual' for a virtual package, or the empty string for a rootfs.
  17. def get_dependency_tree():
  18. logging.info("Getting dependency tree...")
  19. deps = {}
  20. rdeps = defaultdict(list)
  21. types = {}
  22. versions = {}
  23. # Special case for the 'all' top-level fake package
  24. deps['all'] = []
  25. types['all'] = 'target'
  26. versions['all'] = ''
  27. cmd = ["make", "-s", "--no-print-directory", "show-info"]
  28. with open(os.devnull, 'wb') as devnull:
  29. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=devnull,
  30. universal_newlines=True)
  31. pkg_list = json.loads(p.communicate()[0])
  32. for pkg in pkg_list:
  33. deps['all'].append(pkg)
  34. types[pkg] = pkg_list[pkg]["type"]
  35. deps[pkg] = pkg_list[pkg].get("dependencies", [])
  36. for p in deps[pkg]:
  37. rdeps[p].append(pkg)
  38. versions[pkg] = \
  39. None if pkg_list[pkg]["type"] == "rootfs" \
  40. else "virtual" if pkg_list[pkg]["virtual"] \
  41. else pkg_list[pkg]["version"]
  42. return (deps, rdeps, types, versions)