| # Copyright 2010 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Command to extract the dependency tree for a given package. | |
| This produces JSON output for other tools to process. | |
| """ | |
| from __future__ import absolute_import | |
| import logging | |
| import re | |
| import sys | |
| from chromite.lib import build_target_lib | |
| from chromite.lib import commandline | |
| from chromite.lib import cros_build_lib | |
| from chromite.lib import sysroot_lib | |
| from chromite.lib.depgraph import DepGraphGenerator | |
| from chromite.lib.parser import package_info | |
| from chromite.utils import pformat | |
| def FlattenDepTree(deptree, pkgtable=None, parentcpv=None, get_cpe=False): | |
| """Simplify dependency json. | |
| Turn something like this (the parallel_emerge DepsTree format): | |
| { | |
| "app-admin/eselect-1.2.9": { | |
| "action": "merge", | |
| "deps": { | |
| "sys-apps/coreutils-7.5-r1": { | |
| "action": "merge", | |
| "deps": {}, | |
| "deptype": "runtime" | |
| }, | |
| ... | |
| } | |
| } | |
| } | |
| ...into something like this (the cros_extract_deps format): | |
| { | |
| "app-admin/eselect-1.2.9": { | |
| "deps": ["coreutils-7.5-r1"], | |
| "rev_deps": [], | |
| "name": "eselect", | |
| "category": "app-admin", | |
| "version": "1.2.9", | |
| "full_name": "app-admin/eselect-1.2.9", | |
| "action": "merge" | |
| }, | |
| "sys-apps/coreutils-7.5-r1": { | |
| "deps": [], | |
| "rev_deps": ["app-admin/eselect-1.2.9"], | |
| "name": "coreutils", | |
| "category": "sys-apps", | |
| "version": "7.5-r1", | |
| "full_name": "sys-apps/coreutils-7.5-r1", | |
| "action": "merge" | |
| } | |
| } | |
| Args: | |
| deptree: The dependency tree. | |
| pkgtable: The package table to update. If None, create a new one. | |
| parentcpv: The parent CPV. | |
| get_cpe: If set True, include CPE in the flattened dependency tree. | |
| Returns: | |
| A flattened dependency tree. | |
| """ | |
| if pkgtable is None: | |
| pkgtable = {} | |
| for cpv, record in deptree.items(): | |
| if cpv not in pkgtable: | |
| pkg_info = package_info.parse(cpv) | |
| pkgtable[cpv] = { | |
| "deps": [], | |
| "rev_deps": [], | |
| "name": pkg_info.package, | |
| "category": pkg_info.category, | |
| "version": pkg_info.vr, | |
| "full_name": cpv, | |
| "cpes": [], | |
| "action": record["action"], | |
| } | |
| if get_cpe: | |
| pkgtable[cpv]["cpes"].extend( | |
| GetCPEFromCPV( | |
| pkg_info.category, pkg_info.package, pkg_info.version | |
| ) | |
| ) | |
| # If we have a parent, that is a rev_dep for the current package. | |
| if parentcpv: | |
| pkgtable[cpv]["rev_deps"].append(parentcpv) | |
| # If current package has any deps, record those. | |
| for childcpv in record["deps"]: | |
| pkgtable[cpv]["deps"].append(childcpv) | |
| # Visit the subtree recursively as well. | |
| FlattenDepTree( | |
| record["deps"], pkgtable=pkgtable, parentcpv=cpv, get_cpe=get_cpe | |
| ) | |
| # Sort 'deps' & 'rev_deps' alphabetically to make them more readable. | |
| pkgtable[cpv]["deps"].sort() | |
| pkgtable[cpv]["rev_deps"].sort() | |
| return pkgtable | |
| # Portage version suffixes and their PEP 440 equivalents. Matches the | |
| # translation done by Gentoo's pypi.eclass (pypi_translate_version). | |
| _PEP440_SUFFIXES = { | |
| "alpha": "a", | |
| "beta": "b", | |
| "pre": ".dev", | |
| "rc": "rc", | |
| "p": ".post", | |
| } | |
| _PORTAGE_VERSION_RE = re.compile( | |
| r"(?P<release>\d+(?:\.\d+)*)" | |
| r"(?P<suffixes>(?:_(?:alpha|beta|pre|rc|p)\d*)*)" | |
| r"(?:-r\d+)?" | |
| ) | |
| _PORTAGE_SUFFIX_RE = re.compile(r"_(alpha|beta|pre|rc|p)(\d*)") | |
| # Upstream CPython repository, as used by the OSV PSF advisories. | |
| _CPYTHON_REPO = "https://github.com/python/cpython" | |
| # Commit of each CPython release tag (refs/tags/v<version>^{}). A Git | |
| # identifier with this commit lets AutoVM match PSF advisories in OSV. | |
| # Add the new version here when dev-lang/python is uprevved. | |
| CPYTHON_TAG_COMMITS = { | |
| "3.6.15": "b74b1f36993a4e1700869133f3be13dd60ef4e40", | |
| "3.8.19": "469ede4dbc3ef4146803424a9a3a1e16391f5013", | |
| "3.8.20": "39b2f82717a69dde7212bc39b673b0f55c99e6a3", | |
| "3.11.15": "2340a037f7450e70fccfe411e6531afb4d57a312", | |
| "3.13.13": "01104ce1beb3135c2e0c01ec835b994c1f55a1c0", | |
| } | |
| _CPYTHON_VERSION_RE = re.compile(r"(\d+\.\d+\.\d+)(?:_p\d*)?(?:-r\d+)?") | |
| # PyPI packages installed from outside the dev-python category. | |
| _PYPI_PACKAGES_OUTSIDE_DEV_PYTHON = frozenset(("app-admin/awscli",)) | |
| def _IsPyPIPackage(category, package): | |
| """Whether the package is installed from PyPI.""" | |
| return category == "dev-python" or ( | |
| "%s/%s" % (category, package) in _PYPI_PACKAGES_OUTSIDE_DEV_PYTHON | |
| ) | |
| def PortageToPep440(version): | |
| """Convert a Portage version to a PEP 440 version. | |
| Args: | |
| version: The Portage version, e.g. "2.2.1_p20211118". | |
| Returns: | |
| The PEP 440 version, e.g. "2.2.1.post20211118", or None if the | |
| version cannot be converted. | |
| """ | |
| match = _PORTAGE_VERSION_RE.fullmatch(version) | |
| if not match: | |
| return None | |
| result = match.group("release") | |
| for suffix, num in _PORTAGE_SUFFIX_RE.findall(match.group("suffixes")): | |
| result += _PEP440_SUFFIXES[suffix] + (num or "0") | |
| return result | |
| def _GetPyPIIdentifier(category, package, version, pypi_name): | |
| """Build a PyPI identifier, or return None if it cannot be built.""" | |
| pep440_version = PortageToPep440(version) | |
| if not pep440_version: | |
| logging.warning( | |
| "Cannot convert %s/%s-%s to a PEP 440 version", | |
| category, | |
| package, | |
| version, | |
| ) | |
| return None | |
| # Normalize the name the same way AutoVM normalizes pypi PURLs. | |
| name = pypi_name.lower().replace("_", "-") | |
| return "PyPI:%s:%s" % (name, pep440_version) | |
| def _GetCPythonIdentifier(version): | |
| """Build a Git identifier for CPython, or return None if unknown.""" | |
| match = _CPYTHON_VERSION_RE.fullmatch(version) | |
| commit = CPYTHON_TAG_COMMITS.get(match.group(1)) if match else None | |
| if not commit: | |
| logging.warning( | |
| "No CPython tag commit for dev-lang/python-%s; " | |
| "add it to CPYTHON_TAG_COMMITS", | |
| version, | |
| ) | |
| return None | |
| return "Git:%s:%s" % (_CPYTHON_REPO, commit) | |
| def GetCPEFromCPV(category, package, version): | |
| """Look up the CPE for a specified Portage package. | |
| Besides CPEs, this also returns "PyPI:<name>:<version>" identifiers | |
| for PyPI packages (see _IsPyPIPackage) that have a pypi remote-id. | |
| For dev-lang/python it also returns a "Git:<repo>:<commit>" | |
| identifier. | |
| Args: | |
| category: The Portage package's category, e.g. "net-misc" | |
| package: The Portage package's name, e.g. "curl" | |
| version: The Portage version, e.g. "7.30.0" | |
| Returns: | |
| A list of CPE Name strings, e.g. | |
| ["cpe:/a:curl:curl:7.30.0", "cpe:/a:curl:libcurl:7.30.0"] | |
| """ | |
| equery_cmd = ["equery", "m", "-U", "%s/%s" % (category, package)] | |
| lines = cros_build_lib.run( | |
| equery_cmd, check=False, print_cmd=False, stdout=True, encoding="utf-8" | |
| ).stdout.splitlines() | |
| # Look for lines like "Remote-ID: cpe:/a:kernel:linux-pam ID: cpe" | |
| # and extract the cpe URI. | |
| cpes = [] | |
| for line in lines: | |
| if _IsPyPIPackage(category, package) and line.rstrip().endswith( | |
| "ID: pypi" | |
| ): | |
| identifier = _GetPyPIIdentifier( | |
| category, package, version, line.split()[1] | |
| ) | |
| if identifier and identifier not in cpes: | |
| cpes.append(identifier) | |
| continue | |
| if "ID: cpe" not in line: | |
| continue | |
| cpes.append("%s:%s" % (line.split()[1], version.replace("_", ""))) | |
| # Note that we're assuming we can combine the root of the CPE, taken | |
| # from metadata.xml, and tack on the version number as used by | |
| # Portage, and come up with a legitimate CPE. This works so long as | |
| # Portage and CPE agree on the precise formatting of the version | |
| # number, which they almost always do. The major exception we've | |
| # identified thus far is that our ebuilds have a pattern of inserting | |
| # underscores prior to patchlevels, that neither upstream nor CPE | |
| # use. For example, our code will decide we have | |
| # cpe:/a:todd_miller:sudo:1.8.6_p7 yet the advisories use a format | |
| # like cpe:/a:todd_miller:sudo:1.8.6p7, without the underscore. (CPE | |
| # is "right" in this example, in that it matches www.sudo.ws.) | |
| # | |
| # Removing underscores seems to improve our chances of correctly | |
| # arriving at the CPE used by NVD. However, at the end of the day, | |
| # ebuild version numbers are rev'd by people who don't have "try to | |
| # match NVD" as one of their goals, and there is always going to be | |
| # some risk of minor formatting disagreements at the version number | |
| # level, if not from stray underscores then from something else. | |
| # | |
| # This is livable so long as you do some fuzzy version number | |
| # comparison in your vulnerability monitoring, between what-we-have | |
| # and what-the-advisory-says-is-affected. | |
| if category == "dev-lang" and package == "python": | |
| identifier = _GetCPythonIdentifier(version) | |
| if identifier: | |
| cpes.append(identifier) | |
| return cpes | |
| def GenerateCPEList(deps_list, sysroot): | |
| """Generate all CPEs for the packages included in deps_list and SDK packages | |
| Args: | |
| deps_list: A flattened dependency tree (cros_extract_deps format). | |
| sysroot: The board directory to use when finding SDK packages. | |
| Returns: | |
| A list of CPE info for packages in deps_list and SDK packages, e.g. | |
| [ | |
| { | |
| "ComponentName": "app-admin/sudo", | |
| "Repository": "cros", | |
| "Targets": [ | |
| "cpe:/a:todd_miller:sudo:1.8.19p2" | |
| ] | |
| }, | |
| { | |
| "ComponentName": "sys-libs/glibc", | |
| "Repository": "cros", | |
| "Targets": [ | |
| "cpe:/a:gnu:glibc:2.23" | |
| ] | |
| } | |
| ] | |
| """ | |
| cpe_dump = [] | |
| # Generate CPEs for SDK packages. | |
| for pkg_info in sorted( | |
| sysroot_lib.get_sdk_provided_packages(sysroot), key=lambda x: x.cpvr | |
| ): | |
| # Only add CPE for SDK CPVs missing in deps_list. | |
| if deps_list.get(pkg_info.cpvr) is not None: | |
| continue | |
| cpes = GetCPEFromCPV( | |
| pkg_info.category, pkg_info.package, pkg_info.version | |
| ) | |
| if cpes: | |
| cpe_dump.append( | |
| { | |
| "ComponentName": "%s" % pkg_info.atom, | |
| "Repository": "cros", | |
| "Targets": sorted(cpes), | |
| } | |
| ) | |
| else: | |
| logging.warning("No CPE entry for %s", pkg_info.cpvr) | |
| # Generate CPEs for packages in deps_list. | |
| for cpv, record in sorted(deps_list.items()): | |
| if record["cpes"]: | |
| name = "%s/%s" % (record["category"], record["name"]) | |
| cpe_dump.append( | |
| { | |
| "ComponentName": name, | |
| "Repository": "cros", | |
| "Targets": sorted(record["cpes"]), | |
| } | |
| ) | |
| else: | |
| logging.warning("No CPE entry for %s", cpv) | |
| return sorted(cpe_dump, key=lambda k: k["ComponentName"]) | |
| def ParseArgs(argv): | |
| """Parse command line arguments.""" | |
| parser = commandline.ArgumentParser(description=__doc__) | |
| target = parser.add_mutually_exclusive_group() | |
| target.add_argument("--sysroot", type="path", help="Path to the sysroot.") | |
| target.add_argument("--board", help="Board name.") | |
| parser.add_argument( | |
| "--format", | |
| default="deps", | |
| choices=["deps", "cpe"], | |
| help="Output either traditional deps or CPE-only JSON.", | |
| ) | |
| parser.add_argument( | |
| "--output-path", default=None, help="Write output to the given path." | |
| ) | |
| parser.add_argument("pkgs", nargs="*") | |
| opts = parser.parse_args(argv) | |
| opts.Freeze() | |
| return opts | |
| def FilterObsoleteDeps(package_deps): | |
| """Remove all the packages that are to be uninstalled from |package_deps|. | |
| Returns: | |
| None since this method mutates |package_deps| directly. | |
| """ | |
| obsolete_package_deps = [] | |
| for k, v in package_deps.items(): | |
| if v["action"] in ("merge", "nomerge"): | |
| continue | |
| elif v["action"] == "uninstall": | |
| obsolete_package_deps.append(k) | |
| else: | |
| assert False, "Unrecognized action. Package dep data: %s" % v | |
| for p in obsolete_package_deps: | |
| del package_deps[p] | |
| def ExtractDeps( | |
| sysroot, | |
| package_list, | |
| formatting="deps", | |
| include_bdepend=True, | |
| backtrack=True, | |
| ): | |
| """Returns the set of dependencies for the packages in package_list. | |
| For calculating dependencies graph, this should only consider packages | |
| that are DEPENDS, RDEPENDS, or BDEPENDS. Essentially, this should answer the | |
| question "which are all the packages which changing them may change the | |
| execution of any binaries produced by packages in |package_list|." | |
| Args: | |
| sysroot: the path (string) to the root directory into which the package | |
| is pretend to be merged. This value is also used for setting | |
| PORTAGE_CONFIGROOT. | |
| package_list: the list of packages (CP string) to extract their | |
| dependencies from. | |
| formatting: can either be 'deps' or 'cpe'. For 'deps', see the return | |
| format in docstring of FlattenDepTree, for 'cpe', see the return | |
| format in docstring of GenerateCPEList. | |
| include_bdepend: Controls whether BDEPEND packages that would be | |
| installed to BROOT (usually "/" instead of ROOT) are included in the | |
| output. | |
| backtrack: Setting to False disables backtracking in Portage's | |
| dependency solver. If the highest available version of dependencies | |
| doesn't produce a solvable graph Portage will give up and return an | |
| error instead of trying other candidates. | |
| Returns: | |
| A JSON-izable object that either follows 'deps' or 'cpe' format. | |
| """ | |
| lib_argv = ["--quiet", "--pretend", "--emptytree"] | |
| if include_bdepend: | |
| lib_argv += ["--include-bdepend"] | |
| if not backtrack: | |
| lib_argv += ["--backtrack=0"] | |
| lib_argv += ["--sysroot=%s" % sysroot] | |
| lib_argv.extend(package_list) | |
| deps = DepGraphGenerator() | |
| deps.Initialize(lib_argv) | |
| deps_tree, _deps_info, bdeps_tree = deps.GenDependencyTree() | |
| trees = (deps_tree, bdeps_tree) | |
| flattened_trees = tuple( | |
| FlattenDepTree(tree, get_cpe=(formatting == "cpe")) for tree in trees | |
| ) | |
| # Workaround: since emerge doesn't honor the --emptytree flag, for now we | |
| # need to manually filter out packages that are obsolete (meant to be | |
| # uninstalled by emerge) | |
| # TODO(crbug.com/938605): remove this work around once | |
| # https://bugs.gentoo.org/681308 is addressed. | |
| for tree in flattened_trees: | |
| FilterObsoleteDeps(tree) | |
| if formatting == "cpe": | |
| flattened_trees = tuple( | |
| GenerateCPEList(tree, sysroot) for tree in flattened_trees | |
| ) | |
| return flattened_trees | |
| def main(argv): | |
| opts = ParseArgs(argv) | |
| sysroot = opts.sysroot or build_target_lib.get_default_sysroot_path( | |
| opts.board | |
| ) | |
| deps_list, _ = ExtractDeps(sysroot, opts.pkgs, opts.format) | |
| pformat.json( | |
| deps_list, fp=opts.output_path if opts.output_path else sys.stdout | |
| ) |