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