Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mike Frysinger | 0a647fc | 2012-08-06 14:36:05 -0400 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Program to run emerge in parallel, for significant speedup. |
| 7 | |
| 8 | Usage: |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 9 | ./parallel_emerge [--board=BOARD] [--workon=PKGS] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 10 | [--force-remote-binary=PKGS] [emerge args] package |
| 11 | |
David James | 78b6cd9 | 2012-04-02 21:36:12 -0700 | [diff] [blame] | 12 | This script runs multiple emerge processes in parallel, using appropriate |
| 13 | Portage APIs. It is faster than standard emerge because it has a |
| 14 | multiprocess model instead of an asynchronous model. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 15 | """ |
| 16 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 17 | from __future__ import print_function |
| 18 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 19 | import codecs |
| 20 | import copy |
| 21 | import errno |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 22 | import gc |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 23 | import heapq |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 24 | import multiprocessing |
| 25 | import os |
Mike Frysinger | 1ae2809 | 2013-10-17 17:17:22 -0400 | [diff] [blame] | 26 | try: |
| 27 | import Queue |
| 28 | except ImportError: |
| 29 | # Python-3 renamed to "queue". We still use Queue to avoid collisions |
| 30 | # with naming variables as "queue". Maybe we'll transition at some point. |
| 31 | # pylint: disable=F0401 |
| 32 | import queue as Queue |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 33 | import signal |
Bertrand SIMONNET | 19d789e | 2014-12-09 13:36:31 -0800 | [diff] [blame] | 34 | import subprocess |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 35 | import sys |
| 36 | import tempfile |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 37 | import threading |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 38 | import time |
| 39 | import traceback |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 40 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 41 | from chromite.lib import cros_build_lib |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 42 | from chromite.lib import cros_event |
Gregory Meinke | 5c50bf9 | 2018-04-30 10:51:40 -0600 | [diff] [blame^] | 43 | from chromite.lib import osutils |
Chris Ching | b8eba81 | 2017-06-22 09:54:48 -0600 | [diff] [blame] | 44 | from chromite.lib import portage_util |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 45 | from chromite.lib import process_util |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 46 | from chromite.lib import proctitle |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 47 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 48 | # If PORTAGE_USERNAME isn't specified, scrape it from the $HOME variable. On |
| 49 | # Chromium OS, the default "portage" user doesn't have the necessary |
| 50 | # permissions. It'd be easier if we could default to $USERNAME, but $USERNAME |
| 51 | # is "root" here because we get called through sudo. |
| 52 | # |
| 53 | # We need to set this before importing any portage modules, because portage |
| 54 | # looks up "PORTAGE_USERNAME" at import time. |
| 55 | # |
| 56 | # NOTE: .bashrc sets PORTAGE_USERNAME = $USERNAME, so most people won't |
| 57 | # encounter this case unless they have an old chroot or blow away the |
| 58 | # environment by running sudo without the -E specifier. |
| 59 | if "PORTAGE_USERNAME" not in os.environ: |
| 60 | homedir = os.environ.get("HOME") |
| 61 | if homedir: |
| 62 | os.environ["PORTAGE_USERNAME"] = os.path.basename(homedir) |
| 63 | |
Bertrand SIMONNET | 19d789e | 2014-12-09 13:36:31 -0800 | [diff] [blame] | 64 | # Wrap Popen with a lock to ensure no two Popen are executed simultaneously in |
| 65 | # the same process. |
| 66 | # Two Popen call at the same time might be the cause for crbug.com/433482. |
| 67 | _popen_lock = threading.Lock() |
| 68 | _old_popen = subprocess.Popen |
| 69 | |
| 70 | def _LockedPopen(*args, **kwargs): |
| 71 | with _popen_lock: |
| 72 | return _old_popen(*args, **kwargs) |
| 73 | |
| 74 | subprocess.Popen = _LockedPopen |
| 75 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 76 | # Portage doesn't expose dependency trees in its public API, so we have to |
| 77 | # make use of some private APIs here. These modules are found under |
| 78 | # /usr/lib/portage/pym/. |
| 79 | # |
| 80 | # TODO(davidjames): Update Portage to expose public APIs for these features. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 81 | # pylint: disable=F0401 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 82 | from _emerge.actions import adjust_configs |
| 83 | from _emerge.actions import load_emerge_config |
| 84 | from _emerge.create_depgraph_params import create_depgraph_params |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 85 | from _emerge.depgraph import backtrack_depgraph |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 86 | from _emerge.main import emerge_main |
| 87 | from _emerge.main import parse_opts |
| 88 | from _emerge.Package import Package |
Bertrand SIMONNET | a15b507 | 2014-10-23 15:27:52 -0700 | [diff] [blame] | 89 | from _emerge.post_emerge import clean_logs |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 90 | from _emerge.Scheduler import Scheduler |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 91 | from _emerge.stdout_spinner import stdout_spinner |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 92 | from portage._global_updates import _global_updates |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 93 | import portage |
| 94 | import portage.debug |
Don Garrett | f8bf784 | 2014-03-20 17:03:42 -0700 | [diff] [blame] | 95 | # pylint: enable=F0401 |
Mike Frysinger | 91d7da9 | 2013-02-19 15:53:46 -0500 | [diff] [blame] | 96 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 97 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 98 | def Usage(): |
| 99 | """Print usage.""" |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 100 | print("Usage:") |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 101 | print(" ./parallel_emerge [--board=BOARD] [--workon=PKGS] [--rebuild]") |
| 102 | print(" [--eventlogfile=FILE] [emerge args] package") |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 103 | print() |
| 104 | print("Packages specified as workon packages are always built from source.") |
| 105 | print() |
| 106 | print("The --workon argument is mainly useful when you want to build and") |
| 107 | print("install packages that you are working on unconditionally, but do not") |
| 108 | print("to have to rev the package to indicate you want to build it from") |
| 109 | print("source. The build_packages script will automatically supply the") |
| 110 | print("workon argument to emerge, ensuring that packages selected using") |
| 111 | print("cros-workon are rebuilt.") |
| 112 | print() |
| 113 | print("The --rebuild option rebuilds packages whenever their dependencies") |
| 114 | print("are changed. This ensures that your build is correct.") |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 115 | print() |
| 116 | print("The --eventlogfile writes events to the given file. File is") |
| 117 | print("is overwritten if it exists.") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 118 | |
| 119 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 120 | # Global start time |
| 121 | GLOBAL_START = time.time() |
| 122 | |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 123 | # Whether process has been killed by a signal. |
| 124 | KILLED = multiprocessing.Event() |
| 125 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 126 | |
| 127 | class EmergeData(object): |
| 128 | """This simple struct holds various emerge variables. |
| 129 | |
| 130 | This struct helps us easily pass emerge variables around as a unit. |
| 131 | These variables are used for calculating dependencies and installing |
| 132 | packages. |
| 133 | """ |
| 134 | |
David James | bf1e344 | 2011-05-28 07:44:20 -0700 | [diff] [blame] | 135 | __slots__ = ["action", "cmdline_packages", "depgraph", "favorites", |
| 136 | "mtimedb", "opts", "root_config", "scheduler_graph", |
| 137 | "settings", "spinner", "trees"] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 138 | |
| 139 | def __init__(self): |
| 140 | # The action the user requested. If the user is installing packages, this |
| 141 | # is None. If the user is doing anything other than installing packages, |
| 142 | # this will contain the action name, which will map exactly to the |
| 143 | # long-form name of the associated emerge option. |
| 144 | # |
| 145 | # Example: If you call parallel_emerge --unmerge package, the action name |
| 146 | # will be "unmerge" |
| 147 | self.action = None |
| 148 | |
| 149 | # The list of packages the user passed on the command-line. |
| 150 | self.cmdline_packages = None |
| 151 | |
| 152 | # The emerge dependency graph. It'll contain all the packages involved in |
| 153 | # this merge, along with their versions. |
| 154 | self.depgraph = None |
| 155 | |
David James | bf1e344 | 2011-05-28 07:44:20 -0700 | [diff] [blame] | 156 | # The list of candidates to add to the world file. |
| 157 | self.favorites = None |
| 158 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 159 | # A dict of the options passed to emerge. This dict has been cleaned up |
| 160 | # a bit by parse_opts, so that it's a bit easier for the emerge code to |
| 161 | # look at the options. |
| 162 | # |
| 163 | # Emerge takes a few shortcuts in its cleanup process to make parsing of |
| 164 | # the options dict easier. For example, if you pass in "--usepkg=n", the |
| 165 | # "--usepkg" flag is just left out of the dictionary altogether. Because |
| 166 | # --usepkg=n is the default, this makes parsing easier, because emerge |
| 167 | # can just assume that if "--usepkg" is in the dictionary, it's enabled. |
| 168 | # |
| 169 | # These cleanup processes aren't applied to all options. For example, the |
| 170 | # --with-bdeps flag is passed in as-is. For a full list of the cleanups |
| 171 | # applied by emerge, see the parse_opts function in the _emerge.main |
| 172 | # package. |
| 173 | self.opts = None |
| 174 | |
| 175 | # A dictionary used by portage to maintain global state. This state is |
| 176 | # loaded from disk when portage starts up, and saved to disk whenever we |
| 177 | # call mtimedb.commit(). |
| 178 | # |
| 179 | # This database contains information about global updates (i.e., what |
| 180 | # version of portage we have) and what we're currently doing. Portage |
| 181 | # saves what it is currently doing in this database so that it can be |
| 182 | # resumed when you call it with the --resume option. |
| 183 | # |
| 184 | # parallel_emerge does not save what it is currently doing in the mtimedb, |
| 185 | # so we do not support the --resume option. |
| 186 | self.mtimedb = None |
| 187 | |
| 188 | # The portage configuration for our current root. This contains the portage |
| 189 | # settings (see below) and the three portage trees for our current root. |
| 190 | # (The three portage trees are explained below, in the documentation for |
| 191 | # the "trees" member.) |
| 192 | self.root_config = None |
| 193 | |
| 194 | # The scheduler graph is used by emerge to calculate what packages to |
| 195 | # install. We don't actually install any deps, so this isn't really used, |
| 196 | # but we pass it in to the Scheduler object anyway. |
| 197 | self.scheduler_graph = None |
| 198 | |
| 199 | # Portage settings for our current session. Most of these settings are set |
| 200 | # in make.conf inside our current install root. |
| 201 | self.settings = None |
| 202 | |
| 203 | # The spinner, which spews stuff to stdout to indicate that portage is |
| 204 | # doing something. We maintain our own spinner, so we set the portage |
| 205 | # spinner to "silent" mode. |
| 206 | self.spinner = None |
| 207 | |
| 208 | # The portage trees. There are separate portage trees for each root. To get |
| 209 | # the portage tree for the current root, you can look in self.trees[root], |
| 210 | # where root = self.settings["ROOT"]. |
| 211 | # |
| 212 | # In each root, there are three trees: vartree, porttree, and bintree. |
| 213 | # - vartree: A database of the currently-installed packages. |
| 214 | # - porttree: A database of ebuilds, that can be used to build packages. |
| 215 | # - bintree: A database of binary packages. |
| 216 | self.trees = None |
| 217 | |
| 218 | |
| 219 | class DepGraphGenerator(object): |
| 220 | """Grab dependency information about packages from portage. |
| 221 | |
| 222 | Typical usage: |
| 223 | deps = DepGraphGenerator() |
| 224 | deps.Initialize(sys.argv[1:]) |
| 225 | deps_tree, deps_info = deps.GenDependencyTree() |
| 226 | deps_graph = deps.GenDependencyGraph(deps_tree, deps_info) |
| 227 | deps.PrintTree(deps_tree) |
| 228 | PrintDepsMap(deps_graph) |
| 229 | """ |
| 230 | |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 231 | __slots__ = ["board", "emerge", "package_db", "show_output", "sysroot", |
Gregory Meinke | bec9b44 | 2018-04-17 12:01:19 -0600 | [diff] [blame] | 232 | "unpack_only", "max_retries", "install_plan_filename"] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 233 | |
| 234 | def __init__(self): |
| 235 | self.board = None |
| 236 | self.emerge = EmergeData() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 237 | self.package_db = {} |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 238 | self.show_output = False |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 239 | self.sysroot = None |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 240 | self.unpack_only = False |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 241 | self.max_retries = 1 |
Gregory Meinke | bec9b44 | 2018-04-17 12:01:19 -0600 | [diff] [blame] | 242 | self.install_plan_filename = None |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 243 | |
| 244 | def ParseParallelEmergeArgs(self, argv): |
| 245 | """Read the parallel emerge arguments from the command-line. |
| 246 | |
| 247 | We need to be compatible with emerge arg format. We scrape arguments that |
| 248 | are specific to parallel_emerge, and pass through the rest directly to |
| 249 | emerge. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 250 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 251 | Args: |
| 252 | argv: arguments list |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 253 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 254 | Returns: |
| 255 | Arguments that don't belong to parallel_emerge |
| 256 | """ |
| 257 | emerge_args = [] |
| 258 | for arg in argv: |
| 259 | # Specifically match arguments that are specific to parallel_emerge, and |
| 260 | # pass through the rest. |
| 261 | if arg.startswith("--board="): |
| 262 | self.board = arg.replace("--board=", "") |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 263 | elif arg.startswith("--sysroot="): |
| 264 | self.sysroot = arg.replace("--sysroot=", "") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 265 | elif arg.startswith("--workon="): |
| 266 | workon_str = arg.replace("--workon=", "") |
David James | 7a1ea4b | 2011-10-13 15:06:41 -0700 | [diff] [blame] | 267 | emerge_args.append("--reinstall-atoms=%s" % workon_str) |
| 268 | emerge_args.append("--usepkg-exclude=%s" % workon_str) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 269 | elif arg.startswith("--force-remote-binary="): |
| 270 | force_remote_binary = arg.replace("--force-remote-binary=", "") |
David James | 7a1ea4b | 2011-10-13 15:06:41 -0700 | [diff] [blame] | 271 | emerge_args.append("--useoldpkg-atoms=%s" % force_remote_binary) |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 272 | elif arg.startswith("--retries="): |
| 273 | self.max_retries = int(arg.replace("--retries=", "")) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 274 | elif arg == "--show-output": |
| 275 | self.show_output = True |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 276 | elif arg == "--rebuild": |
David James | 7a1ea4b | 2011-10-13 15:06:41 -0700 | [diff] [blame] | 277 | emerge_args.append("--rebuild-if-unbuilt") |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 278 | elif arg == "--unpackonly": |
| 279 | emerge_args.append("--fetchonly") |
| 280 | self.unpack_only = True |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 281 | elif arg.startswith("--eventlogfile="): |
| 282 | log_file_name = arg.replace("--eventlogfile=", "") |
Chris Ching | 4a2ebd6 | 2017-04-26 16:30:05 -0600 | [diff] [blame] | 283 | event_logger = cros_event.getEventFileLogger(log_file_name) |
| 284 | event_logger.setKind('ParallelEmerge') |
| 285 | cros_event.setEventLogger(event_logger) |
Gregory Meinke | bec9b44 | 2018-04-17 12:01:19 -0600 | [diff] [blame] | 286 | elif arg.startswith("--install-plan-filename"): |
| 287 | # No emerge equivalent, used to calculate the list of packages |
| 288 | # that changed and we will need to calculate reverse dependencies. |
| 289 | self.install_plan_filename = arg.replace("--install-plan-filename=", "") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 290 | else: |
| 291 | # Not one of our options, so pass through to emerge. |
| 292 | emerge_args.append(arg) |
| 293 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 294 | # These packages take a really long time to build, so, for expediency, we |
| 295 | # are blacklisting them from automatic rebuilds because one of their |
| 296 | # dependencies needs to be recompiled. |
Mike Frysinger | 5c2a905 | 2014-04-15 15:52:04 -0400 | [diff] [blame] | 297 | for pkg in ("chromeos-base/chromeos-chrome",): |
David James | 7a1ea4b | 2011-10-13 15:06:41 -0700 | [diff] [blame] | 298 | emerge_args.append("--rebuild-exclude=%s" % pkg) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 299 | |
| 300 | return emerge_args |
| 301 | |
| 302 | def Initialize(self, args): |
| 303 | """Initializer. Parses arguments and sets up portage state.""" |
| 304 | |
| 305 | # Parse and strip out args that are just intended for parallel_emerge. |
| 306 | emerge_args = self.ParseParallelEmergeArgs(args) |
| 307 | |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 308 | if self.sysroot and self.board: |
| 309 | cros_build_lib.Die("--sysroot and --board are incompatible.") |
| 310 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 311 | # Setup various environment variables based on our current board. These |
| 312 | # variables are normally setup inside emerge-${BOARD}, but since we don't |
| 313 | # call that script, we have to set it up here. These variables serve to |
| 314 | # point our tools at /build/BOARD and to setup cross compiles to the |
| 315 | # appropriate board as configured in toolchain.conf. |
| 316 | if self.board: |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 317 | self.sysroot = os.environ.get('SYSROOT', |
| 318 | cros_build_lib.GetSysroot(self.board)) |
| 319 | |
| 320 | if self.sysroot: |
| 321 | os.environ["PORTAGE_CONFIGROOT"] = self.sysroot |
| 322 | os.environ["SYSROOT"] = self.sysroot |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 323 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 324 | # Turn off interactive delays |
| 325 | os.environ["EBEEP_IGNORE"] = "1" |
| 326 | os.environ["EPAUSE_IGNORE"] = "1" |
Mike Frysinger | 0a647fc | 2012-08-06 14:36:05 -0400 | [diff] [blame] | 327 | os.environ["CLEAN_DELAY"] = "0" |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 328 | |
| 329 | # Parse the emerge options. |
David James | ea3ca33 | 2011-05-26 11:48:29 -0700 | [diff] [blame] | 330 | action, opts, cmdline_packages = parse_opts(emerge_args, silent=True) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 331 | |
| 332 | # Set environment variables based on options. Portage normally sets these |
| 333 | # environment variables in emerge_main, but we can't use that function, |
| 334 | # because it also does a bunch of other stuff that we don't want. |
| 335 | # TODO(davidjames): Patch portage to move this logic into a function we can |
| 336 | # reuse here. |
| 337 | if "--debug" in opts: |
| 338 | os.environ["PORTAGE_DEBUG"] = "1" |
| 339 | if "--config-root" in opts: |
| 340 | os.environ["PORTAGE_CONFIGROOT"] = opts["--config-root"] |
| 341 | if "--root" in opts: |
| 342 | os.environ["ROOT"] = opts["--root"] |
| 343 | if "--accept-properties" in opts: |
| 344 | os.environ["ACCEPT_PROPERTIES"] = opts["--accept-properties"] |
| 345 | |
David James | 88d780c | 2014-02-05 13:03:29 -0800 | [diff] [blame] | 346 | # If we're installing packages to the board, we can disable vardb locks. |
| 347 | # This is safe because we only run up to one instance of parallel_emerge in |
| 348 | # parallel. |
| 349 | # TODO(davidjames): Enable this for the host too. |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 350 | if self.sysroot: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 351 | os.environ.setdefault("PORTAGE_LOCKS", "false") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 352 | |
| 353 | # Now that we've setup the necessary environment variables, we can load the |
| 354 | # emerge config from disk. |
Gilad Arnold | 9475876 | 2015-05-22 12:23:23 -0700 | [diff] [blame] | 355 | # pylint: disable=unpacking-non-sequence |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 356 | settings, trees, mtimedb = load_emerge_config() |
| 357 | |
David James | ea3ca33 | 2011-05-26 11:48:29 -0700 | [diff] [blame] | 358 | # Add in EMERGE_DEFAULT_OPTS, if specified. |
| 359 | tmpcmdline = [] |
| 360 | if "--ignore-default-opts" not in opts: |
| 361 | tmpcmdline.extend(settings["EMERGE_DEFAULT_OPTS"].split()) |
| 362 | tmpcmdline.extend(emerge_args) |
| 363 | action, opts, cmdline_packages = parse_opts(tmpcmdline) |
| 364 | |
| 365 | # If we're installing to the board, we want the --root-deps option so that |
| 366 | # portage will install the build dependencies to that location as well. |
Bertrand SIMONNET | 0625e1a | 2015-04-07 11:41:16 -0700 | [diff] [blame] | 367 | if self.sysroot: |
David James | ea3ca33 | 2011-05-26 11:48:29 -0700 | [diff] [blame] | 368 | opts.setdefault("--root-deps", True) |
| 369 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 370 | # Check whether our portage tree is out of date. Typically, this happens |
| 371 | # when you're setting up a new portage tree, such as in setup_board and |
| 372 | # make_chroot. In that case, portage applies a bunch of global updates |
| 373 | # here. Once the updates are finished, we need to commit any changes |
| 374 | # that the global update made to our mtimedb, and reload the config. |
| 375 | # |
| 376 | # Portage normally handles this logic in emerge_main, but again, we can't |
| 377 | # use that function here. |
| 378 | if _global_updates(trees, mtimedb["updates"]): |
| 379 | mtimedb.commit() |
Gilad Arnold | 9475876 | 2015-05-22 12:23:23 -0700 | [diff] [blame] | 380 | # pylint: disable=unpacking-non-sequence |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 381 | settings, trees, mtimedb = load_emerge_config(trees=trees) |
| 382 | |
| 383 | # Setup implied options. Portage normally handles this logic in |
| 384 | # emerge_main. |
| 385 | if "--buildpkgonly" in opts or "buildpkg" in settings.features: |
| 386 | opts.setdefault("--buildpkg", True) |
| 387 | if "--getbinpkgonly" in opts: |
| 388 | opts.setdefault("--usepkgonly", True) |
| 389 | opts.setdefault("--getbinpkg", True) |
| 390 | if "getbinpkg" in settings.features: |
| 391 | # Per emerge_main, FEATURES=getbinpkg overrides --getbinpkg=n |
| 392 | opts["--getbinpkg"] = True |
| 393 | if "--getbinpkg" in opts or "--usepkgonly" in opts: |
| 394 | opts.setdefault("--usepkg", True) |
| 395 | if "--fetch-all-uri" in opts: |
| 396 | opts.setdefault("--fetchonly", True) |
| 397 | if "--skipfirst" in opts: |
| 398 | opts.setdefault("--resume", True) |
| 399 | if "--buildpkgonly" in opts: |
| 400 | # --buildpkgonly will not merge anything, so it overrides all binary |
| 401 | # package options. |
| 402 | for opt in ("--getbinpkg", "--getbinpkgonly", |
| 403 | "--usepkg", "--usepkgonly"): |
| 404 | opts.pop(opt, None) |
| 405 | if (settings.get("PORTAGE_DEBUG", "") == "1" and |
| 406 | "python-trace" in settings.features): |
| 407 | portage.debug.set_trace(True) |
| 408 | |
| 409 | # Complain about unsupported options |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 410 | for opt in ("--ask", "--ask-enter-invalid", "--resume", "--skipfirst"): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 411 | if opt in opts: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 412 | print("%s is not supported by parallel_emerge" % opt) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 413 | sys.exit(1) |
| 414 | |
| 415 | # Make emerge specific adjustments to the config (e.g. colors!) |
| 416 | adjust_configs(opts, trees) |
| 417 | |
| 418 | # Save our configuration so far in the emerge object |
| 419 | emerge = self.emerge |
| 420 | emerge.action, emerge.opts = action, opts |
| 421 | emerge.settings, emerge.trees, emerge.mtimedb = settings, trees, mtimedb |
| 422 | emerge.cmdline_packages = cmdline_packages |
| 423 | root = settings["ROOT"] |
| 424 | emerge.root_config = trees[root]["root_config"] |
| 425 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 426 | if "--usepkg" in opts: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 427 | emerge.trees[root]["bintree"].populate("--getbinpkg" in opts) |
| 428 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 429 | def CreateDepgraph(self, emerge, packages): |
| 430 | """Create an emerge depgraph object.""" |
| 431 | # Setup emerge options. |
| 432 | emerge_opts = emerge.opts.copy() |
| 433 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 434 | # Ask portage to build a dependency graph. with the options we specified |
| 435 | # above. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 436 | params = create_depgraph_params(emerge_opts, emerge.action) |
David James | bf1e344 | 2011-05-28 07:44:20 -0700 | [diff] [blame] | 437 | success, depgraph, favorites = backtrack_depgraph( |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 438 | emerge.settings, emerge.trees, emerge_opts, params, emerge.action, |
| 439 | packages, emerge.spinner) |
| 440 | emerge.depgraph = depgraph |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 441 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 442 | # Is it impossible to honor the user's request? Bail! |
| 443 | if not success: |
| 444 | depgraph.display_problems() |
| 445 | sys.exit(1) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 446 | |
| 447 | emerge.depgraph = depgraph |
David James | bf1e344 | 2011-05-28 07:44:20 -0700 | [diff] [blame] | 448 | emerge.favorites = favorites |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 449 | |
David James | deebd69 | 2011-05-09 17:02:52 -0700 | [diff] [blame] | 450 | # Prime and flush emerge caches. |
| 451 | root = emerge.settings["ROOT"] |
| 452 | vardb = emerge.trees[root]["vartree"].dbapi |
David James | 0bdc5de | 2011-05-12 16:22:26 -0700 | [diff] [blame] | 453 | if "--pretend" not in emerge.opts: |
| 454 | vardb.counter_tick() |
David James | deebd69 | 2011-05-09 17:02:52 -0700 | [diff] [blame] | 455 | vardb.flush_cache() |
| 456 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 457 | def GenDependencyTree(self): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 458 | """Get dependency tree info from emerge. |
| 459 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 460 | Returns: |
| 461 | Dependency tree |
| 462 | """ |
| 463 | start = time.time() |
| 464 | |
| 465 | emerge = self.emerge |
| 466 | |
| 467 | # Create a list of packages to merge |
| 468 | packages = set(emerge.cmdline_packages[:]) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 469 | |
| 470 | # Tell emerge to be quiet. We print plenty of info ourselves so we don't |
| 471 | # need any extra output from portage. |
| 472 | portage.util.noiselimit = -1 |
| 473 | |
| 474 | # My favorite feature: The silent spinner. It doesn't spin. Ever. |
| 475 | # I'd disable the colors by default too, but they look kind of cool. |
| 476 | emerge.spinner = stdout_spinner() |
| 477 | emerge.spinner.update = emerge.spinner.update_quiet |
| 478 | |
| 479 | if "--quiet" not in emerge.opts: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 480 | print("Calculating deps...") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 481 | |
Chris Ching | 4a2ebd6 | 2017-04-26 16:30:05 -0600 | [diff] [blame] | 482 | with cros_event.newEvent(task_name="GenerateDepTree"): |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 483 | self.CreateDepgraph(emerge, packages) |
| 484 | depgraph = emerge.depgraph |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 485 | |
| 486 | # Build our own tree from the emerge digraph. |
| 487 | deps_tree = {} |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 488 | # pylint: disable=W0212 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 489 | digraph = depgraph._dynamic_config.digraph |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 490 | root = emerge.settings["ROOT"] |
Bertrand SIMONNET | a15b507 | 2014-10-23 15:27:52 -0700 | [diff] [blame] | 491 | final_db = depgraph._dynamic_config._filtered_trees[root]['graph_db'] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 492 | for node, node_deps in digraph.nodes.items(): |
| 493 | # Calculate dependency packages that need to be installed first. Each |
| 494 | # child on the digraph is a dependency. The "operation" field specifies |
| 495 | # what we're doing (e.g. merge, uninstall, etc.). The "priorities" array |
| 496 | # contains the type of dependency (e.g. build, runtime, runtime_post, |
| 497 | # etc.) |
| 498 | # |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 499 | # Portage refers to the identifiers for packages as a CPV. This acronym |
| 500 | # stands for Component/Path/Version. |
| 501 | # |
| 502 | # Here's an example CPV: chromeos-base/power_manager-0.0.1-r1 |
| 503 | # Split up, this CPV would be: |
| 504 | # C -- Component: chromeos-base |
| 505 | # P -- Path: power_manager |
| 506 | # V -- Version: 0.0.1-r1 |
| 507 | # |
| 508 | # We just refer to CPVs as packages here because it's easier. |
| 509 | deps = {} |
| 510 | for child, priorities in node_deps[0].items(): |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 511 | if isinstance(child, Package) and child.root == root: |
| 512 | cpv = str(child.cpv) |
| 513 | action = str(child.operation) |
| 514 | |
| 515 | # If we're uninstalling a package, check whether Portage is |
| 516 | # installing a replacement. If so, just depend on the installation |
| 517 | # of the new package, because the old package will automatically |
| 518 | # be uninstalled at that time. |
| 519 | if action == "uninstall": |
| 520 | for pkg in final_db.match_pkgs(child.slot_atom): |
| 521 | cpv = str(pkg.cpv) |
| 522 | action = "merge" |
| 523 | break |
| 524 | |
| 525 | deps[cpv] = dict(action=action, |
| 526 | deptypes=[str(x) for x in priorities], |
| 527 | deps={}) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 528 | |
| 529 | # We've built our list of deps, so we can add our package to the tree. |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 530 | if isinstance(node, Package) and node.root == root: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 531 | deps_tree[str(node.cpv)] = dict(action=str(node.operation), |
| 532 | deps=deps) |
| 533 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 534 | # Ask portage for its install plan, so that we can only throw out |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 535 | # dependencies that portage throws out. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 536 | deps_info = {} |
| 537 | for pkg in depgraph.altlist(): |
| 538 | if isinstance(pkg, Package): |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 539 | assert pkg.root == root |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 540 | self.package_db[pkg.cpv] = pkg |
| 541 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 542 | # Save off info about the package |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 543 | deps_info[str(pkg.cpv)] = {"idx": len(deps_info)} |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 544 | |
| 545 | seconds = time.time() - start |
| 546 | if "--quiet" not in emerge.opts: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 547 | print("Deps calculated in %dm%.1fs" % (seconds / 60, seconds % 60)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 548 | |
Gregory Meinke | bec9b44 | 2018-04-17 12:01:19 -0600 | [diff] [blame] | 549 | # Calculate the install plan packages and append to temp file. They will be |
| 550 | # used to calculate all the reverse dependencies on these change packages. |
| 551 | if self.install_plan_filename: |
Gregory Meinke | 5c50bf9 | 2018-04-30 10:51:40 -0600 | [diff] [blame^] | 552 | # Always write the file even if nothing to do, scripts expect existence. |
| 553 | output = '\n'.join(deps_info) |
| 554 | if len(output) > 0: |
| 555 | # add a trailing newline only if the output is not empty. |
| 556 | output += '\n' |
| 557 | osutils.WriteFile(self.install_plan_filename, |
| 558 | output, |
| 559 | mode='a') |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 560 | return deps_tree, deps_info |
| 561 | |
| 562 | def PrintTree(self, deps, depth=""): |
| 563 | """Print the deps we have seen in the emerge output. |
| 564 | |
| 565 | Args: |
Mike Frysinger | 6f3c48e | 2015-05-06 02:38:51 -0400 | [diff] [blame] | 566 | deps: Dependency tree structure. |
| 567 | depth: Allows printing the tree recursively, with indentation. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 568 | """ |
| 569 | for entry in sorted(deps): |
| 570 | action = deps[entry]["action"] |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 571 | print("%s %s (%s)" % (depth, entry, action)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 572 | self.PrintTree(deps[entry]["deps"], depth=depth + " ") |
| 573 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 574 | def GenDependencyGraph(self, deps_tree, deps_info): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 575 | """Generate a doubly linked dependency graph. |
| 576 | |
| 577 | Args: |
| 578 | deps_tree: Dependency tree structure. |
| 579 | deps_info: More details on the dependencies. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 580 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 581 | Returns: |
| 582 | Deps graph in the form of a dict of packages, with each package |
| 583 | specifying a "needs" list and "provides" list. |
| 584 | """ |
| 585 | emerge = self.emerge |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 586 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 587 | # deps_map is the actual dependency graph. |
| 588 | # |
| 589 | # Each package specifies a "needs" list and a "provides" list. The "needs" |
| 590 | # list indicates which packages we depend on. The "provides" list |
| 591 | # indicates the reverse dependencies -- what packages need us. |
| 592 | # |
| 593 | # We also provide some other information in the dependency graph: |
| 594 | # - action: What we're planning on doing with this package. Generally, |
| 595 | # "merge", "nomerge", or "uninstall" |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 596 | deps_map = {} |
| 597 | |
| 598 | def ReverseTree(packages): |
| 599 | """Convert tree to digraph. |
| 600 | |
| 601 | Take the tree of package -> requirements and reverse it to a digraph of |
| 602 | buildable packages -> packages they unblock. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 603 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 604 | Args: |
| 605 | packages: Tree(s) of dependencies. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 606 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 607 | Returns: |
| 608 | Unsanitized digraph. |
| 609 | """ |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 610 | binpkg_phases = set(["setup", "preinst", "postinst"]) |
David James | e5e1c0a | 2014-09-29 17:19:41 -0700 | [diff] [blame] | 611 | needed_dep_types = set(["blocker", "buildtime", "buildtime_slot_op", |
| 612 | "runtime", "runtime_slot_op"]) |
Benjamin Gordon | 670b697 | 2017-08-29 13:43:56 -0600 | [diff] [blame] | 613 | ignored_dep_types = set(["ignored", "runtime_post", "soft"]) |
| 614 | |
| 615 | # There's a bug in the Portage library where it always returns 'optional' |
| 616 | # and never 'buildtime' for the digraph while --usepkg is enabled; even |
| 617 | # when the package is being rebuilt. To work around this, we treat |
| 618 | # 'optional' as needed when we are using --usepkg. See crbug.com/756240 . |
| 619 | if "--usepkg" in self.emerge.opts: |
| 620 | needed_dep_types.add("optional") |
| 621 | else: |
| 622 | ignored_dep_types.add("optional") |
| 623 | |
David James | e5e1c0a | 2014-09-29 17:19:41 -0700 | [diff] [blame] | 624 | all_dep_types = ignored_dep_types | needed_dep_types |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 625 | for pkg in packages: |
| 626 | |
| 627 | # Create an entry for the package |
| 628 | action = packages[pkg]["action"] |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 629 | default_pkg = {"needs": {}, "provides": set(), "action": action, |
| 630 | "nodeps": False, "binary": False} |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 631 | this_pkg = deps_map.setdefault(pkg, default_pkg) |
| 632 | |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 633 | if pkg in deps_info: |
| 634 | this_pkg["idx"] = deps_info[pkg]["idx"] |
| 635 | |
| 636 | # If a package doesn't have any defined phases that might use the |
| 637 | # dependent packages (i.e. pkg_setup, pkg_preinst, or pkg_postinst), |
| 638 | # we can install this package before its deps are ready. |
| 639 | emerge_pkg = self.package_db.get(pkg) |
| 640 | if emerge_pkg and emerge_pkg.type_name == "binary": |
| 641 | this_pkg["binary"] = True |
Mike Frysinger | 66652ec | 2014-04-24 11:42:25 -0400 | [diff] [blame] | 642 | defined_phases = emerge_pkg.defined_phases |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 643 | defined_binpkg_phases = binpkg_phases.intersection(defined_phases) |
| 644 | if not defined_binpkg_phases: |
| 645 | this_pkg["nodeps"] = True |
| 646 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 647 | # Create entries for dependencies of this package first. |
| 648 | ReverseTree(packages[pkg]["deps"]) |
| 649 | |
| 650 | # Add dependencies to this package. |
| 651 | for dep, dep_item in packages[pkg]["deps"].iteritems(): |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 652 | # We only need to enforce strict ordering of dependencies if the |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 653 | # dependency is a blocker, or is a buildtime or runtime dependency. |
| 654 | # (I.e., ignored, optional, and runtime_post dependencies don't |
| 655 | # depend on ordering.) |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 656 | dep_types = dep_item["deptypes"] |
| 657 | if needed_dep_types.intersection(dep_types): |
| 658 | deps_map[dep]["provides"].add(pkg) |
| 659 | this_pkg["needs"][dep] = "/".join(dep_types) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 660 | |
David James | e5e1c0a | 2014-09-29 17:19:41 -0700 | [diff] [blame] | 661 | # Verify we processed all appropriate dependency types. |
| 662 | unknown_dep_types = set(dep_types) - all_dep_types |
| 663 | if unknown_dep_types: |
| 664 | print("Unknown dependency types found:") |
| 665 | print(" %s -> %s (%s)" % (pkg, dep, "/".join(unknown_dep_types))) |
| 666 | sys.exit(1) |
| 667 | |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 668 | # If there's a blocker, Portage may need to move files from one |
| 669 | # package to another, which requires editing the CONTENTS files of |
| 670 | # both packages. To avoid race conditions while editing this file, |
| 671 | # the two packages must not be installed in parallel, so we can't |
| 672 | # safely ignore dependencies. See http://crosbug.com/19328 |
| 673 | if "blocker" in dep_types: |
| 674 | this_pkg["nodeps"] = False |
| 675 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 676 | def FindCycles(): |
| 677 | """Find cycles in the dependency tree. |
| 678 | |
| 679 | Returns: |
| 680 | A dict mapping cyclic packages to a dict of the deps that cause |
| 681 | cycles. For each dep that causes cycles, it returns an example |
| 682 | traversal of the graph that shows the cycle. |
| 683 | """ |
| 684 | |
| 685 | def FindCyclesAtNode(pkg, cycles, unresolved, resolved): |
| 686 | """Find cycles in cyclic dependencies starting at specified package. |
| 687 | |
| 688 | Args: |
| 689 | pkg: Package identifier. |
| 690 | cycles: A dict mapping cyclic packages to a dict of the deps that |
| 691 | cause cycles. For each dep that causes cycles, it returns an |
| 692 | example traversal of the graph that shows the cycle. |
| 693 | unresolved: Nodes that have been visited but are not fully processed. |
| 694 | resolved: Nodes that have been visited and are fully processed. |
| 695 | """ |
| 696 | pkg_cycles = cycles.get(pkg) |
| 697 | if pkg in resolved and not pkg_cycles: |
| 698 | # If we already looked at this package, and found no cyclic |
| 699 | # dependencies, we can stop now. |
| 700 | return |
| 701 | unresolved.append(pkg) |
| 702 | for dep in deps_map[pkg]["needs"]: |
| 703 | if dep in unresolved: |
| 704 | idx = unresolved.index(dep) |
| 705 | mycycle = unresolved[idx:] + [dep] |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 706 | for i in xrange(len(mycycle) - 1): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 707 | pkg1, pkg2 = mycycle[i], mycycle[i+1] |
| 708 | cycles.setdefault(pkg1, {}).setdefault(pkg2, mycycle) |
| 709 | elif not pkg_cycles or dep not in pkg_cycles: |
| 710 | # Looks like we haven't seen this edge before. |
| 711 | FindCyclesAtNode(dep, cycles, unresolved, resolved) |
| 712 | unresolved.pop() |
| 713 | resolved.add(pkg) |
| 714 | |
| 715 | cycles, unresolved, resolved = {}, [], set() |
| 716 | for pkg in deps_map: |
| 717 | FindCyclesAtNode(pkg, cycles, unresolved, resolved) |
| 718 | return cycles |
| 719 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 720 | def RemoveUnusedPackages(): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 721 | """Remove installed packages, propagating dependencies.""" |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 722 | # Schedule packages that aren't on the install list for removal |
| 723 | rm_pkgs = set(deps_map.keys()) - set(deps_info.keys()) |
| 724 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 725 | # Remove the packages we don't want, simplifying the graph and making |
| 726 | # it easier for us to crack cycles. |
| 727 | for pkg in sorted(rm_pkgs): |
| 728 | this_pkg = deps_map[pkg] |
| 729 | needs = this_pkg["needs"] |
| 730 | provides = this_pkg["provides"] |
| 731 | for dep in needs: |
| 732 | dep_provides = deps_map[dep]["provides"] |
| 733 | dep_provides.update(provides) |
| 734 | dep_provides.discard(pkg) |
| 735 | dep_provides.discard(dep) |
| 736 | for target in provides: |
| 737 | target_needs = deps_map[target]["needs"] |
| 738 | target_needs.update(needs) |
| 739 | target_needs.pop(pkg, None) |
| 740 | target_needs.pop(target, None) |
| 741 | del deps_map[pkg] |
| 742 | |
| 743 | def PrintCycleBreak(basedep, dep, mycycle): |
| 744 | """Print details about a cycle that we are planning on breaking. |
| 745 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 746 | We are breaking a cycle where dep needs basedep. mycycle is an |
| 747 | example cycle which contains dep -> basedep. |
| 748 | """ |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 749 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 750 | needs = deps_map[dep]["needs"] |
| 751 | depinfo = needs.get(basedep, "deleted") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 752 | |
David James | 3f77880 | 2011-08-25 19:31:45 -0700 | [diff] [blame] | 753 | # It's OK to swap install order for blockers, as long as the two |
| 754 | # packages aren't installed in parallel. If there is a cycle, then |
| 755 | # we know the packages depend on each other already, so we can drop the |
| 756 | # blocker safely without printing a warning. |
| 757 | if depinfo == "blocker": |
| 758 | return |
| 759 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 760 | # Notify the user that we're breaking a cycle. |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 761 | print("Breaking %s -> %s (%s)" % (dep, basedep, depinfo)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 762 | |
| 763 | # Show cycle. |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 764 | for i in xrange(len(mycycle) - 1): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 765 | pkg1, pkg2 = mycycle[i], mycycle[i+1] |
| 766 | needs = deps_map[pkg1]["needs"] |
| 767 | depinfo = needs.get(pkg2, "deleted") |
| 768 | if pkg1 == dep and pkg2 == basedep: |
| 769 | depinfo = depinfo + ", deleting" |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 770 | print(" %s -> %s (%s)" % (pkg1, pkg2, depinfo)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 771 | |
| 772 | def SanitizeTree(): |
| 773 | """Remove circular dependencies. |
| 774 | |
| 775 | We prune all dependencies involved in cycles that go against the emerge |
| 776 | ordering. This has a nice property: we're guaranteed to merge |
| 777 | dependencies in the same order that portage does. |
| 778 | |
| 779 | Because we don't treat any dependencies as "soft" unless they're killed |
| 780 | by a cycle, we pay attention to a larger number of dependencies when |
| 781 | merging. This hurts performance a bit, but helps reliability. |
| 782 | """ |
| 783 | start = time.time() |
| 784 | cycles = FindCycles() |
| 785 | while cycles: |
| 786 | for dep, mycycles in cycles.iteritems(): |
| 787 | for basedep, mycycle in mycycles.iteritems(): |
| 788 | if deps_info[basedep]["idx"] >= deps_info[dep]["idx"]: |
Matt Tennant | 0879730 | 2011-10-17 16:18:45 -0700 | [diff] [blame] | 789 | if "--quiet" not in emerge.opts: |
| 790 | PrintCycleBreak(basedep, dep, mycycle) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 791 | del deps_map[dep]["needs"][basedep] |
| 792 | deps_map[basedep]["provides"].remove(dep) |
| 793 | cycles = FindCycles() |
| 794 | seconds = time.time() - start |
| 795 | if "--quiet" not in emerge.opts and seconds >= 0.1: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 796 | print("Tree sanitized in %dm%.1fs" % (seconds / 60, seconds % 60)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 797 | |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 798 | def FindRecursiveProvides(pkg, seen): |
| 799 | """Find all nodes that require a particular package. |
| 800 | |
| 801 | Assumes that graph is acyclic. |
| 802 | |
| 803 | Args: |
| 804 | pkg: Package identifier. |
| 805 | seen: Nodes that have been visited so far. |
| 806 | """ |
| 807 | if pkg in seen: |
| 808 | return |
| 809 | seen.add(pkg) |
| 810 | info = deps_map[pkg] |
| 811 | info["tprovides"] = info["provides"].copy() |
| 812 | for dep in info["provides"]: |
| 813 | FindRecursiveProvides(dep, seen) |
| 814 | info["tprovides"].update(deps_map[dep]["tprovides"]) |
| 815 | |
David James | a22906f | 2011-05-04 19:53:26 -0700 | [diff] [blame] | 816 | ReverseTree(deps_tree) |
David James | a22906f | 2011-05-04 19:53:26 -0700 | [diff] [blame] | 817 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 818 | # We need to remove unused packages so that we can use the dependency |
| 819 | # ordering of the install process to show us what cycles to crack. |
| 820 | RemoveUnusedPackages() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 821 | SanitizeTree() |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 822 | seen = set() |
| 823 | for pkg in deps_map: |
| 824 | FindRecursiveProvides(pkg, seen) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 825 | return deps_map |
| 826 | |
| 827 | def PrintInstallPlan(self, deps_map): |
| 828 | """Print an emerge-style install plan. |
| 829 | |
| 830 | The install plan lists what packages we're installing, in order. |
| 831 | It's useful for understanding what parallel_emerge is doing. |
| 832 | |
| 833 | Args: |
| 834 | deps_map: The dependency graph. |
| 835 | """ |
| 836 | |
| 837 | def InstallPlanAtNode(target, deps_map): |
| 838 | nodes = [] |
| 839 | nodes.append(target) |
| 840 | for dep in deps_map[target]["provides"]: |
| 841 | del deps_map[dep]["needs"][target] |
| 842 | if not deps_map[dep]["needs"]: |
| 843 | nodes.extend(InstallPlanAtNode(dep, deps_map)) |
| 844 | return nodes |
| 845 | |
| 846 | deps_map = copy.deepcopy(deps_map) |
| 847 | install_plan = [] |
| 848 | plan = set() |
| 849 | for target, info in deps_map.iteritems(): |
| 850 | if not info["needs"] and target not in plan: |
| 851 | for item in InstallPlanAtNode(target, deps_map): |
| 852 | plan.add(item) |
| 853 | install_plan.append(self.package_db[item]) |
| 854 | |
| 855 | for pkg in plan: |
| 856 | del deps_map[pkg] |
| 857 | |
| 858 | if deps_map: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 859 | print("Cyclic dependencies:", " ".join(deps_map)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 860 | PrintDepsMap(deps_map) |
| 861 | sys.exit(1) |
| 862 | |
| 863 | self.emerge.depgraph.display(install_plan) |
| 864 | |
| 865 | |
| 866 | def PrintDepsMap(deps_map): |
| 867 | """Print dependency graph, for each package list it's prerequisites.""" |
| 868 | for i in sorted(deps_map): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 869 | print("%s: (%s) needs" % (i, deps_map[i]["action"])) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 870 | needs = deps_map[i]["needs"] |
| 871 | for j in sorted(needs): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 872 | print(" %s" % (j)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 873 | if not needs: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 874 | print(" no dependencies") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 875 | |
| 876 | |
| 877 | class EmergeJobState(object): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 878 | """Structure describing the EmergeJobState.""" |
| 879 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 880 | __slots__ = ["done", "filename", "last_notify_timestamp", "last_output_seek", |
| 881 | "last_output_timestamp", "pkgname", "retcode", "start_timestamp", |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 882 | "target", "try_count", "fetch_only", "unpack_only"] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 883 | |
| 884 | def __init__(self, target, pkgname, done, filename, start_timestamp, |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 885 | retcode=None, fetch_only=False, try_count=0, unpack_only=False): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 886 | |
| 887 | # The full name of the target we're building (e.g. |
Mike Frysinger | fd96931 | 2014-04-02 22:16:42 -0400 | [diff] [blame] | 888 | # virtual/target-os-1-r60) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 889 | self.target = target |
| 890 | |
Mike Frysinger | fd96931 | 2014-04-02 22:16:42 -0400 | [diff] [blame] | 891 | # The short name of the target we're building (e.g. target-os-1-r60) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 892 | self.pkgname = pkgname |
| 893 | |
| 894 | # Whether the job is done. (True if the job is done; false otherwise.) |
| 895 | self.done = done |
| 896 | |
| 897 | # The filename where output is currently stored. |
| 898 | self.filename = filename |
| 899 | |
| 900 | # The timestamp of the last time we printed the name of the log file. We |
| 901 | # print this at the beginning of the job, so this starts at |
| 902 | # start_timestamp. |
| 903 | self.last_notify_timestamp = start_timestamp |
| 904 | |
| 905 | # The location (in bytes) of the end of the last complete line we printed. |
| 906 | # This starts off at zero. We use this to jump to the right place when we |
| 907 | # print output from the same ebuild multiple times. |
| 908 | self.last_output_seek = 0 |
| 909 | |
| 910 | # The timestamp of the last time we printed output. Since we haven't |
| 911 | # printed output yet, this starts at zero. |
| 912 | self.last_output_timestamp = 0 |
| 913 | |
| 914 | # The return code of our job, if the job is actually finished. |
| 915 | self.retcode = retcode |
| 916 | |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 917 | # Number of tries for this job |
| 918 | self.try_count = try_count |
| 919 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 920 | # Was this just a fetch job? |
| 921 | self.fetch_only = fetch_only |
| 922 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 923 | # The timestamp when our job started. |
| 924 | self.start_timestamp = start_timestamp |
| 925 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 926 | # No emerge, only unpack packages. |
| 927 | self.unpack_only = unpack_only |
| 928 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 929 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 930 | def KillHandler(_signum, _frame): |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 931 | # Kill self and all subprocesses. |
| 932 | os.killpg(0, signal.SIGKILL) |
| 933 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 934 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 935 | def SetupWorkerSignals(): |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 936 | def ExitHandler(_signum, _frame): |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 937 | # Set KILLED flag. |
| 938 | KILLED.set() |
David James | 13cead4 | 2011-05-18 16:22:01 -0700 | [diff] [blame] | 939 | |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 940 | # Remove our signal handlers so we don't get called recursively. |
| 941 | signal.signal(signal.SIGINT, KillHandler) |
| 942 | signal.signal(signal.SIGTERM, KillHandler) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 943 | |
| 944 | # Ensure that we exit quietly and cleanly, if possible, when we receive |
| 945 | # SIGTERM or SIGINT signals. By default, when the user hits CTRL-C, all |
| 946 | # of the child processes will print details about KeyboardInterrupt |
| 947 | # exceptions, which isn't very helpful. |
| 948 | signal.signal(signal.SIGINT, ExitHandler) |
| 949 | signal.signal(signal.SIGTERM, ExitHandler) |
| 950 | |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 951 | |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 952 | def EmergeProcess(output, job_state, *args, **kwargs): |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 953 | """Merge a package in a subprocess. |
| 954 | |
| 955 | Args: |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 956 | output: Temporary file to write output. |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 957 | job_state: Stored state of package |
David James | 6b29d05 | 2012-11-02 10:27:27 -0700 | [diff] [blame] | 958 | *args: Arguments to pass to Scheduler constructor. |
| 959 | **kwargs: Keyword arguments to pass to Scheduler constructor. |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 960 | |
| 961 | Returns: |
| 962 | The exit code returned by the subprocess. |
| 963 | """ |
Chris Ching | b8eba81 | 2017-06-22 09:54:48 -0600 | [diff] [blame] | 964 | |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 965 | target = job_state.target |
| 966 | |
| 967 | job_state.try_count += 1 |
| 968 | |
Chris Ching | b8eba81 | 2017-06-22 09:54:48 -0600 | [diff] [blame] | 969 | cpv = portage_util.SplitCPV(target) |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 970 | |
Chris Ching | 4a2ebd6 | 2017-04-26 16:30:05 -0600 | [diff] [blame] | 971 | event = cros_event.newEvent(task_name="EmergePackage", |
Chris Ching | b8eba81 | 2017-06-22 09:54:48 -0600 | [diff] [blame] | 972 | name=cpv.package, |
| 973 | category=cpv.category, |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 974 | version=cpv.version, |
| 975 | try_count=job_state.try_count) |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 976 | pid = os.fork() |
| 977 | if pid == 0: |
| 978 | try: |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 979 | proctitle.settitle('EmergeProcess', target) |
| 980 | |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 981 | # Sanity checks. |
Mike Frysinger | f02736e | 2013-11-08 15:27:00 -0500 | [diff] [blame] | 982 | if sys.stdout.fileno() != 1: |
| 983 | raise Exception("sys.stdout.fileno() != 1") |
| 984 | if sys.stderr.fileno() != 2: |
| 985 | raise Exception("sys.stderr.fileno() != 2") |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 986 | |
| 987 | # - Redirect 1 (stdout) and 2 (stderr) at our temporary file. |
| 988 | # - Redirect 0 to point at sys.stdin. In this case, sys.stdin |
| 989 | # points at a file reading os.devnull, because multiprocessing mucks |
| 990 | # with sys.stdin. |
| 991 | # - Leave the sys.stdin and output filehandles alone. |
| 992 | fd_pipes = {0: sys.stdin.fileno(), |
| 993 | 1: output.fileno(), |
| 994 | 2: output.fileno(), |
| 995 | sys.stdin.fileno(): sys.stdin.fileno(), |
| 996 | output.fileno(): output.fileno()} |
Mike Frysinger | 66652ec | 2014-04-24 11:42:25 -0400 | [diff] [blame] | 997 | # pylint: disable=W0212 |
| 998 | portage.process._setup_pipes(fd_pipes, close_fds=False) |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 999 | |
| 1000 | # Portage doesn't like when sys.stdin.fileno() != 0, so point sys.stdin |
| 1001 | # at the filehandle we just created in _setup_pipes. |
| 1002 | if sys.stdin.fileno() != 0: |
David James | 6b29d05 | 2012-11-02 10:27:27 -0700 | [diff] [blame] | 1003 | sys.__stdin__ = sys.stdin = os.fdopen(0, "r") |
| 1004 | |
| 1005 | scheduler = Scheduler(*args, **kwargs) |
| 1006 | |
| 1007 | # Enable blocker handling even though we're in --nodeps mode. This |
| 1008 | # allows us to unmerge the blocker after we've merged the replacement. |
| 1009 | scheduler._opts_ignore_blockers = frozenset() |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1010 | |
| 1011 | # Actually do the merge. |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 1012 | with event: |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1013 | job_state.retcode = scheduler.merge() |
| 1014 | if job_state.retcode != 0: |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 1015 | event.fail(message="non-zero value returned") |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1016 | |
| 1017 | # We catch all exceptions here (including SystemExit, KeyboardInterrupt, |
| 1018 | # etc) so as to ensure that we don't confuse the multiprocessing module, |
| 1019 | # which expects that all forked children exit with os._exit(). |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1020 | # pylint: disable=W0702 |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1021 | except: |
| 1022 | traceback.print_exc(file=output) |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1023 | job_state.retcode = 1 |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1024 | sys.stdout.flush() |
| 1025 | sys.stderr.flush() |
| 1026 | output.flush() |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 1027 | # pylint: disable=W0212 |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1028 | os._exit(job_state.retcode) |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1029 | else: |
| 1030 | # Return the exit code of the subprocess. |
| 1031 | return os.waitpid(pid, 0)[1] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1032 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1033 | |
| 1034 | def UnpackPackage(pkg_state): |
| 1035 | """Unpacks package described by pkg_state. |
| 1036 | |
| 1037 | Args: |
| 1038 | pkg_state: EmergeJobState object describing target. |
| 1039 | |
| 1040 | Returns: |
| 1041 | Exit code returned by subprocess. |
| 1042 | """ |
| 1043 | pkgdir = os.environ.get("PKGDIR", |
| 1044 | os.path.join(os.environ["SYSROOT"], "packages")) |
| 1045 | root = os.environ.get("ROOT", os.environ["SYSROOT"]) |
| 1046 | path = os.path.join(pkgdir, pkg_state.target + ".tbz2") |
| 1047 | comp = cros_build_lib.FindCompressor(cros_build_lib.COMP_BZIP2) |
| 1048 | cmd = [comp, "-dc"] |
| 1049 | if comp.endswith("pbzip2"): |
| 1050 | cmd.append("--ignore-trailing-garbage=1") |
| 1051 | cmd.append(path) |
| 1052 | |
Chris Ching | 4a2ebd6 | 2017-04-26 16:30:05 -0600 | [diff] [blame] | 1053 | with cros_event.newEvent(task_name="UnpackPackage", **pkg_state) as event: |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 1054 | result = cros_build_lib.RunCommand(cmd, cwd=root, stdout_to_pipe=True, |
| 1055 | print_cmd=False, error_code_ok=True) |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1056 | |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 1057 | # If we were not successful, return now and don't attempt untar. |
| 1058 | if result.returncode != 0: |
| 1059 | event.fail("error compressing: returned {}".format(result.returncode)) |
| 1060 | return result.returncode |
| 1061 | |
| 1062 | cmd = ["sudo", "tar", "-xf", "-", "-C", root] |
| 1063 | |
| 1064 | result = cros_build_lib.RunCommand(cmd, cwd=root, input=result.output, |
| 1065 | print_cmd=False, error_code_ok=True) |
| 1066 | if result.returncode != 0: |
| 1067 | event.fail("error extracting:returned {}".format(result.returncode)) |
| 1068 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1069 | return result.returncode |
| 1070 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1071 | |
| 1072 | def EmergeWorker(task_queue, job_queue, emerge, package_db, fetch_only=False, |
| 1073 | unpack_only=False): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1074 | """This worker emerges any packages given to it on the task_queue. |
| 1075 | |
| 1076 | Args: |
| 1077 | task_queue: The queue of tasks for this worker to do. |
| 1078 | job_queue: The queue of results from the worker. |
| 1079 | emerge: An EmergeData() object. |
| 1080 | package_db: A dict, mapping package ids to portage Package objects. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1081 | fetch_only: A bool, indicating if we should just fetch the target. |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1082 | unpack_only: A bool, indicating if we should just unpack the target. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1083 | |
| 1084 | It expects package identifiers to be passed to it via task_queue. When |
| 1085 | a task is started, it pushes the (target, filename) to the started_queue. |
| 1086 | The output is stored in filename. When a merge starts or finishes, we push |
| 1087 | EmergeJobState objects to the job_queue. |
| 1088 | """ |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 1089 | if fetch_only: |
| 1090 | mode = 'fetch' |
| 1091 | elif unpack_only: |
| 1092 | mode = 'unpack' |
| 1093 | else: |
| 1094 | mode = 'emerge' |
| 1095 | proctitle.settitle('EmergeWorker', mode, '[idle]') |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1096 | |
| 1097 | SetupWorkerSignals() |
| 1098 | settings, trees, mtimedb = emerge.settings, emerge.trees, emerge.mtimedb |
David James | deebd69 | 2011-05-09 17:02:52 -0700 | [diff] [blame] | 1099 | |
| 1100 | # Disable flushing of caches to save on I/O. |
David James | 7a1ea4b | 2011-10-13 15:06:41 -0700 | [diff] [blame] | 1101 | root = emerge.settings["ROOT"] |
| 1102 | vardb = emerge.trees[root]["vartree"].dbapi |
Mike Frysinger | e56debd | 2014-11-19 01:54:36 -0500 | [diff] [blame] | 1103 | vardb._flush_cache_enabled = False # pylint: disable=protected-access |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1104 | bindb = emerge.trees[root]["bintree"].dbapi |
| 1105 | # Might be a set, might be a list, might be None; no clue, just use shallow |
| 1106 | # copy to ensure we can roll it back. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 1107 | # pylint: disable=W0212 |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1108 | original_remotepkgs = copy.copy(bindb.bintree._remotepkgs) |
David James | deebd69 | 2011-05-09 17:02:52 -0700 | [diff] [blame] | 1109 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1110 | opts, spinner = emerge.opts, emerge.spinner |
| 1111 | opts["--nodeps"] = True |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1112 | if fetch_only: |
| 1113 | opts["--fetchonly"] = True |
| 1114 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1115 | while True: |
| 1116 | # Wait for a new item to show up on the queue. This is a blocking wait, |
| 1117 | # so if there's nothing to do, we just sit here. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1118 | pkg_state = task_queue.get() |
| 1119 | if pkg_state is None: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1120 | # If target is None, this means that the main thread wants us to quit. |
| 1121 | # The other workers need to exit too, so we'll push the message back on |
| 1122 | # to the queue so they'll get it too. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1123 | task_queue.put(None) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1124 | return |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1125 | if KILLED.is_set(): |
| 1126 | return |
| 1127 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1128 | target = pkg_state.target |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 1129 | proctitle.settitle('EmergeWorker', mode, target) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1130 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1131 | db_pkg = package_db[target] |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1132 | |
| 1133 | if db_pkg.type_name == "binary": |
| 1134 | if not fetch_only and pkg_state.fetched_successfully: |
| 1135 | # Ensure portage doesn't think our pkg is remote- else it'll force |
| 1136 | # a redownload of it (even if the on-disk file is fine). In-memory |
| 1137 | # caching basically, implemented dumbly. |
| 1138 | bindb.bintree._remotepkgs = None |
| 1139 | else: |
| 1140 | bindb.bintree_remotepkgs = original_remotepkgs |
| 1141 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1142 | db_pkg.root_config = emerge.root_config |
| 1143 | install_list = [db_pkg] |
| 1144 | pkgname = db_pkg.pf |
| 1145 | output = tempfile.NamedTemporaryFile(prefix=pkgname + "-", delete=False) |
David James | 01b1e0f | 2012-06-07 17:18:05 -0700 | [diff] [blame] | 1146 | os.chmod(output.name, 644) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1147 | start_timestamp = time.time() |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1148 | job = EmergeJobState(target, pkgname, False, output.name, start_timestamp, |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1149 | fetch_only=fetch_only, unpack_only=unpack_only) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1150 | job_queue.put(job) |
| 1151 | if "--pretend" in opts: |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1152 | job.retcode = 0 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1153 | else: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1154 | try: |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 1155 | emerge.scheduler_graph.mergelist = install_list |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1156 | if unpack_only: |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1157 | job.retcode = UnpackPackage(pkg_state) |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1158 | else: |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1159 | job.retcode = EmergeProcess(output, job, settings, trees, mtimedb, |
| 1160 | opts, spinner, |
| 1161 | favorites=emerge.favorites, |
| 1162 | graph_config=emerge.scheduler_graph) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1163 | except Exception: |
| 1164 | traceback.print_exc(file=output) |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1165 | job.retcode = 1 |
David James | 1ed3e25 | 2011-10-05 20:26:15 -0700 | [diff] [blame] | 1166 | output.close() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1167 | |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1168 | if KILLED.is_set(): |
| 1169 | return |
| 1170 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1171 | job = EmergeJobState(target, pkgname, True, output.name, start_timestamp, |
Chris Ching | 73486ab | 2017-04-26 18:02:37 -0600 | [diff] [blame] | 1172 | job.retcode, fetch_only=fetch_only, |
| 1173 | try_count=job.try_count, unpack_only=unpack_only) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1174 | job_queue.put(job) |
| 1175 | |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 1176 | # Set the title back to idle as the multiprocess pool won't destroy us; |
| 1177 | # when another job comes up, it'll re-use this process. |
| 1178 | proctitle.settitle('EmergeWorker', mode, '[idle]') |
| 1179 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1180 | |
| 1181 | class LinePrinter(object): |
| 1182 | """Helper object to print a single line.""" |
| 1183 | |
| 1184 | def __init__(self, line): |
| 1185 | self.line = line |
| 1186 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1187 | def Print(self, _seek_locations): |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1188 | print(self.line) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1189 | |
| 1190 | |
| 1191 | class JobPrinter(object): |
| 1192 | """Helper object to print output of a job.""" |
| 1193 | |
| 1194 | def __init__(self, job, unlink=False): |
| 1195 | """Print output of job. |
| 1196 | |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 1197 | If unlink is True, unlink the job output file when done. |
| 1198 | """ |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1199 | self.current_time = time.time() |
| 1200 | self.job = job |
| 1201 | self.unlink = unlink |
| 1202 | |
| 1203 | def Print(self, seek_locations): |
| 1204 | |
| 1205 | job = self.job |
| 1206 | |
| 1207 | # Calculate how long the job has been running. |
| 1208 | seconds = self.current_time - job.start_timestamp |
| 1209 | |
| 1210 | # Note that we've printed out the job so far. |
| 1211 | job.last_output_timestamp = self.current_time |
| 1212 | |
| 1213 | # Note that we're starting the job |
| 1214 | info = "job %s (%dm%.1fs)" % (job.pkgname, seconds / 60, seconds % 60) |
| 1215 | last_output_seek = seek_locations.get(job.filename, 0) |
| 1216 | if last_output_seek: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1217 | print("=== Continue output for %s ===" % info) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1218 | else: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1219 | print("=== Start output for %s ===" % info) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1220 | |
| 1221 | # Print actual output from job |
| 1222 | f = codecs.open(job.filename, encoding='utf-8', errors='replace') |
| 1223 | f.seek(last_output_seek) |
| 1224 | prefix = job.pkgname + ":" |
| 1225 | for line in f: |
| 1226 | |
| 1227 | # Save off our position in the file |
| 1228 | if line and line[-1] == "\n": |
| 1229 | last_output_seek = f.tell() |
| 1230 | line = line[:-1] |
| 1231 | |
| 1232 | # Print our line |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1233 | print(prefix, line.encode('utf-8', 'replace')) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1234 | f.close() |
| 1235 | |
| 1236 | # Save our last spot in the file so that we don't print out the same |
| 1237 | # location twice. |
| 1238 | seek_locations[job.filename] = last_output_seek |
| 1239 | |
| 1240 | # Note end of output section |
| 1241 | if job.done: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1242 | print("=== Complete: %s ===" % info) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1243 | else: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1244 | print("=== Still running: %s ===" % info) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1245 | |
| 1246 | if self.unlink: |
| 1247 | os.unlink(job.filename) |
| 1248 | |
| 1249 | |
| 1250 | def PrintWorker(queue): |
| 1251 | """A worker that prints stuff to the screen as requested.""" |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 1252 | proctitle.settitle('PrintWorker') |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1253 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1254 | def ExitHandler(_signum, _frame): |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1255 | # Set KILLED flag. |
| 1256 | KILLED.set() |
| 1257 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1258 | # Switch to default signal handlers so that we'll die after two signals. |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1259 | signal.signal(signal.SIGINT, KillHandler) |
| 1260 | signal.signal(signal.SIGTERM, KillHandler) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1261 | |
| 1262 | # Don't exit on the first SIGINT / SIGTERM, because the parent worker will |
| 1263 | # handle it and tell us when we need to exit. |
| 1264 | signal.signal(signal.SIGINT, ExitHandler) |
| 1265 | signal.signal(signal.SIGTERM, ExitHandler) |
| 1266 | |
| 1267 | # seek_locations is a map indicating the position we are at in each file. |
| 1268 | # It starts off empty, but is set by the various Print jobs as we go along |
| 1269 | # to indicate where we left off in each file. |
| 1270 | seek_locations = {} |
| 1271 | while True: |
| 1272 | try: |
| 1273 | job = queue.get() |
| 1274 | if job: |
| 1275 | job.Print(seek_locations) |
David James | bccf8eb | 2011-07-27 14:06:06 -0700 | [diff] [blame] | 1276 | sys.stdout.flush() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1277 | else: |
| 1278 | break |
| 1279 | except IOError as ex: |
| 1280 | if ex.errno == errno.EINTR: |
| 1281 | # Looks like we received a signal. Keep printing. |
| 1282 | continue |
| 1283 | raise |
| 1284 | |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1285 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1286 | class TargetState(object): |
Chris Ching | 5fcbd62 | 2016-11-28 09:22:15 -0700 | [diff] [blame] | 1287 | """Structure describing the TargetState.""" |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1288 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1289 | __slots__ = ("target", "info", "score", "prefetched", "fetched_successfully") |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1290 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1291 | def __init__(self, target, info): |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1292 | self.target, self.info = target, info |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1293 | self.fetched_successfully = False |
| 1294 | self.prefetched = False |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1295 | self.score = None |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1296 | self.update_score() |
| 1297 | |
| 1298 | def __cmp__(self, other): |
| 1299 | return cmp(self.score, other.score) |
| 1300 | |
| 1301 | def update_score(self): |
| 1302 | self.score = ( |
| 1303 | -len(self.info["tprovides"]), |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1304 | len(self.info["needs"]), |
Brian Harring | 11c5eeb | 2012-03-18 11:02:39 -0700 | [diff] [blame] | 1305 | not self.info["binary"], |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1306 | -len(self.info["provides"]), |
| 1307 | self.info["idx"], |
| 1308 | self.target, |
| 1309 | ) |
| 1310 | |
| 1311 | |
| 1312 | class ScoredHeap(object): |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 1313 | """Implementation of a general purpose scored heap.""" |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1314 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1315 | __slots__ = ("heap", "_heap_set") |
| 1316 | |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1317 | def __init__(self, initial=()): |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1318 | self.heap = list() |
| 1319 | self._heap_set = set() |
| 1320 | if initial: |
| 1321 | self.multi_put(initial) |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1322 | |
| 1323 | def get(self): |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1324 | item = heapq.heappop(self.heap) |
| 1325 | self._heap_set.remove(item.target) |
| 1326 | return item |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1327 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1328 | def put(self, item): |
| 1329 | if not isinstance(item, TargetState): |
| 1330 | raise ValueError("Item %r isn't a TargetState" % (item,)) |
| 1331 | heapq.heappush(self.heap, item) |
| 1332 | self._heap_set.add(item.target) |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1333 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1334 | def multi_put(self, sequence): |
| 1335 | sequence = list(sequence) |
| 1336 | self.heap.extend(sequence) |
| 1337 | self._heap_set.update(x.target for x in sequence) |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1338 | self.sort() |
| 1339 | |
David James | 5c9996d | 2012-03-24 10:50:46 -0700 | [diff] [blame] | 1340 | def sort(self): |
| 1341 | heapq.heapify(self.heap) |
| 1342 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1343 | def __contains__(self, target): |
| 1344 | return target in self._heap_set |
| 1345 | |
| 1346 | def __nonzero__(self): |
| 1347 | return bool(self.heap) |
| 1348 | |
Brian Harring | 867e236 | 2012-03-17 04:05:17 -0700 | [diff] [blame] | 1349 | def __len__(self): |
| 1350 | return len(self.heap) |
| 1351 | |
| 1352 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1353 | class EmergeQueue(object): |
| 1354 | """Class to schedule emerge jobs according to a dependency graph.""" |
| 1355 | |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1356 | def __init__(self, deps_map, emerge, package_db, show_output, unpack_only, |
| 1357 | max_retries): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1358 | # Store the dependency graph. |
| 1359 | self._deps_map = deps_map |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1360 | self._state_map = {} |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1361 | # Initialize the running queue to empty |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1362 | self._build_jobs = {} |
| 1363 | self._build_ready = ScoredHeap() |
| 1364 | self._fetch_jobs = {} |
| 1365 | self._fetch_ready = ScoredHeap() |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1366 | self._unpack_jobs = {} |
| 1367 | self._unpack_ready = ScoredHeap() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1368 | # List of total package installs represented in deps_map. |
| 1369 | install_jobs = [x for x in deps_map if deps_map[x]["action"] == "merge"] |
| 1370 | self._total_jobs = len(install_jobs) |
| 1371 | self._show_output = show_output |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1372 | self._unpack_only = unpack_only |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1373 | self._max_retries = max_retries |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1374 | |
| 1375 | if "--pretend" in emerge.opts: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1376 | print("Skipping merge because of --pretend mode.") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1377 | sys.exit(0) |
| 1378 | |
David James | aaf49e4 | 2014-04-24 09:40:05 -0700 | [diff] [blame] | 1379 | # Set up a session so we can easily terminate all children. |
| 1380 | self._SetupSession() |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1381 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1382 | # Setup scheduler graph object. This is used by the child processes |
| 1383 | # to help schedule jobs. |
| 1384 | emerge.scheduler_graph = emerge.depgraph.schedulerGraph() |
| 1385 | |
| 1386 | # Calculate how many jobs we can run in parallel. We don't want to pass |
| 1387 | # the --jobs flag over to emerge itself, because that'll tell emerge to |
| 1388 | # hide its output, and said output is quite useful for debugging hung |
| 1389 | # jobs. |
| 1390 | procs = min(self._total_jobs, |
| 1391 | emerge.opts.pop("--jobs", multiprocessing.cpu_count())) |
Nam T. Nguyen | f7098b3 | 2015-05-08 11:04:48 -0700 | [diff] [blame] | 1392 | self._build_procs = self._unpack_procs = max(1, procs) |
| 1393 | # Fetch is IO bound, we can use more processes. |
| 1394 | self._fetch_procs = max(4, procs) |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1395 | self._load_avg = emerge.opts.pop("--load-average", None) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1396 | self._job_queue = multiprocessing.Queue() |
| 1397 | self._print_queue = multiprocessing.Queue() |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1398 | |
| 1399 | self._fetch_queue = multiprocessing.Queue() |
| 1400 | args = (self._fetch_queue, self._job_queue, emerge, package_db, True) |
| 1401 | self._fetch_pool = multiprocessing.Pool(self._fetch_procs, EmergeWorker, |
| 1402 | args) |
| 1403 | |
| 1404 | self._build_queue = multiprocessing.Queue() |
| 1405 | args = (self._build_queue, self._job_queue, emerge, package_db) |
| 1406 | self._build_pool = multiprocessing.Pool(self._build_procs, EmergeWorker, |
| 1407 | args) |
| 1408 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1409 | if self._unpack_only: |
| 1410 | # Unpack pool only required on unpack_only jobs. |
| 1411 | self._unpack_queue = multiprocessing.Queue() |
| 1412 | args = (self._unpack_queue, self._job_queue, emerge, package_db, False, |
| 1413 | True) |
| 1414 | self._unpack_pool = multiprocessing.Pool(self._unpack_procs, EmergeWorker, |
| 1415 | args) |
| 1416 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1417 | self._print_worker = multiprocessing.Process(target=PrintWorker, |
| 1418 | args=[self._print_queue]) |
| 1419 | self._print_worker.start() |
| 1420 | |
| 1421 | # Initialize the failed queue to empty. |
| 1422 | self._retry_queue = [] |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1423 | self._failed_count = dict() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1424 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1425 | # Setup an exit handler so that we print nice messages if we are |
| 1426 | # terminated. |
| 1427 | self._SetupExitHandler() |
| 1428 | |
| 1429 | # Schedule our jobs. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1430 | self._state_map.update( |
| 1431 | (pkg, TargetState(pkg, data)) for pkg, data in deps_map.iteritems()) |
| 1432 | self._fetch_ready.multi_put(self._state_map.itervalues()) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1433 | |
David James | aaf49e4 | 2014-04-24 09:40:05 -0700 | [diff] [blame] | 1434 | def _SetupSession(self): |
| 1435 | """Set up a session so we can easily terminate all children.""" |
| 1436 | # When we call os.setsid(), this sets up a session / process group for this |
| 1437 | # process and all children. These session groups are needed so that we can |
| 1438 | # easily kill all children (including processes launched by emerge) before |
| 1439 | # we exit. |
| 1440 | # |
| 1441 | # One unfortunate side effect of os.setsid() is that it blocks CTRL-C from |
| 1442 | # being received. To work around this, we only call os.setsid() in a forked |
| 1443 | # process, so that the parent can still watch for CTRL-C. The parent will |
| 1444 | # just sit around, watching for signals and propagating them to the child, |
| 1445 | # until the child exits. |
| 1446 | # |
| 1447 | # TODO(davidjames): It would be nice if we could replace this with cgroups. |
| 1448 | pid = os.fork() |
| 1449 | if pid == 0: |
| 1450 | os.setsid() |
| 1451 | else: |
Mike Frysinger | d74fe4a | 2014-04-24 11:43:38 -0400 | [diff] [blame] | 1452 | proctitle.settitle('SessionManager') |
| 1453 | |
David James | aaf49e4 | 2014-04-24 09:40:05 -0700 | [diff] [blame] | 1454 | def PropagateToChildren(signum, _frame): |
| 1455 | # Just propagate the signals down to the child. We'll exit when the |
| 1456 | # child does. |
| 1457 | try: |
| 1458 | os.kill(pid, signum) |
| 1459 | except OSError as ex: |
| 1460 | if ex.errno != errno.ESRCH: |
| 1461 | raise |
| 1462 | signal.signal(signal.SIGINT, PropagateToChildren) |
| 1463 | signal.signal(signal.SIGTERM, PropagateToChildren) |
| 1464 | |
| 1465 | def StopGroup(_signum, _frame): |
| 1466 | # When we get stopped, stop the children. |
| 1467 | try: |
| 1468 | os.killpg(pid, signal.SIGSTOP) |
| 1469 | os.kill(0, signal.SIGSTOP) |
| 1470 | except OSError as ex: |
| 1471 | if ex.errno != errno.ESRCH: |
| 1472 | raise |
| 1473 | signal.signal(signal.SIGTSTP, StopGroup) |
| 1474 | |
| 1475 | def ContinueGroup(_signum, _frame): |
| 1476 | # Launch the children again after being stopped. |
| 1477 | try: |
| 1478 | os.killpg(pid, signal.SIGCONT) |
| 1479 | except OSError as ex: |
| 1480 | if ex.errno != errno.ESRCH: |
| 1481 | raise |
| 1482 | signal.signal(signal.SIGCONT, ContinueGroup) |
| 1483 | |
| 1484 | # Loop until the children exit. We exit with os._exit to be sure we |
| 1485 | # don't run any finalizers (those will be run by the child process.) |
| 1486 | # pylint: disable=W0212 |
| 1487 | while True: |
| 1488 | try: |
| 1489 | # Wait for the process to exit. When it does, exit with the return |
| 1490 | # value of the subprocess. |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 1491 | os._exit(process_util.GetExitStatus(os.waitpid(pid, 0)[1])) |
David James | aaf49e4 | 2014-04-24 09:40:05 -0700 | [diff] [blame] | 1492 | except OSError as ex: |
| 1493 | if ex.errno == errno.EINTR: |
| 1494 | continue |
| 1495 | traceback.print_exc() |
| 1496 | os._exit(1) |
| 1497 | except BaseException: |
| 1498 | traceback.print_exc() |
| 1499 | os._exit(1) |
| 1500 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1501 | def _SetupExitHandler(self): |
| 1502 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1503 | def ExitHandler(signum, _frame): |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1504 | # Set KILLED flag. |
| 1505 | KILLED.set() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1506 | |
| 1507 | # Kill our signal handlers so we don't get called recursively |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1508 | signal.signal(signal.SIGINT, KillHandler) |
| 1509 | signal.signal(signal.SIGTERM, KillHandler) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1510 | |
| 1511 | # Print our current job status |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1512 | for job in self._build_jobs.itervalues(): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1513 | if job: |
| 1514 | self._print_queue.put(JobPrinter(job, unlink=True)) |
| 1515 | |
| 1516 | # Notify the user that we are exiting |
| 1517 | self._Print("Exiting on signal %s" % signum) |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1518 | self._print_queue.put(None) |
| 1519 | self._print_worker.join() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1520 | |
| 1521 | # Kill child threads, then exit. |
David James | 7358d03 | 2011-05-19 10:40:03 -0700 | [diff] [blame] | 1522 | os.killpg(0, signal.SIGKILL) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1523 | sys.exit(1) |
| 1524 | |
| 1525 | # Print out job status when we are killed |
| 1526 | signal.signal(signal.SIGINT, ExitHandler) |
| 1527 | signal.signal(signal.SIGTERM, ExitHandler) |
| 1528 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1529 | def _ScheduleUnpack(self, pkg_state): |
| 1530 | self._unpack_jobs[pkg_state.target] = None |
| 1531 | self._unpack_queue.put(pkg_state) |
| 1532 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1533 | def _Schedule(self, pkg_state): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1534 | # We maintain a tree of all deps, if this doesn't need |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1535 | # to be installed just free up its children and continue. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1536 | # It is possible to reinstall deps of deps, without reinstalling |
| 1537 | # first level deps, like so: |
Mike Frysinger | fd96931 | 2014-04-02 22:16:42 -0400 | [diff] [blame] | 1538 | # virtual/target-os (merge) -> eselect (nomerge) -> python (merge) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1539 | this_pkg = pkg_state.info |
| 1540 | target = pkg_state.target |
| 1541 | if pkg_state.info is not None: |
| 1542 | if this_pkg["action"] == "nomerge": |
| 1543 | self._Finish(target) |
| 1544 | elif target not in self._build_jobs: |
| 1545 | # Kick off the build if it's marked to be built. |
| 1546 | self._build_jobs[target] = None |
| 1547 | self._build_queue.put(pkg_state) |
| 1548 | return True |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1549 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1550 | def _ScheduleLoop(self, unpack_only=False): |
| 1551 | if unpack_only: |
| 1552 | ready_queue = self._unpack_ready |
| 1553 | jobs_queue = self._unpack_jobs |
| 1554 | procs = self._unpack_procs |
| 1555 | else: |
| 1556 | ready_queue = self._build_ready |
| 1557 | jobs_queue = self._build_jobs |
| 1558 | procs = self._build_procs |
| 1559 | |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1560 | # If the current load exceeds our desired load average, don't schedule |
| 1561 | # more than one job. |
| 1562 | if self._load_avg and os.getloadavg()[0] > self._load_avg: |
| 1563 | needed_jobs = 1 |
| 1564 | else: |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1565 | needed_jobs = procs |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1566 | |
| 1567 | # Schedule more jobs. |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1568 | while ready_queue and len(jobs_queue) < needed_jobs: |
| 1569 | state = ready_queue.get() |
| 1570 | if unpack_only: |
| 1571 | self._ScheduleUnpack(state) |
| 1572 | else: |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1573 | if state.target not in self._failed_count: |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1574 | self._Schedule(state) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1575 | |
| 1576 | def _Print(self, line): |
| 1577 | """Print a single line.""" |
| 1578 | self._print_queue.put(LinePrinter(line)) |
| 1579 | |
| 1580 | def _Status(self): |
| 1581 | """Print status.""" |
| 1582 | current_time = time.time() |
Aviv Keshet | 3b38168 | 2015-11-12 13:15:06 -0800 | [diff] [blame] | 1583 | current_time_struct = time.localtime(current_time) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1584 | no_output = True |
| 1585 | |
| 1586 | # Print interim output every minute if --show-output is used. Otherwise, |
| 1587 | # print notifications about running packages every 2 minutes, and print |
| 1588 | # full output for jobs that have been running for 60 minutes or more. |
| 1589 | if self._show_output: |
| 1590 | interval = 60 |
| 1591 | notify_interval = 0 |
| 1592 | else: |
| 1593 | interval = 60 * 60 |
| 1594 | notify_interval = 60 * 2 |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1595 | for job in self._build_jobs.itervalues(): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1596 | if job: |
| 1597 | last_timestamp = max(job.start_timestamp, job.last_output_timestamp) |
| 1598 | if last_timestamp + interval < current_time: |
| 1599 | self._print_queue.put(JobPrinter(job)) |
| 1600 | job.last_output_timestamp = current_time |
| 1601 | no_output = False |
| 1602 | elif (notify_interval and |
| 1603 | job.last_notify_timestamp + notify_interval < current_time): |
| 1604 | job_seconds = current_time - job.start_timestamp |
| 1605 | args = (job.pkgname, job_seconds / 60, job_seconds % 60, job.filename) |
| 1606 | info = "Still building %s (%dm%.1fs). Logs in %s" % args |
| 1607 | job.last_notify_timestamp = current_time |
| 1608 | self._Print(info) |
| 1609 | no_output = False |
| 1610 | |
| 1611 | # If we haven't printed any messages yet, print a general status message |
| 1612 | # here. |
| 1613 | if no_output: |
| 1614 | seconds = current_time - GLOBAL_START |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1615 | fjobs, fready = len(self._fetch_jobs), len(self._fetch_ready) |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1616 | ujobs, uready = len(self._unpack_jobs), len(self._unpack_ready) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1617 | bjobs, bready = len(self._build_jobs), len(self._build_ready) |
| 1618 | retries = len(self._retry_queue) |
| 1619 | pending = max(0, len(self._deps_map) - fjobs - bjobs) |
| 1620 | line = "Pending %s/%s, " % (pending, self._total_jobs) |
| 1621 | if fjobs or fready: |
| 1622 | line += "Fetching %s/%s, " % (fjobs, fready + fjobs) |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1623 | if ujobs or uready: |
| 1624 | line += "Unpacking %s/%s, " % (ujobs, uready + ujobs) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1625 | if bjobs or bready or retries: |
| 1626 | line += "Building %s/%s, " % (bjobs, bready + bjobs) |
| 1627 | if retries: |
| 1628 | line += "Retrying %s, " % (retries,) |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 1629 | load = " ".join(str(x) for x in os.getloadavg()) |
Aviv Keshet | 3b38168 | 2015-11-12 13:15:06 -0800 | [diff] [blame] | 1630 | line += ("[Time %s | Elapsed %dm%.1fs | Load %s]" % ( |
| 1631 | time.strftime('%H:%M:%S', current_time_struct), seconds / 60, |
| 1632 | seconds % 60, load)) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1633 | self._Print(line) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1634 | |
| 1635 | def _Finish(self, target): |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1636 | """Mark a target as completed and unblock dependencies.""" |
| 1637 | this_pkg = self._deps_map[target] |
| 1638 | if this_pkg["needs"] and this_pkg["nodeps"]: |
| 1639 | # We got installed, but our deps have not been installed yet. Dependent |
| 1640 | # packages should only be installed when our needs have been fully met. |
| 1641 | this_pkg["action"] = "nomerge" |
| 1642 | else: |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1643 | for dep in this_pkg["provides"]: |
| 1644 | dep_pkg = self._deps_map[dep] |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1645 | state = self._state_map[dep] |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1646 | del dep_pkg["needs"][target] |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1647 | state.update_score() |
| 1648 | if not state.prefetched: |
| 1649 | if dep in self._fetch_ready: |
| 1650 | # If it's not currently being fetched, update the prioritization |
| 1651 | self._fetch_ready.sort() |
| 1652 | elif not dep_pkg["needs"]: |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1653 | if dep_pkg["nodeps"] and dep_pkg["action"] == "nomerge": |
| 1654 | self._Finish(dep) |
| 1655 | else: |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1656 | self._build_ready.put(self._state_map[dep]) |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1657 | self._deps_map.pop(target) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1658 | |
| 1659 | def _Retry(self): |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1660 | while self._retry_queue: |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1661 | state = self._retry_queue.pop(0) |
| 1662 | if self._Schedule(state): |
| 1663 | self._Print("Retrying emerge of %s." % state.target) |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1664 | break |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1665 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1666 | def _Shutdown(self): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1667 | # Tell emerge workers to exit. They all exit when 'None' is pushed |
| 1668 | # to the queue. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1669 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1670 | # Shutdown the workers first; then jobs (which is how they feed things back) |
| 1671 | # then finally the print queue. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1672 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1673 | def _stop(queue, pool): |
| 1674 | if pool is None: |
| 1675 | return |
| 1676 | try: |
| 1677 | queue.put(None) |
| 1678 | pool.close() |
| 1679 | pool.join() |
| 1680 | finally: |
| 1681 | pool.terminate() |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1682 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1683 | _stop(self._fetch_queue, self._fetch_pool) |
| 1684 | self._fetch_queue = self._fetch_pool = None |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1685 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1686 | _stop(self._build_queue, self._build_pool) |
| 1687 | self._build_queue = self._build_pool = None |
| 1688 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1689 | if self._unpack_only: |
| 1690 | _stop(self._unpack_queue, self._unpack_pool) |
| 1691 | self._unpack_queue = self._unpack_pool = None |
| 1692 | |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1693 | if self._job_queue is not None: |
| 1694 | self._job_queue.close() |
| 1695 | self._job_queue = None |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1696 | |
| 1697 | # Now that our workers are finished, we can kill the print queue. |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1698 | if self._print_worker is not None: |
| 1699 | try: |
| 1700 | self._print_queue.put(None) |
| 1701 | self._print_queue.close() |
| 1702 | self._print_worker.join() |
| 1703 | finally: |
| 1704 | self._print_worker.terminate() |
| 1705 | self._print_queue = self._print_worker = None |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1706 | |
| 1707 | def Run(self): |
| 1708 | """Run through the scheduled ebuilds. |
| 1709 | |
| 1710 | Keep running so long as we have uninstalled packages in the |
| 1711 | dependency graph to merge. |
| 1712 | """ |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1713 | if not self._deps_map: |
| 1714 | return |
| 1715 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1716 | # Start the fetchers. |
| 1717 | for _ in xrange(min(self._fetch_procs, len(self._fetch_ready))): |
| 1718 | state = self._fetch_ready.get() |
| 1719 | self._fetch_jobs[state.target] = None |
| 1720 | self._fetch_queue.put(state) |
| 1721 | |
| 1722 | # Print an update, then get going. |
| 1723 | self._Status() |
| 1724 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1725 | while self._deps_map: |
| 1726 | # Check here that we are actually waiting for something. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1727 | if (self._build_queue.empty() and |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1728 | self._job_queue.empty() and |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1729 | not self._fetch_jobs and |
| 1730 | not self._fetch_ready and |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1731 | not self._unpack_jobs and |
| 1732 | not self._unpack_ready and |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1733 | not self._build_jobs and |
| 1734 | not self._build_ready and |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1735 | self._deps_map): |
| 1736 | # If we have failed on a package, retry it now. |
| 1737 | if self._retry_queue: |
| 1738 | self._Retry() |
| 1739 | else: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1740 | # Tell the user why we're exiting. |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1741 | if self._failed_count: |
| 1742 | print('Packages failed:\n\t%s' % |
| 1743 | '\n\t'.join(self._failed_count.iterkeys())) |
David James | 0eae23e | 2012-07-03 15:04:25 -0700 | [diff] [blame] | 1744 | status_file = os.environ.get("PARALLEL_EMERGE_STATUS_FILE") |
| 1745 | if status_file: |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1746 | failed_pkgs = set(portage.versions.cpv_getkey(x) |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1747 | for x in self._failed_count.iterkeys()) |
David James | 0eae23e | 2012-07-03 15:04:25 -0700 | [diff] [blame] | 1748 | with open(status_file, "a") as f: |
| 1749 | f.write("%s\n" % " ".join(failed_pkgs)) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1750 | else: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1751 | print("Deadlock! Circular dependencies!") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1752 | sys.exit(1) |
| 1753 | |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1754 | for _ in xrange(12): |
David James | a74289a | 2011-08-12 10:41:24 -0700 | [diff] [blame] | 1755 | try: |
| 1756 | job = self._job_queue.get(timeout=5) |
| 1757 | break |
| 1758 | except Queue.Empty: |
| 1759 | # Check if any more jobs can be scheduled. |
| 1760 | self._ScheduleLoop() |
| 1761 | else: |
Brian Harring | 706747c | 2012-03-16 03:04:31 -0700 | [diff] [blame] | 1762 | # Print an update every 60 seconds. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1763 | self._Status() |
| 1764 | continue |
| 1765 | |
| 1766 | target = job.target |
| 1767 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1768 | if job.fetch_only: |
| 1769 | if not job.done: |
| 1770 | self._fetch_jobs[job.target] = job |
| 1771 | else: |
| 1772 | state = self._state_map[job.target] |
| 1773 | state.prefetched = True |
| 1774 | state.fetched_successfully = (job.retcode == 0) |
| 1775 | del self._fetch_jobs[job.target] |
| 1776 | self._Print("Fetched %s in %2.2fs" |
| 1777 | % (target, time.time() - job.start_timestamp)) |
| 1778 | |
| 1779 | if self._show_output or job.retcode != 0: |
| 1780 | self._print_queue.put(JobPrinter(job, unlink=True)) |
| 1781 | else: |
| 1782 | os.unlink(job.filename) |
| 1783 | # Failure or not, let build work with it next. |
| 1784 | if not self._deps_map[job.target]["needs"]: |
| 1785 | self._build_ready.put(state) |
| 1786 | self._ScheduleLoop() |
| 1787 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1788 | if self._unpack_only and job.retcode == 0: |
| 1789 | self._unpack_ready.put(state) |
| 1790 | self._ScheduleLoop(unpack_only=True) |
| 1791 | |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1792 | if self._fetch_ready: |
| 1793 | state = self._fetch_ready.get() |
| 1794 | self._fetch_queue.put(state) |
| 1795 | self._fetch_jobs[state.target] = None |
| 1796 | else: |
| 1797 | # Minor optimization; shut down fetchers early since we know |
| 1798 | # the queue is empty. |
| 1799 | self._fetch_queue.put(None) |
| 1800 | continue |
| 1801 | |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1802 | if job.unpack_only: |
| 1803 | if not job.done: |
| 1804 | self._unpack_jobs[target] = job |
| 1805 | else: |
| 1806 | del self._unpack_jobs[target] |
| 1807 | self._Print("Unpacked %s in %2.2fs" |
| 1808 | % (target, time.time() - job.start_timestamp)) |
| 1809 | if self._show_output or job.retcode != 0: |
| 1810 | self._print_queue.put(JobPrinter(job, unlink=True)) |
| 1811 | else: |
| 1812 | os.unlink(job.filename) |
| 1813 | if self._unpack_ready: |
| 1814 | state = self._unpack_ready.get() |
| 1815 | self._unpack_queue.put(state) |
| 1816 | self._unpack_jobs[state.target] = None |
| 1817 | continue |
| 1818 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1819 | if not job.done: |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1820 | self._build_jobs[target] = job |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1821 | self._Print("Started %s (logged in %s)" % (target, job.filename)) |
| 1822 | continue |
| 1823 | |
| 1824 | # Print output of job |
| 1825 | if self._show_output or job.retcode != 0: |
| 1826 | self._print_queue.put(JobPrinter(job, unlink=True)) |
| 1827 | else: |
| 1828 | os.unlink(job.filename) |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1829 | del self._build_jobs[target] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1830 | |
| 1831 | seconds = time.time() - job.start_timestamp |
| 1832 | details = "%s (in %dm%.1fs)" % (target, seconds / 60, seconds % 60) |
| 1833 | |
| 1834 | # Complain if necessary. |
| 1835 | if job.retcode != 0: |
| 1836 | # Handle job failure. |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1837 | failed_count = self._failed_count.get(target, 0) |
| 1838 | if failed_count >= self._max_retries: |
| 1839 | # If this job has failed and can't be retried, give up. |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1840 | self._Print("Failed %s. Your build has failed." % details) |
| 1841 | else: |
| 1842 | # Queue up this build to try again after a long while. |
Brian Harring | 0be85c6 | 2012-03-17 19:52:12 -0700 | [diff] [blame] | 1843 | self._retry_queue.append(self._state_map[target]) |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1844 | self._failed_count[target] = failed_count + 1 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1845 | self._Print("Failed %s, retrying later." % details) |
| 1846 | else: |
David James | 32420cc | 2011-08-25 21:32:46 -0700 | [diff] [blame] | 1847 | self._Print("Completed %s" % details) |
| 1848 | |
| 1849 | # Mark as completed and unblock waiting ebuilds. |
| 1850 | self._Finish(target) |
| 1851 | |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1852 | if target in self._failed_count and self._retry_queue: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1853 | # If we have successfully retried a failed package, and there |
| 1854 | # are more failed packages, try the next one. We will only have |
| 1855 | # one retrying package actively running at a time. |
| 1856 | self._Retry() |
| 1857 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1858 | |
David James | 8c7e5e3 | 2011-06-28 11:26:03 -0700 | [diff] [blame] | 1859 | # Schedule pending jobs and print an update. |
| 1860 | self._ScheduleLoop() |
| 1861 | self._Status() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1862 | |
David James | e703d0f | 2012-01-12 16:27:45 -0800 | [diff] [blame] | 1863 | # If packages were retried, output a warning. |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1864 | if self._failed_count: |
David James | e703d0f | 2012-01-12 16:27:45 -0800 | [diff] [blame] | 1865 | self._Print("") |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1866 | self._Print("WARNING: The following packages failed once or more,") |
David James | e703d0f | 2012-01-12 16:27:45 -0800 | [diff] [blame] | 1867 | self._Print("but succeeded upon retry. This might indicate incorrect") |
| 1868 | self._Print("dependencies.") |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1869 | for pkg in self._failed_count.iterkeys(): |
David James | e703d0f | 2012-01-12 16:27:45 -0800 | [diff] [blame] | 1870 | self._Print(" %s" % pkg) |
| 1871 | self._Print("@@@STEP_WARNINGS@@@") |
| 1872 | self._Print("") |
| 1873 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1874 | # Tell child threads to exit. |
| 1875 | self._Print("Merge complete") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1876 | |
| 1877 | |
Brian Harring | 3067505 | 2012-02-29 12:18:22 -0800 | [diff] [blame] | 1878 | def main(argv): |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1879 | try: |
| 1880 | return real_main(argv) |
| 1881 | finally: |
| 1882 | # Work around multiprocessing sucking and not cleaning up after itself. |
| 1883 | # http://bugs.python.org/issue4106; |
| 1884 | # Step one; ensure GC is ran *prior* to the VM starting shutdown. |
| 1885 | gc.collect() |
| 1886 | # Step two; go looking for those threads and try to manually reap |
| 1887 | # them if we can. |
| 1888 | for x in threading.enumerate(): |
| 1889 | # Filter on the name, and ident; if ident is None, the thread |
| 1890 | # wasn't started. |
| 1891 | if x.name == 'QueueFeederThread' and x.ident is not None: |
| 1892 | x.join(1) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1893 | |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1894 | |
| 1895 | def real_main(argv): |
Brian Harring | 3067505 | 2012-02-29 12:18:22 -0800 | [diff] [blame] | 1896 | parallel_emerge_args = argv[:] |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1897 | deps = DepGraphGenerator() |
Brian Harring | 3067505 | 2012-02-29 12:18:22 -0800 | [diff] [blame] | 1898 | deps.Initialize(parallel_emerge_args) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1899 | emerge = deps.emerge |
| 1900 | |
| 1901 | if emerge.action is not None: |
Brian Harring | 3067505 | 2012-02-29 12:18:22 -0800 | [diff] [blame] | 1902 | argv = deps.ParseParallelEmergeArgs(argv) |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1903 | return emerge_main(argv) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1904 | elif not emerge.cmdline_packages: |
| 1905 | Usage() |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1906 | return 1 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1907 | |
| 1908 | # Unless we're in pretend mode, there's not much point running without |
| 1909 | # root access. We need to be able to install packages. |
| 1910 | # |
| 1911 | # NOTE: Even if you're running --pretend, it's a good idea to run |
| 1912 | # parallel_emerge with root access so that portage can write to the |
| 1913 | # dependency cache. This is important for performance. |
David James | 321490a | 2012-12-17 12:05:56 -0800 | [diff] [blame] | 1914 | if "--pretend" not in emerge.opts and portage.data.secpass < 2: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1915 | print("parallel_emerge: superuser access is required.") |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1916 | return 1 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1917 | |
| 1918 | if "--quiet" not in emerge.opts: |
| 1919 | cmdline_packages = " ".join(emerge.cmdline_packages) |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1920 | print("Starting fast-emerge.") |
| 1921 | print(" Building package %s on %s" % (cmdline_packages, |
Gilad Arnold | 05f94b0 | 2015-05-22 10:41:05 -0700 | [diff] [blame] | 1922 | deps.sysroot or "root")) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1923 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 1924 | deps_tree, deps_info = deps.GenDependencyTree() |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1925 | |
| 1926 | # You want me to be verbose? I'll give you two trees! Twice as much value. |
| 1927 | if "--tree" in emerge.opts and "--verbose" in emerge.opts: |
| 1928 | deps.PrintTree(deps_tree) |
| 1929 | |
David James | 386ccd1 | 2011-05-04 20:17:42 -0700 | [diff] [blame] | 1930 | deps_graph = deps.GenDependencyGraph(deps_tree, deps_info) |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1931 | |
| 1932 | # OK, time to print out our progress so far. |
| 1933 | deps.PrintInstallPlan(deps_graph) |
| 1934 | if "--tree" in emerge.opts: |
| 1935 | PrintDepsMap(deps_graph) |
| 1936 | |
| 1937 | # Are we upgrading portage? If so, and there are more packages to merge, |
| 1938 | # schedule a restart of parallel_emerge to merge the rest. This ensures that |
| 1939 | # we pick up all updates to portage settings before merging any more |
| 1940 | # packages. |
| 1941 | portage_upgrade = False |
| 1942 | root = emerge.settings["ROOT"] |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 1943 | # pylint: disable=W0212 |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1944 | if root == "/": |
Gilad Arnold | cead28a | 2015-05-22 10:45:02 -0700 | [diff] [blame] | 1945 | final_db = emerge.depgraph._dynamic_config._filtered_trees[root]['graph_db'] |
Mike Frysinger | 3fb56ef | 2018-01-05 19:00:04 -0500 | [diff] [blame] | 1946 | for db_pkg in final_db.cp_list("sys-apps/portage"): |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1947 | portage_pkg = deps_graph.get(db_pkg.cpv) |
David James | 0ff16f2 | 2012-11-02 14:18:07 -0700 | [diff] [blame] | 1948 | if portage_pkg: |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1949 | portage_upgrade = True |
| 1950 | if "--quiet" not in emerge.opts: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1951 | print("Upgrading portage first, then restarting...") |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1952 | |
David James | 0ff16f2 | 2012-11-02 14:18:07 -0700 | [diff] [blame] | 1953 | # Upgrade Portage first, then the rest of the packages. |
| 1954 | # |
| 1955 | # In order to grant the child permission to run setsid, we need to run sudo |
| 1956 | # again. We preserve SUDO_USER here in case an ebuild depends on it. |
| 1957 | if portage_upgrade: |
| 1958 | # Calculate what arguments to use when re-invoking. |
| 1959 | args = ["sudo", "-E", "SUDO_USER=%s" % os.environ.get("SUDO_USER", "")] |
| 1960 | args += [os.path.abspath(sys.argv[0])] + parallel_emerge_args |
| 1961 | args += ["--exclude=sys-apps/portage"] |
| 1962 | |
| 1963 | # First upgrade Portage. |
| 1964 | passthrough_args = ("--quiet", "--pretend", "--verbose") |
| 1965 | emerge_args = [k for k in emerge.opts if k in passthrough_args] |
| 1966 | ret = emerge_main(emerge_args + ["portage"]) |
| 1967 | if ret != 0: |
| 1968 | return ret |
| 1969 | |
| 1970 | # Now upgrade the rest. |
| 1971 | os.execvp(args[0], args) |
| 1972 | |
Bertrand SIMONNET | c03c8ee | 2014-12-10 17:02:55 -0800 | [diff] [blame] | 1973 | # Attempt to solve crbug.com/433482 |
| 1974 | # The file descriptor error appears only when getting userpriv_groups |
| 1975 | # (lazily generated). Loading userpriv_groups here will reduce the number of |
| 1976 | # calls from few hundreds to one. |
| 1977 | portage.data._get_global('userpriv_groups') |
| 1978 | |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1979 | # Run the queued emerges. |
Thiago Goncales | f4acc42 | 2013-07-17 10:26:35 -0700 | [diff] [blame] | 1980 | scheduler = EmergeQueue(deps_graph, emerge, deps.package_db, deps.show_output, |
Bertrand SIMONNET | 411945d | 2015-05-20 17:23:28 -0700 | [diff] [blame] | 1981 | deps.unpack_only, deps.max_retries) |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1982 | try: |
| 1983 | scheduler.Run() |
| 1984 | finally: |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 1985 | # pylint: disable=W0212 |
Brian Harring | a43f595 | 2012-04-12 01:19:34 -0700 | [diff] [blame] | 1986 | scheduler._Shutdown() |
David James | 97ce890 | 2011-08-16 09:51:05 -0700 | [diff] [blame] | 1987 | scheduler = None |
David James | fcb70ef | 2011-02-02 16:02:30 -0800 | [diff] [blame] | 1988 | |
Mike Frysinger | d20a6e2 | 2012-10-04 19:01:10 -0400 | [diff] [blame] | 1989 | clean_logs(emerge.settings) |
| 1990 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 1991 | print("Done") |
Brian Harring | 8294d65 | 2012-05-23 02:20:52 -0700 | [diff] [blame] | 1992 | return 0 |