Reland "parallel_emerge: Work around Portage library bug with usepkg"

This reverts commit 95f30c12f2f0012aa5bc45ee05b1cdeae9c7b9ea.

crrev.com/c/643566 fixed the original source of breakage, so this should
now be safe to put in again.

BUG=chromium:756240
TEST=Sent to chromiumos-sdk trybot

Change-Id: Icb9f5f7669e8e5514f698a175693203e06e3a8e9
Reviewed-on: https://chromium-review.googlesource.com/644617
Commit-Ready: Benjamin Gordon <bmgordon@chromium.org>
Tested-by: Benjamin Gordon <bmgordon@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/scripts/parallel_emerge.py b/scripts/parallel_emerge.py
index f364529..04145f3 100644
--- a/scripts/parallel_emerge.py
+++ b/scripts/parallel_emerge.py
@@ -592,7 +592,17 @@
       binpkg_phases = set(["setup", "preinst", "postinst"])
       needed_dep_types = set(["blocker", "buildtime", "buildtime_slot_op",
                               "runtime", "runtime_slot_op"])
-      ignored_dep_types = set(["ignored", "optional", "runtime_post", "soft"])
+      ignored_dep_types = set(["ignored", "runtime_post", "soft"])
+
+      # There's a bug in the Portage library where it always returns 'optional'
+      # and never 'buildtime' for the digraph while --usepkg is enabled; even
+      # when the package is being rebuilt. To work around this, we treat
+      # 'optional' as needed when we are using --usepkg. See crbug.com/756240 .
+      if "--usepkg" in self.emerge.opts:
+        needed_dep_types.add("optional")
+      else:
+        ignored_dep_types.add("optional")
+
       all_dep_types = ignored_dep_types | needed_dep_types
       for pkg in packages: