blob: 32f345aab7860a981f79df3117e63109d2694d6a [file] [log] [blame]
Jim Hebert91c052c2011-03-11 11:00:53 -08001#!/usr/bin/python2.6
2# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import json
7import portage
8import sys
9from parallel_emerge import DepGraphGenerator
10
11def FlattenDepTree(deptree, pkgtable={}, parentcpv=None):
12 """
13 Turn something like this (the parallel_emerge DepsTree format):
14{
15 "app-admin/eselect-1.2.9": {
16 "action": "merge",
17 "deps": {
18 "sys-apps/coreutils-7.5-r1": {
19 "action": "merge",
20 "deps": {},
21 "deptype": "runtime"
22 },
23 ...
24 }
25 }
26}
27 ...into something like this (the cros_extract_deps format):
28{
29 "app-admin/eselect-1.2.9": {
30 "deps": ["coreutils-7.5-r1"],
31 "rev_deps": [],
32 "name": "eselect",
33 "category": "app-admin",
34 "version": "1.2.9",
35 "full_name": "app-admin/eselect-1.2.9",
36 "action": "merge"
37 },
38 "sys-apps/coreutils-7.5-r1": {
39 "deps": [],
40 "rev_deps": ["app-admin/eselect-1.2.9"],
41 "name": "coreutils",
42 "category": "sys-apps",
43 "version": "7.5-r1",
44 "full_name": "sys-apps/coreutils-7.5-r1",
45 "action": "merge"
46 }
47}
48 """
49 for cpv, record in deptree.items():
50 if cpv not in pkgtable:
51 cat, nam, ver, rev = portage.catpkgsplit(cpv)
52 pkgtable[cpv] = {"deps": [],
53 "rev_deps": [],
54 "name": nam,
55 "category": cat,
56 "version": "%s-%s" % (ver, rev),
57 "full_name": cpv,
58 "action": record["action"]}
59 # If we have a parent, that is a rev_dep for the current package.
60 if parentcpv:
61 pkgtable[cpv]["rev_deps"].append(parentcpv)
62 # If current package has any deps, record those.
63 for childcpv in record["deps"]:
64 pkgtable[cpv]["deps"].append(childcpv)
65 # Visit the subtree recursively as well.
66 FlattenDepTree(record["deps"], pkgtable=pkgtable, parentcpv=cpv)
67 return pkgtable
68
69
70def Usage():
71 """Print usage."""
72 print """Usage:
73cros_extract_deps [--board=BOARD] [emerge args] package
74
75This extracts the dependency tree for the specified package, and outputs it
76to stdout, in a serialized JSON format. Emerge flags such as --root-deps
77can be used to influence what sort of dependency tree is extracted.
78"""
79
80
Brian Harring30675052012-02-29 12:18:22 -080081def main(argv):
82 if len(argv) == 1:
Jim Hebert91c052c2011-03-11 11:00:53 -080083 Usage()
84 sys.exit(1)
85
86 # We want the toolchain to be quiet because we are going to output
87 # a well-formed json payload.
David James0bdc5de2011-05-12 16:22:26 -070088 argv = ['--quiet', '--pretend']
Jim Hebert91c052c2011-03-11 11:00:53 -080089
90 # cros_extract_deps defaults to rdeps. However, only use this if
91 # there was no explicit --root-deps command line switch.
92 default_rootdeps_arg = ['--root-deps=rdeps']
Brian Harring30675052012-02-29 12:18:22 -080093 for arg in argv:
Jim Hebert91c052c2011-03-11 11:00:53 -080094 if arg.startswith('--root-deps'):
95 default_rootdeps_arg = []
96
97 # Now, assemble the overall argv as the concatenation of the
98 # default list + possible rootdeps-default + actual command line.
99 argv.extend(default_rootdeps_arg)
100 argv.extend(sys.argv[1:])
101
102 deps = DepGraphGenerator()
103 deps.Initialize(argv)
David James386ccd12011-05-04 20:17:42 -0700104 deps_tree, deps_info = deps.GenDependencyTree()
Jim Hebert91c052c2011-03-11 11:00:53 -0800105 print json.dumps(FlattenDepTree(deps_tree), sort_keys=True, indent=2)