brpkgutil.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 logging
  4. import os
  5. import subprocess
  6. from collections import defaultdict
  7. # This function returns a tuple of four dictionaries, all using package
  8. # names as keys:
  9. # - a dictionary which values are the lists of packages that are the
  10. # dependencies of the package used as key;
  11. # - a dictionary which values are the lists of packages that are the
  12. # reverse dependencies of the package used as key;
  13. # - a dictionary which values are the type of the package used as key;
  14. # - a dictionary which values are the version of the package used as key,
  15. # 'virtual' for a virtual package, or the empty string for a rootfs.
  16. def get_dependency_tree():
  17. logging.info("Getting dependency tree...")
  18. deps = defaultdict(list)
  19. rdeps = defaultdict(list)
  20. types = {}
  21. versions = {}
  22. # Special case for the 'all' top-level fake package
  23. deps['all'] = []
  24. types['all'] = 'target'
  25. versions['all'] = ''
  26. cmd = ["make", "-s", "--no-print-directory", "show-dependency-tree"]
  27. with open(os.devnull, 'wb') as devnull:
  28. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=devnull,
  29. universal_newlines=True)
  30. output = p.communicate()[0]
  31. for l in output.splitlines():
  32. if " -> " in l:
  33. pkg = l.split(" -> ")[0]
  34. deps[pkg] += l.split(" -> ")[1].split()
  35. for p in l.split(" -> ")[1].split():
  36. rdeps[p].append(pkg)
  37. else:
  38. pkg, type_version = l.split(": ", 1)
  39. t, v = "{} -".format(type_version).split(None, 2)[:2]
  40. deps['all'].append(pkg)
  41. types[pkg] = t
  42. versions[pkg] = v
  43. return (deps, rdeps, types, versions)