blob: 83f00f7466688c7339e41a363e41eb9f579744f2 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Jim Hebert91c052c2011-03-11 11:00:53 -08002# 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
Mike Frysinger012371e2019-01-03 03:42:32 -05006"""Command to extract the dependancy tree for a given package.
7
8This produces JSON output for other tools to process.
9"""
Don Garrett25f309a2014-03-19 14:02:12 -070010
Mike Frysinger383367e2014-09-16 15:06:17 -040011from __future__ import print_function
Chris McDonaldd2fa6162019-07-30 15:30:58 -060012from __future__ import absolute_import
Mike Frysinger383367e2014-09-16 15:06:17 -040013
Jim Hebert91c052c2011-03-11 11:00:53 -080014import json
Xuewei Zhang656f9932017-09-15 16:15:05 -070015import os
Don Garrettf8bf7842014-03-20 17:03:42 -070016
Chris McDonalda4fb7fe2019-08-19 15:16:57 -060017from chromite.lib.depgraph import DepGraphGenerator
Don Garrettf8bf7842014-03-20 17:03:42 -070018
Jim Hebertcf870d72013-06-12 15:33:34 -070019from chromite.lib import commandline
20from chromite.lib import cros_build_lib
Ralph Nathan446aee92015-03-23 14:44:56 -070021from chromite.lib import cros_logging as logging
Xuewei Zhang656f9932017-09-15 16:15:05 -070022from chromite.lib import osutils
23from chromite.lib import portage_util
Mike Frysingercc838832014-05-24 13:10:30 -040024
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070025def FlattenDepTree(deptree, pkgtable=None, parentcpv=None, get_cpe=False):
Don Garrett25f309a2014-03-19 14:02:12 -070026 """Simplify dependency json.
27
28Turn something like this (the parallel_emerge DepsTree format):
Jim Hebert91c052c2011-03-11 11:00:53 -080029{
30 "app-admin/eselect-1.2.9": {
31 "action": "merge",
32 "deps": {
33 "sys-apps/coreutils-7.5-r1": {
34 "action": "merge",
35 "deps": {},
36 "deptype": "runtime"
37 },
38 ...
39 }
40 }
41}
42 ...into something like this (the cros_extract_deps format):
43{
44 "app-admin/eselect-1.2.9": {
45 "deps": ["coreutils-7.5-r1"],
46 "rev_deps": [],
47 "name": "eselect",
48 "category": "app-admin",
49 "version": "1.2.9",
50 "full_name": "app-admin/eselect-1.2.9",
51 "action": "merge"
52 },
53 "sys-apps/coreutils-7.5-r1": {
54 "deps": [],
55 "rev_deps": ["app-admin/eselect-1.2.9"],
56 "name": "coreutils",
57 "category": "sys-apps",
58 "version": "7.5-r1",
59 "full_name": "sys-apps/coreutils-7.5-r1",
60 "action": "merge"
61 }
62}
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070063
64 Args:
65 deptree: The dependency tree.
66 pkgtable: The package table to update. If None, create a new one.
67 parentcpv: The parent CPV.
68 get_cpe: If set True, include CPE in the flattened dependency tree.
69
70 Returns:
71 A flattened dependency tree.
Jim Hebert91c052c2011-03-11 11:00:53 -080072 """
David James1b363582012-12-17 11:53:11 -080073 if pkgtable is None:
74 pkgtable = {}
Mike Frysinger0bdbc102019-06-13 15:27:29 -040075 for cpv, record in deptree.items():
Jim Hebert91c052c2011-03-11 11:00:53 -080076 if cpv not in pkgtable:
Xuewei Zhang656f9932017-09-15 16:15:05 -070077 split = portage_util.SplitCPV(cpv)
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070078 pkgtable[cpv] = {'deps': [],
79 'rev_deps': [],
Xuewei Zhang656f9932017-09-15 16:15:05 -070080 'name': split.package,
81 'category': split.category,
82 'version': '%s' % split.version,
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070083 'full_name': cpv,
84 'cpes': [],
85 'action': record['action']}
86 if get_cpe:
Xuewei Zhang656f9932017-09-15 16:15:05 -070087 pkgtable[cpv]['cpes'].extend(GetCPEFromCPV(
88 split.category, split.package, split.version_no_rev))
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070089
Jim Hebert91c052c2011-03-11 11:00:53 -080090 # If we have a parent, that is a rev_dep for the current package.
91 if parentcpv:
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070092 pkgtable[cpv]['rev_deps'].append(parentcpv)
Jim Hebert91c052c2011-03-11 11:00:53 -080093 # If current package has any deps, record those.
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070094 for childcpv in record['deps']:
95 pkgtable[cpv]['deps'].append(childcpv)
Jim Hebert91c052c2011-03-11 11:00:53 -080096 # Visit the subtree recursively as well.
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -070097 FlattenDepTree(record['deps'], pkgtable=pkgtable, parentcpv=cpv,
98 get_cpe=get_cpe)
Ned Nguyen3bbd2072019-01-28 19:33:41 -070099 # Sort 'deps' & 'rev_deps' alphabetically to make them more readable.
100 pkgtable[cpv]['deps'].sort()
101 pkgtable[cpv]['rev_deps'].sort()
Jim Hebert91c052c2011-03-11 11:00:53 -0800102 return pkgtable
103
104
Jim Hebertcf870d72013-06-12 15:33:34 -0700105def GetCPEFromCPV(category, package, version):
106 """Look up the CPE for a specified Portage package.
Jim Hebert91c052c2011-03-11 11:00:53 -0800107
Jim Hebertcf870d72013-06-12 15:33:34 -0700108 Args:
109 category: The Portage package's category, e.g. "net-misc"
110 package: The Portage package's name, e.g. "curl"
111 version: The Portage version, e.g. "7.30.0"
112
Mike Frysinger02e1e072013-11-10 22:11:34 -0500113 Returns:
114 A list of CPE Name strings, e.g.
115 ["cpe:/a:curl:curl:7.30.0", "cpe:/a:curl:libcurl:7.30.0"]
Jim Hebertcf870d72013-06-12 15:33:34 -0700116 """
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700117 equery_cmd = ['equery', 'm', '-U', '%s/%s' % (category, package)]
Jim Hebertcf870d72013-06-12 15:33:34 -0700118 lines = cros_build_lib.RunCommand(equery_cmd, error_code_ok=True,
119 print_cmd=False,
120 redirect_stdout=True).output.splitlines()
121 # Look for lines like "Remote-ID: cpe:/a:kernel:linux-pam ID: cpe"
122 # and extract the cpe URI.
123 cpes = []
124 for line in lines:
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700125 if 'ID: cpe' not in line:
Jim Hebertcf870d72013-06-12 15:33:34 -0700126 continue
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700127 cpes.append('%s:%s' % (line.split()[1], version.replace('_', '')))
Jim Hebertcf870d72013-06-12 15:33:34 -0700128 # Note that we're assuming we can combine the root of the CPE, taken
129 # from metadata.xml, and tack on the version number as used by
130 # Portage, and come up with a legitimate CPE. This works so long as
131 # Portage and CPE agree on the precise formatting of the version
Jim Hebert96aff9c2013-07-16 15:43:17 -0700132 # number, which they almost always do. The major exception we've
133 # identified thus far is that our ebuilds have a pattern of inserting
134 # underscores prior to patchlevels, that neither upstream nor CPE
135 # use. For example, our code will decide we have
Jim Hebertcf870d72013-06-12 15:33:34 -0700136 # cpe:/a:todd_miller:sudo:1.8.6_p7 yet the advisories use a format
137 # like cpe:/a:todd_miller:sudo:1.8.6p7, without the underscore. (CPE
138 # is "right" in this example, in that it matches www.sudo.ws.)
139 #
Jim Hebert96aff9c2013-07-16 15:43:17 -0700140 # Removing underscores seems to improve our chances of correctly
141 # arriving at the CPE used by NVD. However, at the end of the day,
142 # ebuild version numbers are rev'd by people who don't have "try to
143 # match NVD" as one of their goals, and there is always going to be
144 # some risk of minor formatting disagreements at the version number
145 # level, if not from stray underscores then from something else.
146 #
Jim Hebertcf870d72013-06-12 15:33:34 -0700147 # This is livable so long as you do some fuzzy version number
148 # comparison in your vulnerability monitoring, between what-we-have
149 # and what-the-advisory-says-is-affected.
150 return cpes
151
152
Chris McDonalde69db662018-11-15 12:50:18 -0700153def GenerateSDKCPVList(sysroot):
Xuewei Zhang656f9932017-09-15 16:15:05 -0700154 """Find all SDK packages from package.provided
155
156 Args:
Chris McDonalde69db662018-11-15 12:50:18 -0700157 sysroot: The board directory to use when finding SDK packages.
Xuewei Zhang656f9932017-09-15 16:15:05 -0700158
159 Returns:
160 A list of CPV Name strings, e.g.
161 ["sys-libs/glibc-2.23-r9", "dev-lang/go-1.8.3-r1"]
162 """
163 # Look at packages in package.provided.
Chris McDonalde69db662018-11-15 12:50:18 -0700164 sdk_file_path = os.path.join(sysroot, 'etc', 'portage',
Xuewei Zhang656f9932017-09-15 16:15:05 -0700165 'profile', 'package.provided')
166 for line in osutils.ReadFile(sdk_file_path).splitlines():
167 # Skip comments and empty lines.
168 line = line.split('#', 1)[0].strip()
169 if not line:
170 continue
171 yield line
172
173
Chris McDonalde69db662018-11-15 12:50:18 -0700174def GenerateCPEList(deps_list, sysroot):
Xuewei Zhang656f9932017-09-15 16:15:05 -0700175 """Generate all CPEs for the packages included in deps_list and SDK packages
176
177 Args:
178 deps_list: A flattened dependency tree (cros_extract_deps format).
Chris McDonalde69db662018-11-15 12:50:18 -0700179 sysroot: The board directory to use when finding SDK packages.
Xuewei Zhang656f9932017-09-15 16:15:05 -0700180
181 Returns:
182 A list of CPE info for packages in deps_list and SDK packages, e.g.
183 [
184 {
185 "ComponentName": "app-admin/sudo",
186 "Repository": "cros",
187 "Targets": [
188 "cpe:/a:todd_miller:sudo:1.8.19p2"
189 ]
190 },
191 {
192 "ComponentName": "sys-libs/glibc",
193 "Repository": "cros",
194 "Targets": [
195 "cpe:/a:gnu:glibc:2.23"
196 ]
197 }
198 ]
199 """
Jim Hebertcf870d72013-06-12 15:33:34 -0700200 cpe_dump = []
Xuewei Zhang656f9932017-09-15 16:15:05 -0700201
202 # Generage CPEs for SDK packages.
Mike Frysinger1457e482019-01-03 04:04:02 -0500203 for sdk_cpv in sorted(GenerateSDKCPVList(sysroot)):
Xuewei Zhang656f9932017-09-15 16:15:05 -0700204 # Only add CPE for SDK CPVs missing in deps_list.
205 if deps_list.get(sdk_cpv) is not None:
206 continue
207
208 split = portage_util.SplitCPV(sdk_cpv)
209 cpes = GetCPEFromCPV(split.category, split.package, split.version_no_rev)
210 if cpes:
Alex Klein9f93b482018-10-01 09:26:51 -0600211 cpe_dump.append({'ComponentName': '%s' % split.cp,
Xuewei Zhang656f9932017-09-15 16:15:05 -0700212 'Repository': 'cros',
213 'Targets': sorted(cpes)})
214 else:
215 logging.warning('No CPE entry for %s', sdk_cpv)
216
217 # Generage CPEs for packages in deps_list.
Mike Frysinger1457e482019-01-03 04:04:02 -0500218 for cpv, record in sorted(deps_list.items()):
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700219 if record['cpes']:
220 name = '%s/%s' % (record['category'], record['name'])
221 cpe_dump.append({'ComponentName': name,
222 'Repository': 'cros',
223 'Targets': sorted(record['cpes'])})
Jim Hebertcf870d72013-06-12 15:33:34 -0700224 else:
Ralph Nathan446aee92015-03-23 14:44:56 -0700225 logging.warning('No CPE entry for %s', cpv)
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700226 return sorted(cpe_dump, key=lambda k: k['ComponentName'])
Jim Hebert91c052c2011-03-11 11:00:53 -0800227
228
Chris McDonalde69db662018-11-15 12:50:18 -0700229def ParseArgs(argv):
230 """Parse command line arguments."""
Mike Frysinger012371e2019-01-03 03:42:32 -0500231 parser = commandline.ArgumentParser(description=__doc__)
Chris McDonalde69db662018-11-15 12:50:18 -0700232 target = parser.add_mutually_exclusive_group()
233 target.add_argument('--sysroot', type='path', help='Path to the sysroot.')
234 target.add_argument('--board', help='Board name.')
235
Yu-Ju Hong7f01e9a2014-10-23 11:01:57 -0700236 parser.add_argument('--format', default='deps',
237 choices=['deps', 'cpe'],
Yu-Ju Hongf48d3982014-10-30 16:12:16 -0700238 help='Output either traditional deps or CPE-only JSON.')
239 parser.add_argument('--output-path', default=None,
240 help='Write output to the given path.')
Mike Frysinger012371e2019-01-03 03:42:32 -0500241 parser.add_argument('pkgs', nargs='*')
242 opts = parser.parse_args(argv)
243 opts.Freeze()
244 return opts
Jim Hebert91c052c2011-03-11 11:00:53 -0800245
Chris McDonalde69db662018-11-15 12:50:18 -0700246
Ned Nguyene16dcfb2019-03-22 10:36:05 -0600247def FilterObsoleteDeps(package_deps):
248 """Remove all the packages that are to be uninstalled from |package_deps|.
249
250 Returns:
251 None since this method mutates |package_deps| directly.
252 """
253 obsolete_package_deps = []
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400254 for k, v in package_deps.items():
Ned Nguyene16dcfb2019-03-22 10:36:05 -0600255 if v['action'] in ('merge', 'nomerge'):
256 continue
257 elif v['action'] == 'uninstall':
258 obsolete_package_deps.append(k)
259 else:
260 assert False, (
261 'Unrecognized action. Package dep data: %s' % v)
262 for p in obsolete_package_deps:
263 del package_deps[p]
264
265
Chris McDonalde69db662018-11-15 12:50:18 -0700266def ExtractDeps(sysroot, package_list, formatting='deps'):
Ned Nguyendd3e09f2019-03-14 18:54:03 -0600267 """Returns the set of dependencies for the packages in package_list.
268
269 For calculating dependencies graph, this should only consider packages
270 that are DEPENDS or RDEPENDS. Essentially, this should answer the question
271 "which are all the packages which changing them may change the execution
272 of any binaries produced by packages in |package_list|."
273
274 Args:
275 sysroot: the path (string) to the root directory into which the package is
276 pretend to be merged. This value is also used for setting
277 PORTAGE_CONFIGROOT.
278 package_list: the list of packages (CP string) to extract their dependencies
279 from.
280 formatting: can either be 'deps' or 'cpe'. For 'deps', see the return
281 format in docstring of FlattenDepTree, for 'cpe', see the return format in
282 docstring of GenerateCPEList.
283
284 Returns:
285 A JSON-izable object that either follows 'deps' or 'cpe' format.
286 """
Chris McDonalde69db662018-11-15 12:50:18 -0700287 lib_argv = ['--quiet', '--pretend', '--emptytree']
288 lib_argv += ['--sysroot=%s' % sysroot]
289 lib_argv.extend(package_list)
Jim Hebert91c052c2011-03-11 11:00:53 -0800290
291 deps = DepGraphGenerator()
Jim Hebertcf870d72013-06-12 15:33:34 -0700292 deps.Initialize(lib_argv)
David James1b363582012-12-17 11:53:11 -0800293 deps_tree, _deps_info = deps.GenDependencyTree()
Chris McDonalde69db662018-11-15 12:50:18 -0700294 flattened_deps = FlattenDepTree(deps_tree, get_cpe=(formatting == 'cpe'))
Ned Nguyene16dcfb2019-03-22 10:36:05 -0600295
296 # Workaround: since emerge doesn't honor the --emptytree flag, for now we need
297 # to manually filter out packages that are obsolete (meant to be
298 # uninstalled by emerge)
299 # TODO(crbug.com/938605): remove this work around once
300 # https://bugs.gentoo.org/681308 is addressed.
301 FilterObsoleteDeps(flattened_deps)
302
Chris McDonalde69db662018-11-15 12:50:18 -0700303 if formatting == 'cpe':
304 flattened_deps = GenerateCPEList(flattened_deps, sysroot)
305 return flattened_deps
306
307
308def main(argv):
Mike Frysinger012371e2019-01-03 03:42:32 -0500309 opts = ParseArgs(argv)
Chris McDonalde69db662018-11-15 12:50:18 -0700310
Mike Frysinger012371e2019-01-03 03:42:32 -0500311 sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board)
312 deps_list = ExtractDeps(sysroot, opts.pkgs, opts.format)
Yu-Ju Hongf48d3982014-10-30 16:12:16 -0700313
314 deps_output = json.dumps(deps_list, sort_keys=True, indent=2)
Mike Frysinger012371e2019-01-03 03:42:32 -0500315 if opts.output_path:
316 with open(opts.output_path, 'w') as f:
Yu-Ju Hongf48d3982014-10-30 16:12:16 -0700317 f.write(deps_output)
318 else:
319 print(deps_output)