brpkgutil.py 1.8 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 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, universal_newlines=True)
  29. output = p.communicate()[0]
  30. for l in output.splitlines():
  31. if " -> " in l:
  32. pkg = l.split(" -> ")[0]
  33. deps[pkg] += l.split(" -> ")[1].split()
  34. for p in l.split(" -> ")[1].split():
  35. rdeps[p].append(pkg)
  36. else:
  37. pkg, type_version = l.split(": ", 1)
  38. t, v = "{} -".format(type_version).split(None, 2)[:2]
  39. deps['all'].append(pkg)
  40. types[pkg] = t
  41. versions[pkg] = v
  42. return (deps, rdeps, types, versions)