blob: b91cb9bbd2b9a6bcca0e8cfb0bcaa8ed98407e07 [file] [log] [blame]
David Jamesfcb70ef2011-02-02 16:02:30 -08001#!/usr/bin/python2.6
Mike Frysinger0a647fc2012-08-06 14:36:05 -04002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
David Jamesfcb70ef2011-02-02 16:02:30 -08003# 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
8Usage:
David James386ccd12011-05-04 20:17:42 -07009 ./parallel_emerge [--board=BOARD] [--workon=PKGS]
David Jamesfcb70ef2011-02-02 16:02:30 -080010 [--force-remote-binary=PKGS] [emerge args] package
11
David James78b6cd92012-04-02 21:36:12 -070012This script runs multiple emerge processes in parallel, using appropriate
13Portage APIs. It is faster than standard emerge because it has a
14multiprocess model instead of an asynchronous model.
David Jamesfcb70ef2011-02-02 16:02:30 -080015"""
16
17import codecs
18import copy
19import errno
Brian Harring8294d652012-05-23 02:20:52 -070020import gc
David James8c7e5e32011-06-28 11:26:03 -070021import heapq
David Jamesfcb70ef2011-02-02 16:02:30 -080022import multiprocessing
23import os
24import Queue
David Jamesfcb70ef2011-02-02 16:02:30 -080025import signal
26import sys
27import tempfile
Brian Harring8294d652012-05-23 02:20:52 -070028import threading
David Jamesfcb70ef2011-02-02 16:02:30 -080029import time
30import traceback
David Jamesfcb70ef2011-02-02 16:02:30 -080031
32# If PORTAGE_USERNAME isn't specified, scrape it from the $HOME variable. On
33# Chromium OS, the default "portage" user doesn't have the necessary
34# permissions. It'd be easier if we could default to $USERNAME, but $USERNAME
35# is "root" here because we get called through sudo.
36#
37# We need to set this before importing any portage modules, because portage
38# looks up "PORTAGE_USERNAME" at import time.
39#
40# NOTE: .bashrc sets PORTAGE_USERNAME = $USERNAME, so most people won't
41# encounter this case unless they have an old chroot or blow away the
42# environment by running sudo without the -E specifier.
43if "PORTAGE_USERNAME" not in os.environ:
44 homedir = os.environ.get("HOME")
45 if homedir:
46 os.environ["PORTAGE_USERNAME"] = os.path.basename(homedir)
47
48# Portage doesn't expose dependency trees in its public API, so we have to
49# make use of some private APIs here. These modules are found under
50# /usr/lib/portage/pym/.
51#
52# TODO(davidjames): Update Portage to expose public APIs for these features.
53from _emerge.actions import adjust_configs
54from _emerge.actions import load_emerge_config
55from _emerge.create_depgraph_params import create_depgraph_params
David James386ccd12011-05-04 20:17:42 -070056from _emerge.depgraph import backtrack_depgraph
Mike Frysinger901eaad2012-10-10 18:18:03 -040057try:
58 from _emerge.main import clean_logs
59except ImportError:
60 # Older portage versions did not provide clean_logs, so stub it.
61 # We need this if running in an older chroot that hasn't yet upgraded
62 # the portage version.
63 clean_logs = lambda x: None
David Jamesfcb70ef2011-02-02 16:02:30 -080064from _emerge.main import emerge_main
65from _emerge.main import parse_opts
66from _emerge.Package import Package
67from _emerge.Scheduler import Scheduler
David Jamesfcb70ef2011-02-02 16:02:30 -080068from _emerge.stdout_spinner import stdout_spinner
David James386ccd12011-05-04 20:17:42 -070069from portage._global_updates import _global_updates
David Jamesfcb70ef2011-02-02 16:02:30 -080070import portage
71import portage.debug
David Jamesfcb70ef2011-02-02 16:02:30 -080072
David Jamesfcb70ef2011-02-02 16:02:30 -080073def Usage():
74 """Print usage."""
75 print "Usage:"
David James386ccd12011-05-04 20:17:42 -070076 print " ./parallel_emerge [--board=BOARD] [--workon=PKGS]"
David Jamesfcb70ef2011-02-02 16:02:30 -080077 print " [--rebuild] [emerge args] package"
78 print
79 print "Packages specified as workon packages are always built from source."
David Jamesfcb70ef2011-02-02 16:02:30 -080080 print
81 print "The --workon argument is mainly useful when you want to build and"
82 print "install packages that you are working on unconditionally, but do not"
83 print "to have to rev the package to indicate you want to build it from"
84 print "source. The build_packages script will automatically supply the"
85 print "workon argument to emerge, ensuring that packages selected using"
86 print "cros-workon are rebuilt."
87 print
88 print "The --rebuild option rebuilds packages whenever their dependencies"
89 print "are changed. This ensures that your build is correct."
David Jamesfcb70ef2011-02-02 16:02:30 -080090
91
David Jamesfcb70ef2011-02-02 16:02:30 -080092# Global start time
93GLOBAL_START = time.time()
94
David James7358d032011-05-19 10:40:03 -070095# Whether process has been killed by a signal.
96KILLED = multiprocessing.Event()
97
David Jamesfcb70ef2011-02-02 16:02:30 -080098
99class EmergeData(object):
100 """This simple struct holds various emerge variables.
101
102 This struct helps us easily pass emerge variables around as a unit.
103 These variables are used for calculating dependencies and installing
104 packages.
105 """
106
David Jamesbf1e3442011-05-28 07:44:20 -0700107 __slots__ = ["action", "cmdline_packages", "depgraph", "favorites",
108 "mtimedb", "opts", "root_config", "scheduler_graph",
109 "settings", "spinner", "trees"]
David Jamesfcb70ef2011-02-02 16:02:30 -0800110
111 def __init__(self):
112 # The action the user requested. If the user is installing packages, this
113 # is None. If the user is doing anything other than installing packages,
114 # this will contain the action name, which will map exactly to the
115 # long-form name of the associated emerge option.
116 #
117 # Example: If you call parallel_emerge --unmerge package, the action name
118 # will be "unmerge"
119 self.action = None
120
121 # The list of packages the user passed on the command-line.
122 self.cmdline_packages = None
123
124 # The emerge dependency graph. It'll contain all the packages involved in
125 # this merge, along with their versions.
126 self.depgraph = None
127
David Jamesbf1e3442011-05-28 07:44:20 -0700128 # The list of candidates to add to the world file.
129 self.favorites = None
130
David Jamesfcb70ef2011-02-02 16:02:30 -0800131 # A dict of the options passed to emerge. This dict has been cleaned up
132 # a bit by parse_opts, so that it's a bit easier for the emerge code to
133 # look at the options.
134 #
135 # Emerge takes a few shortcuts in its cleanup process to make parsing of
136 # the options dict easier. For example, if you pass in "--usepkg=n", the
137 # "--usepkg" flag is just left out of the dictionary altogether. Because
138 # --usepkg=n is the default, this makes parsing easier, because emerge
139 # can just assume that if "--usepkg" is in the dictionary, it's enabled.
140 #
141 # These cleanup processes aren't applied to all options. For example, the
142 # --with-bdeps flag is passed in as-is. For a full list of the cleanups
143 # applied by emerge, see the parse_opts function in the _emerge.main
144 # package.
145 self.opts = None
146
147 # A dictionary used by portage to maintain global state. This state is
148 # loaded from disk when portage starts up, and saved to disk whenever we
149 # call mtimedb.commit().
150 #
151 # This database contains information about global updates (i.e., what
152 # version of portage we have) and what we're currently doing. Portage
153 # saves what it is currently doing in this database so that it can be
154 # resumed when you call it with the --resume option.
155 #
156 # parallel_emerge does not save what it is currently doing in the mtimedb,
157 # so we do not support the --resume option.
158 self.mtimedb = None
159
160 # The portage configuration for our current root. This contains the portage
161 # settings (see below) and the three portage trees for our current root.
162 # (The three portage trees are explained below, in the documentation for
163 # the "trees" member.)
164 self.root_config = None
165
166 # The scheduler graph is used by emerge to calculate what packages to
167 # install. We don't actually install any deps, so this isn't really used,
168 # but we pass it in to the Scheduler object anyway.
169 self.scheduler_graph = None
170
171 # Portage settings for our current session. Most of these settings are set
172 # in make.conf inside our current install root.
173 self.settings = None
174
175 # The spinner, which spews stuff to stdout to indicate that portage is
176 # doing something. We maintain our own spinner, so we set the portage
177 # spinner to "silent" mode.
178 self.spinner = None
179
180 # The portage trees. There are separate portage trees for each root. To get
181 # the portage tree for the current root, you can look in self.trees[root],
182 # where root = self.settings["ROOT"].
183 #
184 # In each root, there are three trees: vartree, porttree, and bintree.
185 # - vartree: A database of the currently-installed packages.
186 # - porttree: A database of ebuilds, that can be used to build packages.
187 # - bintree: A database of binary packages.
188 self.trees = None
189
190
191class DepGraphGenerator(object):
192 """Grab dependency information about packages from portage.
193
194 Typical usage:
195 deps = DepGraphGenerator()
196 deps.Initialize(sys.argv[1:])
197 deps_tree, deps_info = deps.GenDependencyTree()
198 deps_graph = deps.GenDependencyGraph(deps_tree, deps_info)
199 deps.PrintTree(deps_tree)
200 PrintDepsMap(deps_graph)
201 """
202
David James386ccd12011-05-04 20:17:42 -0700203 __slots__ = ["board", "emerge", "package_db", "show_output"]
David Jamesfcb70ef2011-02-02 16:02:30 -0800204
205 def __init__(self):
206 self.board = None
207 self.emerge = EmergeData()
David Jamesfcb70ef2011-02-02 16:02:30 -0800208 self.package_db = {}
David Jamesfcb70ef2011-02-02 16:02:30 -0800209 self.show_output = False
David Jamesfcb70ef2011-02-02 16:02:30 -0800210
211 def ParseParallelEmergeArgs(self, argv):
212 """Read the parallel emerge arguments from the command-line.
213
214 We need to be compatible with emerge arg format. We scrape arguments that
215 are specific to parallel_emerge, and pass through the rest directly to
216 emerge.
217 Args:
218 argv: arguments list
219 Returns:
220 Arguments that don't belong to parallel_emerge
221 """
222 emerge_args = []
223 for arg in argv:
224 # Specifically match arguments that are specific to parallel_emerge, and
225 # pass through the rest.
226 if arg.startswith("--board="):
227 self.board = arg.replace("--board=", "")
228 elif arg.startswith("--workon="):
229 workon_str = arg.replace("--workon=", "")
David James7a1ea4b2011-10-13 15:06:41 -0700230 emerge_args.append("--reinstall-atoms=%s" % workon_str)
231 emerge_args.append("--usepkg-exclude=%s" % workon_str)
David Jamesfcb70ef2011-02-02 16:02:30 -0800232 elif arg.startswith("--force-remote-binary="):
233 force_remote_binary = arg.replace("--force-remote-binary=", "")
David James7a1ea4b2011-10-13 15:06:41 -0700234 emerge_args.append("--useoldpkg-atoms=%s" % force_remote_binary)
David Jamesfcb70ef2011-02-02 16:02:30 -0800235 elif arg == "--show-output":
236 self.show_output = True
David James386ccd12011-05-04 20:17:42 -0700237 elif arg == "--rebuild":
David James7a1ea4b2011-10-13 15:06:41 -0700238 emerge_args.append("--rebuild-if-unbuilt")
David Jamesfcb70ef2011-02-02 16:02:30 -0800239 else:
240 # Not one of our options, so pass through to emerge.
241 emerge_args.append(arg)
242
David James386ccd12011-05-04 20:17:42 -0700243 # These packages take a really long time to build, so, for expediency, we
244 # are blacklisting them from automatic rebuilds because one of their
245 # dependencies needs to be recompiled.
246 for pkg in ("chromeos-base/chromeos-chrome", "media-plugins/o3d",
247 "dev-java/icedtea"):
David James7a1ea4b2011-10-13 15:06:41 -0700248 emerge_args.append("--rebuild-exclude=%s" % pkg)
David Jamesfcb70ef2011-02-02 16:02:30 -0800249
250 return emerge_args
251
252 def Initialize(self, args):
253 """Initializer. Parses arguments and sets up portage state."""
254
255 # Parse and strip out args that are just intended for parallel_emerge.
256 emerge_args = self.ParseParallelEmergeArgs(args)
257
258 # Setup various environment variables based on our current board. These
259 # variables are normally setup inside emerge-${BOARD}, but since we don't
260 # call that script, we have to set it up here. These variables serve to
261 # point our tools at /build/BOARD and to setup cross compiles to the
262 # appropriate board as configured in toolchain.conf.
263 if self.board:
264 os.environ["PORTAGE_CONFIGROOT"] = "/build/" + self.board
265 os.environ["PORTAGE_SYSROOT"] = "/build/" + self.board
266 os.environ["SYSROOT"] = "/build/" + self.board
David Jamesfcb70ef2011-02-02 16:02:30 -0800267
268 # Although CHROMEOS_ROOT isn't specific to boards, it's normally setup
269 # inside emerge-${BOARD}, so we set it up here for compatibility. It
270 # will be going away soon as we migrate to CROS_WORKON_SRCROOT.
271 os.environ.setdefault("CHROMEOS_ROOT", os.environ["HOME"] + "/trunk")
272
273 # Turn off interactive delays
274 os.environ["EBEEP_IGNORE"] = "1"
275 os.environ["EPAUSE_IGNORE"] = "1"
Mike Frysinger0a647fc2012-08-06 14:36:05 -0400276 os.environ["CLEAN_DELAY"] = "0"
David Jamesfcb70ef2011-02-02 16:02:30 -0800277
278 # Parse the emerge options.
David Jamesea3ca332011-05-26 11:48:29 -0700279 action, opts, cmdline_packages = parse_opts(emerge_args, silent=True)
David Jamesfcb70ef2011-02-02 16:02:30 -0800280
281 # Set environment variables based on options. Portage normally sets these
282 # environment variables in emerge_main, but we can't use that function,
283 # because it also does a bunch of other stuff that we don't want.
284 # TODO(davidjames): Patch portage to move this logic into a function we can
285 # reuse here.
286 if "--debug" in opts:
287 os.environ["PORTAGE_DEBUG"] = "1"
288 if "--config-root" in opts:
289 os.environ["PORTAGE_CONFIGROOT"] = opts["--config-root"]
290 if "--root" in opts:
291 os.environ["ROOT"] = opts["--root"]
292 if "--accept-properties" in opts:
293 os.environ["ACCEPT_PROPERTIES"] = opts["--accept-properties"]
294
David Jamesfcb70ef2011-02-02 16:02:30 -0800295 # If we're installing packages to the board, and we're not using the
David James927a56d2012-04-03 11:26:39 -0700296 # official flag, we can disable vardb locks. This is safe because we
297 # only run up to one instance of parallel_emerge in parallel.
David Jamesfcb70ef2011-02-02 16:02:30 -0800298 if self.board and os.environ.get("CHROMEOS_OFFICIAL") != "1":
299 os.environ.setdefault("PORTAGE_LOCKS", "false")
David Jamesfcb70ef2011-02-02 16:02:30 -0800300
301 # Now that we've setup the necessary environment variables, we can load the
302 # emerge config from disk.
303 settings, trees, mtimedb = load_emerge_config()
304
David Jamesea3ca332011-05-26 11:48:29 -0700305 # Add in EMERGE_DEFAULT_OPTS, if specified.
306 tmpcmdline = []
307 if "--ignore-default-opts" not in opts:
308 tmpcmdline.extend(settings["EMERGE_DEFAULT_OPTS"].split())
309 tmpcmdline.extend(emerge_args)
310 action, opts, cmdline_packages = parse_opts(tmpcmdline)
311
312 # If we're installing to the board, we want the --root-deps option so that
313 # portage will install the build dependencies to that location as well.
314 if self.board:
315 opts.setdefault("--root-deps", True)
316
David Jamesfcb70ef2011-02-02 16:02:30 -0800317 # Check whether our portage tree is out of date. Typically, this happens
318 # when you're setting up a new portage tree, such as in setup_board and
319 # make_chroot. In that case, portage applies a bunch of global updates
320 # here. Once the updates are finished, we need to commit any changes
321 # that the global update made to our mtimedb, and reload the config.
322 #
323 # Portage normally handles this logic in emerge_main, but again, we can't
324 # use that function here.
325 if _global_updates(trees, mtimedb["updates"]):
326 mtimedb.commit()
327 settings, trees, mtimedb = load_emerge_config(trees=trees)
328
329 # Setup implied options. Portage normally handles this logic in
330 # emerge_main.
331 if "--buildpkgonly" in opts or "buildpkg" in settings.features:
332 opts.setdefault("--buildpkg", True)
333 if "--getbinpkgonly" in opts:
334 opts.setdefault("--usepkgonly", True)
335 opts.setdefault("--getbinpkg", True)
336 if "getbinpkg" in settings.features:
337 # Per emerge_main, FEATURES=getbinpkg overrides --getbinpkg=n
338 opts["--getbinpkg"] = True
339 if "--getbinpkg" in opts or "--usepkgonly" in opts:
340 opts.setdefault("--usepkg", True)
341 if "--fetch-all-uri" in opts:
342 opts.setdefault("--fetchonly", True)
343 if "--skipfirst" in opts:
344 opts.setdefault("--resume", True)
345 if "--buildpkgonly" in opts:
346 # --buildpkgonly will not merge anything, so it overrides all binary
347 # package options.
348 for opt in ("--getbinpkg", "--getbinpkgonly",
349 "--usepkg", "--usepkgonly"):
350 opts.pop(opt, None)
351 if (settings.get("PORTAGE_DEBUG", "") == "1" and
352 "python-trace" in settings.features):
353 portage.debug.set_trace(True)
354
355 # Complain about unsupported options
David James386ccd12011-05-04 20:17:42 -0700356 for opt in ("--ask", "--ask-enter-invalid", "--resume", "--skipfirst"):
David Jamesfcb70ef2011-02-02 16:02:30 -0800357 if opt in opts:
358 print "%s is not supported by parallel_emerge" % opt
359 sys.exit(1)
360
361 # Make emerge specific adjustments to the config (e.g. colors!)
362 adjust_configs(opts, trees)
363
364 # Save our configuration so far in the emerge object
365 emerge = self.emerge
366 emerge.action, emerge.opts = action, opts
367 emerge.settings, emerge.trees, emerge.mtimedb = settings, trees, mtimedb
368 emerge.cmdline_packages = cmdline_packages
369 root = settings["ROOT"]
370 emerge.root_config = trees[root]["root_config"]
371
David James386ccd12011-05-04 20:17:42 -0700372 if "--usepkg" in opts:
David Jamesfcb70ef2011-02-02 16:02:30 -0800373 emerge.trees[root]["bintree"].populate("--getbinpkg" in opts)
374
David Jamesfcb70ef2011-02-02 16:02:30 -0800375 def CreateDepgraph(self, emerge, packages):
376 """Create an emerge depgraph object."""
377 # Setup emerge options.
378 emerge_opts = emerge.opts.copy()
379
David James386ccd12011-05-04 20:17:42 -0700380 # Ask portage to build a dependency graph. with the options we specified
381 # above.
David Jamesfcb70ef2011-02-02 16:02:30 -0800382 params = create_depgraph_params(emerge_opts, emerge.action)
David Jamesbf1e3442011-05-28 07:44:20 -0700383 success, depgraph, favorites = backtrack_depgraph(
David James386ccd12011-05-04 20:17:42 -0700384 emerge.settings, emerge.trees, emerge_opts, params, emerge.action,
385 packages, emerge.spinner)
386 emerge.depgraph = depgraph
David Jamesfcb70ef2011-02-02 16:02:30 -0800387
David James386ccd12011-05-04 20:17:42 -0700388 # Is it impossible to honor the user's request? Bail!
389 if not success:
390 depgraph.display_problems()
391 sys.exit(1)
David Jamesfcb70ef2011-02-02 16:02:30 -0800392
393 emerge.depgraph = depgraph
David Jamesbf1e3442011-05-28 07:44:20 -0700394 emerge.favorites = favorites
David Jamesfcb70ef2011-02-02 16:02:30 -0800395
David Jamesdeebd692011-05-09 17:02:52 -0700396 # Prime and flush emerge caches.
397 root = emerge.settings["ROOT"]
398 vardb = emerge.trees[root]["vartree"].dbapi
David James0bdc5de2011-05-12 16:22:26 -0700399 if "--pretend" not in emerge.opts:
400 vardb.counter_tick()
David Jamesdeebd692011-05-09 17:02:52 -0700401 vardb.flush_cache()
402
David James386ccd12011-05-04 20:17:42 -0700403 def GenDependencyTree(self):
David Jamesfcb70ef2011-02-02 16:02:30 -0800404 """Get dependency tree info from emerge.
405
David Jamesfcb70ef2011-02-02 16:02:30 -0800406 Returns:
407 Dependency tree
408 """
409 start = time.time()
410
411 emerge = self.emerge
412
413 # Create a list of packages to merge
414 packages = set(emerge.cmdline_packages[:])
David Jamesfcb70ef2011-02-02 16:02:30 -0800415
416 # Tell emerge to be quiet. We print plenty of info ourselves so we don't
417 # need any extra output from portage.
418 portage.util.noiselimit = -1
419
420 # My favorite feature: The silent spinner. It doesn't spin. Ever.
421 # I'd disable the colors by default too, but they look kind of cool.
422 emerge.spinner = stdout_spinner()
423 emerge.spinner.update = emerge.spinner.update_quiet
424
425 if "--quiet" not in emerge.opts:
426 print "Calculating deps..."
427
428 self.CreateDepgraph(emerge, packages)
429 depgraph = emerge.depgraph
430
431 # Build our own tree from the emerge digraph.
432 deps_tree = {}
433 digraph = depgraph._dynamic_config.digraph
David James3f778802011-08-25 19:31:45 -0700434 root = emerge.settings["ROOT"]
435 final_db = depgraph._dynamic_config.mydbapi[root]
David Jamesfcb70ef2011-02-02 16:02:30 -0800436 for node, node_deps in digraph.nodes.items():
437 # Calculate dependency packages that need to be installed first. Each
438 # child on the digraph is a dependency. The "operation" field specifies
439 # what we're doing (e.g. merge, uninstall, etc.). The "priorities" array
440 # contains the type of dependency (e.g. build, runtime, runtime_post,
441 # etc.)
442 #
David Jamesfcb70ef2011-02-02 16:02:30 -0800443 # Portage refers to the identifiers for packages as a CPV. This acronym
444 # stands for Component/Path/Version.
445 #
446 # Here's an example CPV: chromeos-base/power_manager-0.0.1-r1
447 # Split up, this CPV would be:
448 # C -- Component: chromeos-base
449 # P -- Path: power_manager
450 # V -- Version: 0.0.1-r1
451 #
452 # We just refer to CPVs as packages here because it's easier.
453 deps = {}
454 for child, priorities in node_deps[0].items():
David James3f778802011-08-25 19:31:45 -0700455 if isinstance(child, Package) and child.root == root:
456 cpv = str(child.cpv)
457 action = str(child.operation)
458
459 # If we're uninstalling a package, check whether Portage is
460 # installing a replacement. If so, just depend on the installation
461 # of the new package, because the old package will automatically
462 # be uninstalled at that time.
463 if action == "uninstall":
464 for pkg in final_db.match_pkgs(child.slot_atom):
465 cpv = str(pkg.cpv)
466 action = "merge"
467 break
468
469 deps[cpv] = dict(action=action,
470 deptypes=[str(x) for x in priorities],
471 deps={})
David Jamesfcb70ef2011-02-02 16:02:30 -0800472
473 # We've built our list of deps, so we can add our package to the tree.
David James3f778802011-08-25 19:31:45 -0700474 if isinstance(node, Package) and node.root == root:
David Jamesfcb70ef2011-02-02 16:02:30 -0800475 deps_tree[str(node.cpv)] = dict(action=str(node.operation),
476 deps=deps)
477
David Jamesfcb70ef2011-02-02 16:02:30 -0800478 # Ask portage for its install plan, so that we can only throw out
David James386ccd12011-05-04 20:17:42 -0700479 # dependencies that portage throws out.
David Jamesfcb70ef2011-02-02 16:02:30 -0800480 deps_info = {}
481 for pkg in depgraph.altlist():
482 if isinstance(pkg, Package):
David James3f778802011-08-25 19:31:45 -0700483 assert pkg.root == root
David Jamesfcb70ef2011-02-02 16:02:30 -0800484 self.package_db[pkg.cpv] = pkg
485
David Jamesfcb70ef2011-02-02 16:02:30 -0800486 # Save off info about the package
David James386ccd12011-05-04 20:17:42 -0700487 deps_info[str(pkg.cpv)] = {"idx": len(deps_info)}
David Jamesfcb70ef2011-02-02 16:02:30 -0800488
489 seconds = time.time() - start
490 if "--quiet" not in emerge.opts:
491 print "Deps calculated in %dm%.1fs" % (seconds / 60, seconds % 60)
492
493 return deps_tree, deps_info
494
495 def PrintTree(self, deps, depth=""):
496 """Print the deps we have seen in the emerge output.
497
498 Args:
499 deps: Dependency tree structure.
500 depth: Allows printing the tree recursively, with indentation.
501 """
502 for entry in sorted(deps):
503 action = deps[entry]["action"]
504 print "%s %s (%s)" % (depth, entry, action)
505 self.PrintTree(deps[entry]["deps"], depth=depth + " ")
506
David James386ccd12011-05-04 20:17:42 -0700507 def GenDependencyGraph(self, deps_tree, deps_info):
David Jamesfcb70ef2011-02-02 16:02:30 -0800508 """Generate a doubly linked dependency graph.
509
510 Args:
511 deps_tree: Dependency tree structure.
512 deps_info: More details on the dependencies.
513 Returns:
514 Deps graph in the form of a dict of packages, with each package
515 specifying a "needs" list and "provides" list.
516 """
517 emerge = self.emerge
518 root = emerge.settings["ROOT"]
519
David Jamesfcb70ef2011-02-02 16:02:30 -0800520 # deps_map is the actual dependency graph.
521 #
522 # Each package specifies a "needs" list and a "provides" list. The "needs"
523 # list indicates which packages we depend on. The "provides" list
524 # indicates the reverse dependencies -- what packages need us.
525 #
526 # We also provide some other information in the dependency graph:
527 # - action: What we're planning on doing with this package. Generally,
528 # "merge", "nomerge", or "uninstall"
David Jamesfcb70ef2011-02-02 16:02:30 -0800529 deps_map = {}
530
531 def ReverseTree(packages):
532 """Convert tree to digraph.
533
534 Take the tree of package -> requirements and reverse it to a digraph of
535 buildable packages -> packages they unblock.
536 Args:
537 packages: Tree(s) of dependencies.
538 Returns:
539 Unsanitized digraph.
540 """
David James8c7e5e32011-06-28 11:26:03 -0700541 binpkg_phases = set(["setup", "preinst", "postinst"])
David James3f778802011-08-25 19:31:45 -0700542 needed_dep_types = set(["blocker", "buildtime", "runtime"])
David Jamesfcb70ef2011-02-02 16:02:30 -0800543 for pkg in packages:
544
545 # Create an entry for the package
546 action = packages[pkg]["action"]
David James8c7e5e32011-06-28 11:26:03 -0700547 default_pkg = {"needs": {}, "provides": set(), "action": action,
548 "nodeps": False, "binary": False}
David Jamesfcb70ef2011-02-02 16:02:30 -0800549 this_pkg = deps_map.setdefault(pkg, default_pkg)
550
David James8c7e5e32011-06-28 11:26:03 -0700551 if pkg in deps_info:
552 this_pkg["idx"] = deps_info[pkg]["idx"]
553
554 # If a package doesn't have any defined phases that might use the
555 # dependent packages (i.e. pkg_setup, pkg_preinst, or pkg_postinst),
556 # we can install this package before its deps are ready.
557 emerge_pkg = self.package_db.get(pkg)
558 if emerge_pkg and emerge_pkg.type_name == "binary":
559 this_pkg["binary"] = True
560 defined_phases = emerge_pkg.metadata.defined_phases
561 defined_binpkg_phases = binpkg_phases.intersection(defined_phases)
562 if not defined_binpkg_phases:
563 this_pkg["nodeps"] = True
564
David Jamesfcb70ef2011-02-02 16:02:30 -0800565 # Create entries for dependencies of this package first.
566 ReverseTree(packages[pkg]["deps"])
567
568 # Add dependencies to this package.
569 for dep, dep_item in packages[pkg]["deps"].iteritems():
David James8c7e5e32011-06-28 11:26:03 -0700570 # We only need to enforce strict ordering of dependencies if the
David James3f778802011-08-25 19:31:45 -0700571 # dependency is a blocker, or is a buildtime or runtime dependency.
572 # (I.e., ignored, optional, and runtime_post dependencies don't
573 # depend on ordering.)
David James8c7e5e32011-06-28 11:26:03 -0700574 dep_types = dep_item["deptypes"]
575 if needed_dep_types.intersection(dep_types):
576 deps_map[dep]["provides"].add(pkg)
577 this_pkg["needs"][dep] = "/".join(dep_types)
David Jamesfcb70ef2011-02-02 16:02:30 -0800578
David James3f778802011-08-25 19:31:45 -0700579 # If there's a blocker, Portage may need to move files from one
580 # package to another, which requires editing the CONTENTS files of
581 # both packages. To avoid race conditions while editing this file,
582 # the two packages must not be installed in parallel, so we can't
583 # safely ignore dependencies. See http://crosbug.com/19328
584 if "blocker" in dep_types:
585 this_pkg["nodeps"] = False
586
David Jamesfcb70ef2011-02-02 16:02:30 -0800587 def FindCycles():
588 """Find cycles in the dependency tree.
589
590 Returns:
591 A dict mapping cyclic packages to a dict of the deps that cause
592 cycles. For each dep that causes cycles, it returns an example
593 traversal of the graph that shows the cycle.
594 """
595
596 def FindCyclesAtNode(pkg, cycles, unresolved, resolved):
597 """Find cycles in cyclic dependencies starting at specified package.
598
599 Args:
600 pkg: Package identifier.
601 cycles: A dict mapping cyclic packages to a dict of the deps that
602 cause cycles. For each dep that causes cycles, it returns an
603 example traversal of the graph that shows the cycle.
604 unresolved: Nodes that have been visited but are not fully processed.
605 resolved: Nodes that have been visited and are fully processed.
606 """
607 pkg_cycles = cycles.get(pkg)
608 if pkg in resolved and not pkg_cycles:
609 # If we already looked at this package, and found no cyclic
610 # dependencies, we can stop now.
611 return
612 unresolved.append(pkg)
613 for dep in deps_map[pkg]["needs"]:
614 if dep in unresolved:
615 idx = unresolved.index(dep)
616 mycycle = unresolved[idx:] + [dep]
617 for i in range(len(mycycle) - 1):
618 pkg1, pkg2 = mycycle[i], mycycle[i+1]
619 cycles.setdefault(pkg1, {}).setdefault(pkg2, mycycle)
620 elif not pkg_cycles or dep not in pkg_cycles:
621 # Looks like we haven't seen this edge before.
622 FindCyclesAtNode(dep, cycles, unresolved, resolved)
623 unresolved.pop()
624 resolved.add(pkg)
625
626 cycles, unresolved, resolved = {}, [], set()
627 for pkg in deps_map:
628 FindCyclesAtNode(pkg, cycles, unresolved, resolved)
629 return cycles
630
David James386ccd12011-05-04 20:17:42 -0700631 def RemoveUnusedPackages():
David Jamesfcb70ef2011-02-02 16:02:30 -0800632 """Remove installed packages, propagating dependencies."""
David Jamesfcb70ef2011-02-02 16:02:30 -0800633 # Schedule packages that aren't on the install list for removal
634 rm_pkgs = set(deps_map.keys()) - set(deps_info.keys())
635
David Jamesfcb70ef2011-02-02 16:02:30 -0800636 # Remove the packages we don't want, simplifying the graph and making
637 # it easier for us to crack cycles.
638 for pkg in sorted(rm_pkgs):
639 this_pkg = deps_map[pkg]
640 needs = this_pkg["needs"]
641 provides = this_pkg["provides"]
642 for dep in needs:
643 dep_provides = deps_map[dep]["provides"]
644 dep_provides.update(provides)
645 dep_provides.discard(pkg)
646 dep_provides.discard(dep)
647 for target in provides:
648 target_needs = deps_map[target]["needs"]
649 target_needs.update(needs)
650 target_needs.pop(pkg, None)
651 target_needs.pop(target, None)
652 del deps_map[pkg]
653
654 def PrintCycleBreak(basedep, dep, mycycle):
655 """Print details about a cycle that we are planning on breaking.
656
657 We are breaking a cycle where dep needs basedep. mycycle is an
658 example cycle which contains dep -> basedep."""
659
David Jamesfcb70ef2011-02-02 16:02:30 -0800660 needs = deps_map[dep]["needs"]
661 depinfo = needs.get(basedep, "deleted")
David Jamesfcb70ef2011-02-02 16:02:30 -0800662
David James3f778802011-08-25 19:31:45 -0700663 # It's OK to swap install order for blockers, as long as the two
664 # packages aren't installed in parallel. If there is a cycle, then
665 # we know the packages depend on each other already, so we can drop the
666 # blocker safely without printing a warning.
667 if depinfo == "blocker":
668 return
669
David Jamesfcb70ef2011-02-02 16:02:30 -0800670 # Notify the user that we're breaking a cycle.
671 print "Breaking %s -> %s (%s)" % (dep, basedep, depinfo)
672
673 # Show cycle.
674 for i in range(len(mycycle) - 1):
675 pkg1, pkg2 = mycycle[i], mycycle[i+1]
676 needs = deps_map[pkg1]["needs"]
677 depinfo = needs.get(pkg2, "deleted")
678 if pkg1 == dep and pkg2 == basedep:
679 depinfo = depinfo + ", deleting"
680 print " %s -> %s (%s)" % (pkg1, pkg2, depinfo)
681
682 def SanitizeTree():
683 """Remove circular dependencies.
684
685 We prune all dependencies involved in cycles that go against the emerge
686 ordering. This has a nice property: we're guaranteed to merge
687 dependencies in the same order that portage does.
688
689 Because we don't treat any dependencies as "soft" unless they're killed
690 by a cycle, we pay attention to a larger number of dependencies when
691 merging. This hurts performance a bit, but helps reliability.
692 """
693 start = time.time()
694 cycles = FindCycles()
695 while cycles:
696 for dep, mycycles in cycles.iteritems():
697 for basedep, mycycle in mycycles.iteritems():
698 if deps_info[basedep]["idx"] >= deps_info[dep]["idx"]:
Matt Tennant08797302011-10-17 16:18:45 -0700699 if "--quiet" not in emerge.opts:
700 PrintCycleBreak(basedep, dep, mycycle)
David Jamesfcb70ef2011-02-02 16:02:30 -0800701 del deps_map[dep]["needs"][basedep]
702 deps_map[basedep]["provides"].remove(dep)
703 cycles = FindCycles()
704 seconds = time.time() - start
705 if "--quiet" not in emerge.opts and seconds >= 0.1:
706 print "Tree sanitized in %dm%.1fs" % (seconds / 60, seconds % 60)
707
David James8c7e5e32011-06-28 11:26:03 -0700708 def FindRecursiveProvides(pkg, seen):
709 """Find all nodes that require a particular package.
710
711 Assumes that graph is acyclic.
712
713 Args:
714 pkg: Package identifier.
715 seen: Nodes that have been visited so far.
716 """
717 if pkg in seen:
718 return
719 seen.add(pkg)
720 info = deps_map[pkg]
721 info["tprovides"] = info["provides"].copy()
722 for dep in info["provides"]:
723 FindRecursiveProvides(dep, seen)
724 info["tprovides"].update(deps_map[dep]["tprovides"])
725
David Jamesa22906f2011-05-04 19:53:26 -0700726 ReverseTree(deps_tree)
David Jamesa22906f2011-05-04 19:53:26 -0700727
David James386ccd12011-05-04 20:17:42 -0700728 # We need to remove unused packages so that we can use the dependency
729 # ordering of the install process to show us what cycles to crack.
730 RemoveUnusedPackages()
David Jamesfcb70ef2011-02-02 16:02:30 -0800731 SanitizeTree()
David James8c7e5e32011-06-28 11:26:03 -0700732 seen = set()
733 for pkg in deps_map:
734 FindRecursiveProvides(pkg, seen)
David Jamesfcb70ef2011-02-02 16:02:30 -0800735 return deps_map
736
737 def PrintInstallPlan(self, deps_map):
738 """Print an emerge-style install plan.
739
740 The install plan lists what packages we're installing, in order.
741 It's useful for understanding what parallel_emerge is doing.
742
743 Args:
744 deps_map: The dependency graph.
745 """
746
747 def InstallPlanAtNode(target, deps_map):
748 nodes = []
749 nodes.append(target)
750 for dep in deps_map[target]["provides"]:
751 del deps_map[dep]["needs"][target]
752 if not deps_map[dep]["needs"]:
753 nodes.extend(InstallPlanAtNode(dep, deps_map))
754 return nodes
755
756 deps_map = copy.deepcopy(deps_map)
757 install_plan = []
758 plan = set()
759 for target, info in deps_map.iteritems():
760 if not info["needs"] and target not in plan:
761 for item in InstallPlanAtNode(target, deps_map):
762 plan.add(item)
763 install_plan.append(self.package_db[item])
764
765 for pkg in plan:
766 del deps_map[pkg]
767
768 if deps_map:
769 print "Cyclic dependencies:", " ".join(deps_map)
770 PrintDepsMap(deps_map)
771 sys.exit(1)
772
773 self.emerge.depgraph.display(install_plan)
774
775
776def PrintDepsMap(deps_map):
777 """Print dependency graph, for each package list it's prerequisites."""
778 for i in sorted(deps_map):
779 print "%s: (%s) needs" % (i, deps_map[i]["action"])
780 needs = deps_map[i]["needs"]
781 for j in sorted(needs):
782 print " %s" % (j)
783 if not needs:
784 print " no dependencies"
785
786
787class EmergeJobState(object):
788 __slots__ = ["done", "filename", "last_notify_timestamp", "last_output_seek",
789 "last_output_timestamp", "pkgname", "retcode", "start_timestamp",
Brian Harring0be85c62012-03-17 19:52:12 -0700790 "target", "fetch_only"]
David Jamesfcb70ef2011-02-02 16:02:30 -0800791
792 def __init__(self, target, pkgname, done, filename, start_timestamp,
Brian Harring0be85c62012-03-17 19:52:12 -0700793 retcode=None, fetch_only=False):
David Jamesfcb70ef2011-02-02 16:02:30 -0800794
795 # The full name of the target we're building (e.g.
796 # chromeos-base/chromeos-0.0.1-r60)
797 self.target = target
798
799 # The short name of the target we're building (e.g. chromeos-0.0.1-r60)
800 self.pkgname = pkgname
801
802 # Whether the job is done. (True if the job is done; false otherwise.)
803 self.done = done
804
805 # The filename where output is currently stored.
806 self.filename = filename
807
808 # The timestamp of the last time we printed the name of the log file. We
809 # print this at the beginning of the job, so this starts at
810 # start_timestamp.
811 self.last_notify_timestamp = start_timestamp
812
813 # The location (in bytes) of the end of the last complete line we printed.
814 # This starts off at zero. We use this to jump to the right place when we
815 # print output from the same ebuild multiple times.
816 self.last_output_seek = 0
817
818 # The timestamp of the last time we printed output. Since we haven't
819 # printed output yet, this starts at zero.
820 self.last_output_timestamp = 0
821
822 # The return code of our job, if the job is actually finished.
823 self.retcode = retcode
824
Brian Harring0be85c62012-03-17 19:52:12 -0700825 # Was this just a fetch job?
826 self.fetch_only = fetch_only
827
David Jamesfcb70ef2011-02-02 16:02:30 -0800828 # The timestamp when our job started.
829 self.start_timestamp = start_timestamp
830
831
David James7358d032011-05-19 10:40:03 -0700832def KillHandler(signum, frame):
833 # Kill self and all subprocesses.
834 os.killpg(0, signal.SIGKILL)
835
David Jamesfcb70ef2011-02-02 16:02:30 -0800836def SetupWorkerSignals():
837 def ExitHandler(signum, frame):
David James7358d032011-05-19 10:40:03 -0700838 # Set KILLED flag.
839 KILLED.set()
David James13cead42011-05-18 16:22:01 -0700840
David James7358d032011-05-19 10:40:03 -0700841 # Remove our signal handlers so we don't get called recursively.
842 signal.signal(signal.SIGINT, KillHandler)
843 signal.signal(signal.SIGTERM, KillHandler)
David Jamesfcb70ef2011-02-02 16:02:30 -0800844
845 # Ensure that we exit quietly and cleanly, if possible, when we receive
846 # SIGTERM or SIGINT signals. By default, when the user hits CTRL-C, all
847 # of the child processes will print details about KeyboardInterrupt
848 # exceptions, which isn't very helpful.
849 signal.signal(signal.SIGINT, ExitHandler)
850 signal.signal(signal.SIGTERM, ExitHandler)
851
David James6b29d052012-11-02 10:27:27 -0700852def EmergeProcess(output, *args, **kwargs):
David James1ed3e252011-10-05 20:26:15 -0700853 """Merge a package in a subprocess.
854
855 Args:
David James1ed3e252011-10-05 20:26:15 -0700856 output: Temporary file to write output.
David James6b29d052012-11-02 10:27:27 -0700857 *args: Arguments to pass to Scheduler constructor.
858 **kwargs: Keyword arguments to pass to Scheduler constructor.
David James1ed3e252011-10-05 20:26:15 -0700859
860 Returns:
861 The exit code returned by the subprocess.
862 """
863 pid = os.fork()
864 if pid == 0:
865 try:
866 # Sanity checks.
867 if sys.stdout.fileno() != 1: raise Exception("sys.stdout.fileno() != 1")
868 if sys.stderr.fileno() != 2: raise Exception("sys.stderr.fileno() != 2")
869
870 # - Redirect 1 (stdout) and 2 (stderr) at our temporary file.
871 # - Redirect 0 to point at sys.stdin. In this case, sys.stdin
872 # points at a file reading os.devnull, because multiprocessing mucks
873 # with sys.stdin.
874 # - Leave the sys.stdin and output filehandles alone.
875 fd_pipes = {0: sys.stdin.fileno(),
876 1: output.fileno(),
877 2: output.fileno(),
878 sys.stdin.fileno(): sys.stdin.fileno(),
879 output.fileno(): output.fileno()}
880 portage.process._setup_pipes(fd_pipes)
881
882 # Portage doesn't like when sys.stdin.fileno() != 0, so point sys.stdin
883 # at the filehandle we just created in _setup_pipes.
884 if sys.stdin.fileno() != 0:
David James6b29d052012-11-02 10:27:27 -0700885 sys.__stdin__ = sys.stdin = os.fdopen(0, "r")
886
887 scheduler = Scheduler(*args, **kwargs)
888
889 # Enable blocker handling even though we're in --nodeps mode. This
890 # allows us to unmerge the blocker after we've merged the replacement.
891 scheduler._opts_ignore_blockers = frozenset()
David James1ed3e252011-10-05 20:26:15 -0700892
893 # Actually do the merge.
894 retval = scheduler.merge()
895
896 # We catch all exceptions here (including SystemExit, KeyboardInterrupt,
897 # etc) so as to ensure that we don't confuse the multiprocessing module,
898 # which expects that all forked children exit with os._exit().
899 except:
900 traceback.print_exc(file=output)
901 retval = 1
902 sys.stdout.flush()
903 sys.stderr.flush()
904 output.flush()
905 os._exit(retval)
906 else:
907 # Return the exit code of the subprocess.
908 return os.waitpid(pid, 0)[1]
David Jamesfcb70ef2011-02-02 16:02:30 -0800909
Brian Harring0be85c62012-03-17 19:52:12 -0700910def EmergeWorker(task_queue, job_queue, emerge, package_db, fetch_only=False):
David Jamesfcb70ef2011-02-02 16:02:30 -0800911 """This worker emerges any packages given to it on the task_queue.
912
913 Args:
914 task_queue: The queue of tasks for this worker to do.
915 job_queue: The queue of results from the worker.
916 emerge: An EmergeData() object.
917 package_db: A dict, mapping package ids to portage Package objects.
Brian Harring0be85c62012-03-17 19:52:12 -0700918 fetch_only: A bool, indicating if we should just fetch the target.
David Jamesfcb70ef2011-02-02 16:02:30 -0800919
920 It expects package identifiers to be passed to it via task_queue. When
921 a task is started, it pushes the (target, filename) to the started_queue.
922 The output is stored in filename. When a merge starts or finishes, we push
923 EmergeJobState objects to the job_queue.
924 """
925
926 SetupWorkerSignals()
927 settings, trees, mtimedb = emerge.settings, emerge.trees, emerge.mtimedb
David Jamesdeebd692011-05-09 17:02:52 -0700928
929 # Disable flushing of caches to save on I/O.
David James7a1ea4b2011-10-13 15:06:41 -0700930 root = emerge.settings["ROOT"]
931 vardb = emerge.trees[root]["vartree"].dbapi
932 vardb._flush_cache_enabled = False
Brian Harring0be85c62012-03-17 19:52:12 -0700933 bindb = emerge.trees[root]["bintree"].dbapi
934 # Might be a set, might be a list, might be None; no clue, just use shallow
935 # copy to ensure we can roll it back.
936 original_remotepkgs = copy.copy(bindb.bintree._remotepkgs)
David Jamesdeebd692011-05-09 17:02:52 -0700937
David Jamesfcb70ef2011-02-02 16:02:30 -0800938 opts, spinner = emerge.opts, emerge.spinner
939 opts["--nodeps"] = True
Brian Harring0be85c62012-03-17 19:52:12 -0700940 if fetch_only:
941 opts["--fetchonly"] = True
942
David Jamesfcb70ef2011-02-02 16:02:30 -0800943 while True:
944 # Wait for a new item to show up on the queue. This is a blocking wait,
945 # so if there's nothing to do, we just sit here.
Brian Harring0be85c62012-03-17 19:52:12 -0700946 pkg_state = task_queue.get()
947 if pkg_state is None:
David Jamesfcb70ef2011-02-02 16:02:30 -0800948 # If target is None, this means that the main thread wants us to quit.
949 # The other workers need to exit too, so we'll push the message back on
950 # to the queue so they'll get it too.
Brian Harring0be85c62012-03-17 19:52:12 -0700951 task_queue.put(None)
David Jamesfcb70ef2011-02-02 16:02:30 -0800952 return
David James7358d032011-05-19 10:40:03 -0700953 if KILLED.is_set():
954 return
955
Brian Harring0be85c62012-03-17 19:52:12 -0700956 target = pkg_state.target
957
David Jamesfcb70ef2011-02-02 16:02:30 -0800958 db_pkg = package_db[target]
Brian Harring0be85c62012-03-17 19:52:12 -0700959
960 if db_pkg.type_name == "binary":
961 if not fetch_only and pkg_state.fetched_successfully:
962 # Ensure portage doesn't think our pkg is remote- else it'll force
963 # a redownload of it (even if the on-disk file is fine). In-memory
964 # caching basically, implemented dumbly.
965 bindb.bintree._remotepkgs = None
966 else:
967 bindb.bintree_remotepkgs = original_remotepkgs
968
David Jamesfcb70ef2011-02-02 16:02:30 -0800969 db_pkg.root_config = emerge.root_config
970 install_list = [db_pkg]
971 pkgname = db_pkg.pf
972 output = tempfile.NamedTemporaryFile(prefix=pkgname + "-", delete=False)
David James01b1e0f2012-06-07 17:18:05 -0700973 os.chmod(output.name, 644)
David Jamesfcb70ef2011-02-02 16:02:30 -0800974 start_timestamp = time.time()
Brian Harring0be85c62012-03-17 19:52:12 -0700975 job = EmergeJobState(target, pkgname, False, output.name, start_timestamp,
976 fetch_only=fetch_only)
David Jamesfcb70ef2011-02-02 16:02:30 -0800977 job_queue.put(job)
978 if "--pretend" in opts:
979 retcode = 0
980 else:
David Jamesfcb70ef2011-02-02 16:02:30 -0800981 try:
David James386ccd12011-05-04 20:17:42 -0700982 emerge.scheduler_graph.mergelist = install_list
David James6b29d052012-11-02 10:27:27 -0700983 retcode = EmergeProcess(output, settings, trees, mtimedb, opts,
984 spinner, favorites=emerge.favorites,
985 graph_config=emerge.scheduler_graph)
David Jamesfcb70ef2011-02-02 16:02:30 -0800986 except Exception:
987 traceback.print_exc(file=output)
988 retcode = 1
David James1ed3e252011-10-05 20:26:15 -0700989 output.close()
David Jamesfcb70ef2011-02-02 16:02:30 -0800990
David James7358d032011-05-19 10:40:03 -0700991 if KILLED.is_set():
992 return
993
David Jamesfcb70ef2011-02-02 16:02:30 -0800994 job = EmergeJobState(target, pkgname, True, output.name, start_timestamp,
Brian Harring0be85c62012-03-17 19:52:12 -0700995 retcode, fetch_only=fetch_only)
David Jamesfcb70ef2011-02-02 16:02:30 -0800996 job_queue.put(job)
997
998
999class LinePrinter(object):
1000 """Helper object to print a single line."""
1001
1002 def __init__(self, line):
1003 self.line = line
1004
1005 def Print(self, seek_locations):
1006 print self.line
1007
1008
1009class JobPrinter(object):
1010 """Helper object to print output of a job."""
1011
1012 def __init__(self, job, unlink=False):
1013 """Print output of job.
1014
1015 If unlink is True, unlink the job output file when done."""
1016 self.current_time = time.time()
1017 self.job = job
1018 self.unlink = unlink
1019
1020 def Print(self, seek_locations):
1021
1022 job = self.job
1023
1024 # Calculate how long the job has been running.
1025 seconds = self.current_time - job.start_timestamp
1026
1027 # Note that we've printed out the job so far.
1028 job.last_output_timestamp = self.current_time
1029
1030 # Note that we're starting the job
1031 info = "job %s (%dm%.1fs)" % (job.pkgname, seconds / 60, seconds % 60)
1032 last_output_seek = seek_locations.get(job.filename, 0)
1033 if last_output_seek:
1034 print "=== Continue output for %s ===" % info
1035 else:
1036 print "=== Start output for %s ===" % info
1037
1038 # Print actual output from job
1039 f = codecs.open(job.filename, encoding='utf-8', errors='replace')
1040 f.seek(last_output_seek)
1041 prefix = job.pkgname + ":"
1042 for line in f:
1043
1044 # Save off our position in the file
1045 if line and line[-1] == "\n":
1046 last_output_seek = f.tell()
1047 line = line[:-1]
1048
1049 # Print our line
1050 print prefix, line.encode('utf-8', 'replace')
1051 f.close()
1052
1053 # Save our last spot in the file so that we don't print out the same
1054 # location twice.
1055 seek_locations[job.filename] = last_output_seek
1056
1057 # Note end of output section
1058 if job.done:
1059 print "=== Complete: %s ===" % info
1060 else:
1061 print "=== Still running: %s ===" % info
1062
1063 if self.unlink:
1064 os.unlink(job.filename)
1065
1066
1067def PrintWorker(queue):
1068 """A worker that prints stuff to the screen as requested."""
1069
1070 def ExitHandler(signum, frame):
David James7358d032011-05-19 10:40:03 -07001071 # Set KILLED flag.
1072 KILLED.set()
1073
David Jamesfcb70ef2011-02-02 16:02:30 -08001074 # Switch to default signal handlers so that we'll die after two signals.
David James7358d032011-05-19 10:40:03 -07001075 signal.signal(signal.SIGINT, KillHandler)
1076 signal.signal(signal.SIGTERM, KillHandler)
David Jamesfcb70ef2011-02-02 16:02:30 -08001077
1078 # Don't exit on the first SIGINT / SIGTERM, because the parent worker will
1079 # handle it and tell us when we need to exit.
1080 signal.signal(signal.SIGINT, ExitHandler)
1081 signal.signal(signal.SIGTERM, ExitHandler)
1082
1083 # seek_locations is a map indicating the position we are at in each file.
1084 # It starts off empty, but is set by the various Print jobs as we go along
1085 # to indicate where we left off in each file.
1086 seek_locations = {}
1087 while True:
1088 try:
1089 job = queue.get()
1090 if job:
1091 job.Print(seek_locations)
David Jamesbccf8eb2011-07-27 14:06:06 -07001092 sys.stdout.flush()
David Jamesfcb70ef2011-02-02 16:02:30 -08001093 else:
1094 break
1095 except IOError as ex:
1096 if ex.errno == errno.EINTR:
1097 # Looks like we received a signal. Keep printing.
1098 continue
1099 raise
1100
Brian Harring867e2362012-03-17 04:05:17 -07001101
Brian Harring0be85c62012-03-17 19:52:12 -07001102class TargetState(object):
Brian Harring867e2362012-03-17 04:05:17 -07001103
Brian Harring0be85c62012-03-17 19:52:12 -07001104 __slots__ = ("target", "info", "score", "prefetched", "fetched_successfully")
Brian Harring867e2362012-03-17 04:05:17 -07001105
Brian Harring0be85c62012-03-17 19:52:12 -07001106 def __init__(self, target, info, fetched=False):
Brian Harring867e2362012-03-17 04:05:17 -07001107 self.target, self.info = target, info
Brian Harring0be85c62012-03-17 19:52:12 -07001108 self.fetched_successfully = False
1109 self.prefetched = False
Brian Harring867e2362012-03-17 04:05:17 -07001110 self.update_score()
1111
1112 def __cmp__(self, other):
1113 return cmp(self.score, other.score)
1114
1115 def update_score(self):
1116 self.score = (
1117 -len(self.info["tprovides"]),
Brian Harring0be85c62012-03-17 19:52:12 -07001118 len(self.info["needs"]),
Brian Harring11c5eeb2012-03-18 11:02:39 -07001119 not self.info["binary"],
Brian Harring867e2362012-03-17 04:05:17 -07001120 -len(self.info["provides"]),
1121 self.info["idx"],
1122 self.target,
1123 )
1124
1125
1126class ScoredHeap(object):
1127
Brian Harring0be85c62012-03-17 19:52:12 -07001128 __slots__ = ("heap", "_heap_set")
1129
Brian Harring867e2362012-03-17 04:05:17 -07001130 def __init__(self, initial=()):
Brian Harring0be85c62012-03-17 19:52:12 -07001131 self.heap = list()
1132 self._heap_set = set()
1133 if initial:
1134 self.multi_put(initial)
Brian Harring867e2362012-03-17 04:05:17 -07001135
1136 def get(self):
Brian Harring0be85c62012-03-17 19:52:12 -07001137 item = heapq.heappop(self.heap)
1138 self._heap_set.remove(item.target)
1139 return item
Brian Harring867e2362012-03-17 04:05:17 -07001140
Brian Harring0be85c62012-03-17 19:52:12 -07001141 def put(self, item):
1142 if not isinstance(item, TargetState):
1143 raise ValueError("Item %r isn't a TargetState" % (item,))
1144 heapq.heappush(self.heap, item)
1145 self._heap_set.add(item.target)
Brian Harring867e2362012-03-17 04:05:17 -07001146
Brian Harring0be85c62012-03-17 19:52:12 -07001147 def multi_put(self, sequence):
1148 sequence = list(sequence)
1149 self.heap.extend(sequence)
1150 self._heap_set.update(x.target for x in sequence)
Brian Harring867e2362012-03-17 04:05:17 -07001151 self.sort()
1152
David James5c9996d2012-03-24 10:50:46 -07001153 def sort(self):
1154 heapq.heapify(self.heap)
1155
Brian Harring0be85c62012-03-17 19:52:12 -07001156 def __contains__(self, target):
1157 return target in self._heap_set
1158
1159 def __nonzero__(self):
1160 return bool(self.heap)
1161
Brian Harring867e2362012-03-17 04:05:17 -07001162 def __len__(self):
1163 return len(self.heap)
1164
1165
David Jamesfcb70ef2011-02-02 16:02:30 -08001166class EmergeQueue(object):
1167 """Class to schedule emerge jobs according to a dependency graph."""
1168
1169 def __init__(self, deps_map, emerge, package_db, show_output):
1170 # Store the dependency graph.
1171 self._deps_map = deps_map
Brian Harring0be85c62012-03-17 19:52:12 -07001172 self._state_map = {}
David Jamesfcb70ef2011-02-02 16:02:30 -08001173 # Initialize the running queue to empty
Brian Harring0be85c62012-03-17 19:52:12 -07001174 self._build_jobs = {}
1175 self._build_ready = ScoredHeap()
1176 self._fetch_jobs = {}
1177 self._fetch_ready = ScoredHeap()
David Jamesfcb70ef2011-02-02 16:02:30 -08001178 # List of total package installs represented in deps_map.
1179 install_jobs = [x for x in deps_map if deps_map[x]["action"] == "merge"]
1180 self._total_jobs = len(install_jobs)
1181 self._show_output = show_output
1182
1183 if "--pretend" in emerge.opts:
1184 print "Skipping merge because of --pretend mode."
1185 sys.exit(0)
1186
David James7358d032011-05-19 10:40:03 -07001187 # Set a process group so we can easily terminate all children.
1188 os.setsid()
1189
David Jamesfcb70ef2011-02-02 16:02:30 -08001190 # Setup scheduler graph object. This is used by the child processes
1191 # to help schedule jobs.
1192 emerge.scheduler_graph = emerge.depgraph.schedulerGraph()
1193
1194 # Calculate how many jobs we can run in parallel. We don't want to pass
1195 # the --jobs flag over to emerge itself, because that'll tell emerge to
1196 # hide its output, and said output is quite useful for debugging hung
1197 # jobs.
1198 procs = min(self._total_jobs,
1199 emerge.opts.pop("--jobs", multiprocessing.cpu_count()))
Brian Harring0be85c62012-03-17 19:52:12 -07001200 self._build_procs = procs
1201 self._fetch_procs = procs
David James8c7e5e32011-06-28 11:26:03 -07001202 self._load_avg = emerge.opts.pop("--load-average", None)
David Jamesfcb70ef2011-02-02 16:02:30 -08001203 self._job_queue = multiprocessing.Queue()
1204 self._print_queue = multiprocessing.Queue()
Brian Harring0be85c62012-03-17 19:52:12 -07001205
1206 self._fetch_queue = multiprocessing.Queue()
1207 args = (self._fetch_queue, self._job_queue, emerge, package_db, True)
1208 self._fetch_pool = multiprocessing.Pool(self._fetch_procs, EmergeWorker,
1209 args)
1210
1211 self._build_queue = multiprocessing.Queue()
1212 args = (self._build_queue, self._job_queue, emerge, package_db)
1213 self._build_pool = multiprocessing.Pool(self._build_procs, EmergeWorker,
1214 args)
1215
David Jamesfcb70ef2011-02-02 16:02:30 -08001216 self._print_worker = multiprocessing.Process(target=PrintWorker,
1217 args=[self._print_queue])
1218 self._print_worker.start()
1219
1220 # Initialize the failed queue to empty.
1221 self._retry_queue = []
1222 self._failed = set()
1223
David Jamesfcb70ef2011-02-02 16:02:30 -08001224 # Setup an exit handler so that we print nice messages if we are
1225 # terminated.
1226 self._SetupExitHandler()
1227
1228 # Schedule our jobs.
Brian Harring0be85c62012-03-17 19:52:12 -07001229 self._state_map.update(
1230 (pkg, TargetState(pkg, data)) for pkg, data in deps_map.iteritems())
1231 self._fetch_ready.multi_put(self._state_map.itervalues())
David Jamesfcb70ef2011-02-02 16:02:30 -08001232
1233 def _SetupExitHandler(self):
1234
1235 def ExitHandler(signum, frame):
David James7358d032011-05-19 10:40:03 -07001236 # Set KILLED flag.
1237 KILLED.set()
David Jamesfcb70ef2011-02-02 16:02:30 -08001238
1239 # Kill our signal handlers so we don't get called recursively
David James7358d032011-05-19 10:40:03 -07001240 signal.signal(signal.SIGINT, KillHandler)
1241 signal.signal(signal.SIGTERM, KillHandler)
David Jamesfcb70ef2011-02-02 16:02:30 -08001242
1243 # Print our current job status
Brian Harring0be85c62012-03-17 19:52:12 -07001244 for job in self._build_jobs.itervalues():
David Jamesfcb70ef2011-02-02 16:02:30 -08001245 if job:
1246 self._print_queue.put(JobPrinter(job, unlink=True))
1247
1248 # Notify the user that we are exiting
1249 self._Print("Exiting on signal %s" % signum)
David James7358d032011-05-19 10:40:03 -07001250 self._print_queue.put(None)
1251 self._print_worker.join()
David Jamesfcb70ef2011-02-02 16:02:30 -08001252
1253 # Kill child threads, then exit.
David James7358d032011-05-19 10:40:03 -07001254 os.killpg(0, signal.SIGKILL)
David Jamesfcb70ef2011-02-02 16:02:30 -08001255 sys.exit(1)
1256
1257 # Print out job status when we are killed
1258 signal.signal(signal.SIGINT, ExitHandler)
1259 signal.signal(signal.SIGTERM, ExitHandler)
1260
Brian Harring0be85c62012-03-17 19:52:12 -07001261 def _Schedule(self, pkg_state):
David Jamesfcb70ef2011-02-02 16:02:30 -08001262 # We maintain a tree of all deps, if this doesn't need
David James8c7e5e32011-06-28 11:26:03 -07001263 # to be installed just free up its children and continue.
David Jamesfcb70ef2011-02-02 16:02:30 -08001264 # It is possible to reinstall deps of deps, without reinstalling
1265 # first level deps, like so:
1266 # chromeos (merge) -> eselect (nomerge) -> python (merge)
Brian Harring0be85c62012-03-17 19:52:12 -07001267 this_pkg = pkg_state.info
1268 target = pkg_state.target
1269 if pkg_state.info is not None:
1270 if this_pkg["action"] == "nomerge":
1271 self._Finish(target)
1272 elif target not in self._build_jobs:
1273 # Kick off the build if it's marked to be built.
1274 self._build_jobs[target] = None
1275 self._build_queue.put(pkg_state)
1276 return True
David Jamesfcb70ef2011-02-02 16:02:30 -08001277
David James8c7e5e32011-06-28 11:26:03 -07001278 def _ScheduleLoop(self):
1279 # If the current load exceeds our desired load average, don't schedule
1280 # more than one job.
1281 if self._load_avg and os.getloadavg()[0] > self._load_avg:
1282 needed_jobs = 1
1283 else:
Brian Harring0be85c62012-03-17 19:52:12 -07001284 needed_jobs = self._build_procs
David James8c7e5e32011-06-28 11:26:03 -07001285
1286 # Schedule more jobs.
Brian Harring0be85c62012-03-17 19:52:12 -07001287 while self._build_ready and len(self._build_jobs) < needed_jobs:
1288 state = self._build_ready.get()
1289 if state.target not in self._failed:
1290 self._Schedule(state)
David Jamesfcb70ef2011-02-02 16:02:30 -08001291
1292 def _Print(self, line):
1293 """Print a single line."""
1294 self._print_queue.put(LinePrinter(line))
1295
1296 def _Status(self):
1297 """Print status."""
1298 current_time = time.time()
1299 no_output = True
1300
1301 # Print interim output every minute if --show-output is used. Otherwise,
1302 # print notifications about running packages every 2 minutes, and print
1303 # full output for jobs that have been running for 60 minutes or more.
1304 if self._show_output:
1305 interval = 60
1306 notify_interval = 0
1307 else:
1308 interval = 60 * 60
1309 notify_interval = 60 * 2
Brian Harring0be85c62012-03-17 19:52:12 -07001310 for target, job in self._build_jobs.iteritems():
David Jamesfcb70ef2011-02-02 16:02:30 -08001311 if job:
1312 last_timestamp = max(job.start_timestamp, job.last_output_timestamp)
1313 if last_timestamp + interval < current_time:
1314 self._print_queue.put(JobPrinter(job))
1315 job.last_output_timestamp = current_time
1316 no_output = False
1317 elif (notify_interval and
1318 job.last_notify_timestamp + notify_interval < current_time):
1319 job_seconds = current_time - job.start_timestamp
1320 args = (job.pkgname, job_seconds / 60, job_seconds % 60, job.filename)
1321 info = "Still building %s (%dm%.1fs). Logs in %s" % args
1322 job.last_notify_timestamp = current_time
1323 self._Print(info)
1324 no_output = False
1325
1326 # If we haven't printed any messages yet, print a general status message
1327 # here.
1328 if no_output:
1329 seconds = current_time - GLOBAL_START
Brian Harring0be85c62012-03-17 19:52:12 -07001330 fjobs, fready = len(self._fetch_jobs), len(self._fetch_ready)
1331 bjobs, bready = len(self._build_jobs), len(self._build_ready)
1332 retries = len(self._retry_queue)
1333 pending = max(0, len(self._deps_map) - fjobs - bjobs)
1334 line = "Pending %s/%s, " % (pending, self._total_jobs)
1335 if fjobs or fready:
1336 line += "Fetching %s/%s, " % (fjobs, fready + fjobs)
1337 if bjobs or bready or retries:
1338 line += "Building %s/%s, " % (bjobs, bready + bjobs)
1339 if retries:
1340 line += "Retrying %s, " % (retries,)
David James8c7e5e32011-06-28 11:26:03 -07001341 load = " ".join(str(x) for x in os.getloadavg())
Brian Harring0be85c62012-03-17 19:52:12 -07001342 line += ("[Time %dm%.1fs Load %s]" % (seconds/60, seconds %60, load))
1343 self._Print(line)
David Jamesfcb70ef2011-02-02 16:02:30 -08001344
1345 def _Finish(self, target):
David James8c7e5e32011-06-28 11:26:03 -07001346 """Mark a target as completed and unblock dependencies."""
1347 this_pkg = self._deps_map[target]
1348 if this_pkg["needs"] and this_pkg["nodeps"]:
1349 # We got installed, but our deps have not been installed yet. Dependent
1350 # packages should only be installed when our needs have been fully met.
1351 this_pkg["action"] = "nomerge"
1352 else:
1353 finish = []
1354 for dep in this_pkg["provides"]:
1355 dep_pkg = self._deps_map[dep]
Brian Harring0be85c62012-03-17 19:52:12 -07001356 state = self._state_map[dep]
David James8c7e5e32011-06-28 11:26:03 -07001357 del dep_pkg["needs"][target]
Brian Harring0be85c62012-03-17 19:52:12 -07001358 state.update_score()
1359 if not state.prefetched:
1360 if dep in self._fetch_ready:
1361 # If it's not currently being fetched, update the prioritization
1362 self._fetch_ready.sort()
1363 elif not dep_pkg["needs"]:
David James8c7e5e32011-06-28 11:26:03 -07001364 if dep_pkg["nodeps"] and dep_pkg["action"] == "nomerge":
1365 self._Finish(dep)
1366 else:
Brian Harring0be85c62012-03-17 19:52:12 -07001367 self._build_ready.put(self._state_map[dep])
David James8c7e5e32011-06-28 11:26:03 -07001368 self._deps_map.pop(target)
David Jamesfcb70ef2011-02-02 16:02:30 -08001369
1370 def _Retry(self):
David James8c7e5e32011-06-28 11:26:03 -07001371 while self._retry_queue:
Brian Harring0be85c62012-03-17 19:52:12 -07001372 state = self._retry_queue.pop(0)
1373 if self._Schedule(state):
1374 self._Print("Retrying emerge of %s." % state.target)
David James8c7e5e32011-06-28 11:26:03 -07001375 break
David Jamesfcb70ef2011-02-02 16:02:30 -08001376
Brian Harringa43f5952012-04-12 01:19:34 -07001377 def _Shutdown(self):
David Jamesfcb70ef2011-02-02 16:02:30 -08001378 # Tell emerge workers to exit. They all exit when 'None' is pushed
1379 # to the queue.
Brian Harring0be85c62012-03-17 19:52:12 -07001380
Brian Harringa43f5952012-04-12 01:19:34 -07001381 # Shutdown the workers first; then jobs (which is how they feed things back)
1382 # then finally the print queue.
Brian Harring0be85c62012-03-17 19:52:12 -07001383
Brian Harringa43f5952012-04-12 01:19:34 -07001384 def _stop(queue, pool):
1385 if pool is None:
1386 return
1387 try:
1388 queue.put(None)
1389 pool.close()
1390 pool.join()
1391 finally:
1392 pool.terminate()
Brian Harring0be85c62012-03-17 19:52:12 -07001393
Brian Harringa43f5952012-04-12 01:19:34 -07001394 _stop(self._fetch_queue, self._fetch_pool)
1395 self._fetch_queue = self._fetch_pool = None
Brian Harring0be85c62012-03-17 19:52:12 -07001396
Brian Harringa43f5952012-04-12 01:19:34 -07001397 _stop(self._build_queue, self._build_pool)
1398 self._build_queue = self._build_pool = None
1399
1400 if self._job_queue is not None:
1401 self._job_queue.close()
1402 self._job_queue = None
David Jamesfcb70ef2011-02-02 16:02:30 -08001403
1404 # Now that our workers are finished, we can kill the print queue.
Brian Harringa43f5952012-04-12 01:19:34 -07001405 if self._print_worker is not None:
1406 try:
1407 self._print_queue.put(None)
1408 self._print_queue.close()
1409 self._print_worker.join()
1410 finally:
1411 self._print_worker.terminate()
1412 self._print_queue = self._print_worker = None
David Jamesfcb70ef2011-02-02 16:02:30 -08001413
1414 def Run(self):
1415 """Run through the scheduled ebuilds.
1416
1417 Keep running so long as we have uninstalled packages in the
1418 dependency graph to merge.
1419 """
Brian Harringa43f5952012-04-12 01:19:34 -07001420 if not self._deps_map:
1421 return
1422
Brian Harring0be85c62012-03-17 19:52:12 -07001423 # Start the fetchers.
1424 for _ in xrange(min(self._fetch_procs, len(self._fetch_ready))):
1425 state = self._fetch_ready.get()
1426 self._fetch_jobs[state.target] = None
1427 self._fetch_queue.put(state)
1428
1429 # Print an update, then get going.
1430 self._Status()
1431
David Jamese703d0f2012-01-12 16:27:45 -08001432 retried = set()
David Jamesfcb70ef2011-02-02 16:02:30 -08001433 while self._deps_map:
1434 # Check here that we are actually waiting for something.
Brian Harring0be85c62012-03-17 19:52:12 -07001435 if (self._build_queue.empty() and
David Jamesfcb70ef2011-02-02 16:02:30 -08001436 self._job_queue.empty() and
Brian Harring0be85c62012-03-17 19:52:12 -07001437 not self._fetch_jobs and
1438 not self._fetch_ready and
1439 not self._build_jobs and
1440 not self._build_ready and
David Jamesfcb70ef2011-02-02 16:02:30 -08001441 self._deps_map):
1442 # If we have failed on a package, retry it now.
1443 if self._retry_queue:
1444 self._Retry()
1445 else:
David Jamesfcb70ef2011-02-02 16:02:30 -08001446 # Tell the user why we're exiting.
1447 if self._failed:
Mike Frysingerf2ff9172012-11-01 18:47:41 -04001448 print 'Packages failed:\n\t%s' % '\n\t'.join(self._failed)
David James0eae23e2012-07-03 15:04:25 -07001449 status_file = os.environ.get("PARALLEL_EMERGE_STATUS_FILE")
1450 if status_file:
1451 failed_pkgs = set(portage.cpv_getkey(x) for x in self._failed)
1452 with open(status_file, "a") as f:
1453 f.write("%s\n" % " ".join(failed_pkgs))
David Jamesfcb70ef2011-02-02 16:02:30 -08001454 else:
1455 print "Deadlock! Circular dependencies!"
1456 sys.exit(1)
1457
Brian Harring706747c2012-03-16 03:04:31 -07001458 for i in range(12):
David Jamesa74289a2011-08-12 10:41:24 -07001459 try:
1460 job = self._job_queue.get(timeout=5)
1461 break
1462 except Queue.Empty:
1463 # Check if any more jobs can be scheduled.
1464 self._ScheduleLoop()
1465 else:
Brian Harring706747c2012-03-16 03:04:31 -07001466 # Print an update every 60 seconds.
David Jamesfcb70ef2011-02-02 16:02:30 -08001467 self._Status()
1468 continue
1469
1470 target = job.target
1471
Brian Harring0be85c62012-03-17 19:52:12 -07001472 if job.fetch_only:
1473 if not job.done:
1474 self._fetch_jobs[job.target] = job
1475 else:
1476 state = self._state_map[job.target]
1477 state.prefetched = True
1478 state.fetched_successfully = (job.retcode == 0)
1479 del self._fetch_jobs[job.target]
1480 self._Print("Fetched %s in %2.2fs"
1481 % (target, time.time() - job.start_timestamp))
1482
1483 if self._show_output or job.retcode != 0:
1484 self._print_queue.put(JobPrinter(job, unlink=True))
1485 else:
1486 os.unlink(job.filename)
1487 # Failure or not, let build work with it next.
1488 if not self._deps_map[job.target]["needs"]:
1489 self._build_ready.put(state)
1490 self._ScheduleLoop()
1491
1492 if self._fetch_ready:
1493 state = self._fetch_ready.get()
1494 self._fetch_queue.put(state)
1495 self._fetch_jobs[state.target] = None
1496 else:
1497 # Minor optimization; shut down fetchers early since we know
1498 # the queue is empty.
1499 self._fetch_queue.put(None)
1500 continue
1501
David Jamesfcb70ef2011-02-02 16:02:30 -08001502 if not job.done:
Brian Harring0be85c62012-03-17 19:52:12 -07001503 self._build_jobs[target] = job
David Jamesfcb70ef2011-02-02 16:02:30 -08001504 self._Print("Started %s (logged in %s)" % (target, job.filename))
1505 continue
1506
1507 # Print output of job
1508 if self._show_output or job.retcode != 0:
1509 self._print_queue.put(JobPrinter(job, unlink=True))
1510 else:
1511 os.unlink(job.filename)
Brian Harring0be85c62012-03-17 19:52:12 -07001512 del self._build_jobs[target]
David Jamesfcb70ef2011-02-02 16:02:30 -08001513
1514 seconds = time.time() - job.start_timestamp
1515 details = "%s (in %dm%.1fs)" % (target, seconds / 60, seconds % 60)
David James32420cc2011-08-25 21:32:46 -07001516 previously_failed = target in self._failed
David Jamesfcb70ef2011-02-02 16:02:30 -08001517
1518 # Complain if necessary.
1519 if job.retcode != 0:
1520 # Handle job failure.
David James32420cc2011-08-25 21:32:46 -07001521 if previously_failed:
David Jamesfcb70ef2011-02-02 16:02:30 -08001522 # If this job has failed previously, give up.
1523 self._Print("Failed %s. Your build has failed." % details)
1524 else:
1525 # Queue up this build to try again after a long while.
David Jamese703d0f2012-01-12 16:27:45 -08001526 retried.add(target)
Brian Harring0be85c62012-03-17 19:52:12 -07001527 self._retry_queue.append(self._state_map[target])
David Jamesfcb70ef2011-02-02 16:02:30 -08001528 self._failed.add(target)
1529 self._Print("Failed %s, retrying later." % details)
1530 else:
David James32420cc2011-08-25 21:32:46 -07001531 if previously_failed:
1532 # Remove target from list of failed packages.
1533 self._failed.remove(target)
1534
1535 self._Print("Completed %s" % details)
1536
1537 # Mark as completed and unblock waiting ebuilds.
1538 self._Finish(target)
1539
1540 if previously_failed and self._retry_queue:
David Jamesfcb70ef2011-02-02 16:02:30 -08001541 # If we have successfully retried a failed package, and there
1542 # are more failed packages, try the next one. We will only have
1543 # one retrying package actively running at a time.
1544 self._Retry()
1545
David Jamesfcb70ef2011-02-02 16:02:30 -08001546
David James8c7e5e32011-06-28 11:26:03 -07001547 # Schedule pending jobs and print an update.
1548 self._ScheduleLoop()
1549 self._Status()
David Jamesfcb70ef2011-02-02 16:02:30 -08001550
David Jamese703d0f2012-01-12 16:27:45 -08001551 # If packages were retried, output a warning.
1552 if retried:
1553 self._Print("")
1554 self._Print("WARNING: The following packages failed the first time,")
1555 self._Print("but succeeded upon retry. This might indicate incorrect")
1556 self._Print("dependencies.")
1557 for pkg in retried:
1558 self._Print(" %s" % pkg)
1559 self._Print("@@@STEP_WARNINGS@@@")
1560 self._Print("")
1561
David Jamesfcb70ef2011-02-02 16:02:30 -08001562 # Tell child threads to exit.
1563 self._Print("Merge complete")
David Jamesfcb70ef2011-02-02 16:02:30 -08001564
1565
Brian Harring30675052012-02-29 12:18:22 -08001566def main(argv):
Brian Harring8294d652012-05-23 02:20:52 -07001567 try:
1568 return real_main(argv)
1569 finally:
1570 # Work around multiprocessing sucking and not cleaning up after itself.
1571 # http://bugs.python.org/issue4106;
1572 # Step one; ensure GC is ran *prior* to the VM starting shutdown.
1573 gc.collect()
1574 # Step two; go looking for those threads and try to manually reap
1575 # them if we can.
1576 for x in threading.enumerate():
1577 # Filter on the name, and ident; if ident is None, the thread
1578 # wasn't started.
1579 if x.name == 'QueueFeederThread' and x.ident is not None:
1580 x.join(1)
David Jamesfcb70ef2011-02-02 16:02:30 -08001581
Brian Harring8294d652012-05-23 02:20:52 -07001582
1583def real_main(argv):
Brian Harring30675052012-02-29 12:18:22 -08001584 parallel_emerge_args = argv[:]
David Jamesfcb70ef2011-02-02 16:02:30 -08001585 deps = DepGraphGenerator()
Brian Harring30675052012-02-29 12:18:22 -08001586 deps.Initialize(parallel_emerge_args)
David Jamesfcb70ef2011-02-02 16:02:30 -08001587 emerge = deps.emerge
1588
1589 if emerge.action is not None:
Brian Harring30675052012-02-29 12:18:22 -08001590 argv = deps.ParseParallelEmergeArgs(argv)
Brian Harring8294d652012-05-23 02:20:52 -07001591 return emerge_main(argv)
David Jamesfcb70ef2011-02-02 16:02:30 -08001592 elif not emerge.cmdline_packages:
1593 Usage()
Brian Harring8294d652012-05-23 02:20:52 -07001594 return 1
David Jamesfcb70ef2011-02-02 16:02:30 -08001595
1596 # Unless we're in pretend mode, there's not much point running without
1597 # root access. We need to be able to install packages.
1598 #
1599 # NOTE: Even if you're running --pretend, it's a good idea to run
1600 # parallel_emerge with root access so that portage can write to the
1601 # dependency cache. This is important for performance.
1602 if "--pretend" not in emerge.opts and portage.secpass < 2:
1603 print "parallel_emerge: superuser access is required."
Brian Harring8294d652012-05-23 02:20:52 -07001604 return 1
David Jamesfcb70ef2011-02-02 16:02:30 -08001605
1606 if "--quiet" not in emerge.opts:
1607 cmdline_packages = " ".join(emerge.cmdline_packages)
David Jamesfcb70ef2011-02-02 16:02:30 -08001608 print "Starting fast-emerge."
1609 print " Building package %s on %s" % (cmdline_packages,
1610 deps.board or "root")
David Jamesfcb70ef2011-02-02 16:02:30 -08001611
David James386ccd12011-05-04 20:17:42 -07001612 deps_tree, deps_info = deps.GenDependencyTree()
David Jamesfcb70ef2011-02-02 16:02:30 -08001613
1614 # You want me to be verbose? I'll give you two trees! Twice as much value.
1615 if "--tree" in emerge.opts and "--verbose" in emerge.opts:
1616 deps.PrintTree(deps_tree)
1617
David James386ccd12011-05-04 20:17:42 -07001618 deps_graph = deps.GenDependencyGraph(deps_tree, deps_info)
David Jamesfcb70ef2011-02-02 16:02:30 -08001619
1620 # OK, time to print out our progress so far.
1621 deps.PrintInstallPlan(deps_graph)
1622 if "--tree" in emerge.opts:
1623 PrintDepsMap(deps_graph)
1624
1625 # Are we upgrading portage? If so, and there are more packages to merge,
1626 # schedule a restart of parallel_emerge to merge the rest. This ensures that
1627 # we pick up all updates to portage settings before merging any more
1628 # packages.
1629 portage_upgrade = False
1630 root = emerge.settings["ROOT"]
1631 final_db = emerge.depgraph._dynamic_config.mydbapi[root]
1632 if root == "/":
1633 for db_pkg in final_db.match_pkgs("sys-apps/portage"):
1634 portage_pkg = deps_graph.get(db_pkg.cpv)
David James0ff16f22012-11-02 14:18:07 -07001635 if portage_pkg:
David Jamesfcb70ef2011-02-02 16:02:30 -08001636 portage_upgrade = True
1637 if "--quiet" not in emerge.opts:
1638 print "Upgrading portage first, then restarting..."
1639
David James0ff16f22012-11-02 14:18:07 -07001640 # Upgrade Portage first, then the rest of the packages.
1641 #
1642 # In order to grant the child permission to run setsid, we need to run sudo
1643 # again. We preserve SUDO_USER here in case an ebuild depends on it.
1644 if portage_upgrade:
1645 # Calculate what arguments to use when re-invoking.
1646 args = ["sudo", "-E", "SUDO_USER=%s" % os.environ.get("SUDO_USER", "")]
1647 args += [os.path.abspath(sys.argv[0])] + parallel_emerge_args
1648 args += ["--exclude=sys-apps/portage"]
1649
1650 # First upgrade Portage.
1651 passthrough_args = ("--quiet", "--pretend", "--verbose")
1652 emerge_args = [k for k in emerge.opts if k in passthrough_args]
1653 ret = emerge_main(emerge_args + ["portage"])
1654 if ret != 0:
1655 return ret
1656
1657 # Now upgrade the rest.
1658 os.execvp(args[0], args)
1659
David Jamesfcb70ef2011-02-02 16:02:30 -08001660 # Run the queued emerges.
1661 scheduler = EmergeQueue(deps_graph, emerge, deps.package_db, deps.show_output)
Brian Harringa43f5952012-04-12 01:19:34 -07001662 try:
1663 scheduler.Run()
1664 finally:
1665 scheduler._Shutdown()
David James97ce8902011-08-16 09:51:05 -07001666 scheduler = None
David Jamesfcb70ef2011-02-02 16:02:30 -08001667
Mike Frysingerd20a6e22012-10-04 19:01:10 -04001668 clean_logs(emerge.settings)
1669
David Jamesfcb70ef2011-02-02 16:02:30 -08001670 print "Done"
Brian Harring8294d652012-05-23 02:20:52 -07001671 return 0