depgraph: autounmask for conditional USE deps (bug 566704)

For parents with unsatisfied conditional dependencies, translate
USE change suggestions into autounmask changes.

X-Gentoo-Bug: 566704
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=566704
Acked-by: Alexander Berntsen <bernalex@gentoo.org>
diff --git a/pym/_emerge/depgraph.py b/pym/_emerge/depgraph.py
index 57040ab..f659b0a 100644
--- a/pym/_emerge/depgraph.py
+++ b/pym/_emerge/depgraph.py
@@ -4075,6 +4075,7 @@
 		# Now that the root packages have been added to the graph,
 		# process the dependencies.
 		if not self._create_graph():
+			self._apply_parent_use_changes()
 			return 0, myfavorites
 
 		try:
@@ -4162,6 +4163,24 @@
 		# We're true here unless we are missing binaries.
 		return (True, myfavorites)
 
+	def _apply_parent_use_changes(self):
+		"""
+		For parents with unsatisfied conditional dependencies, translate
+		USE change suggestions into autounmask changes.
+		"""
+		if (self._dynamic_config._unsatisfied_deps_for_display and
+			self._dynamic_config._autounmask):
+			remaining_items = []
+			for item in self._dynamic_config._unsatisfied_deps_for_display:
+				pargs, kwargs = item
+				kwargs = kwargs.copy()
+				kwargs['collect_use_changes'] = True
+				if not self._show_unsatisfied_dep(*pargs,
+					**portage._native_kwargs(kwargs)):
+					remaining_items.append(item)
+			if len(remaining_items) != len(self._dynamic_config._unsatisfied_deps_for_display):
+				self._dynamic_config._unsatisfied_deps_for_display = remaining_items
+
 	def _set_args(self, args):
 		"""
 		Create the "__non_set_args__" package set from atoms and packages given as
@@ -4718,7 +4737,8 @@
 
 
 	def _show_unsatisfied_dep(self, root, atom, myparent=None, arg=None,
-		check_backtrack=False, check_autounmask_breakage=False, show_req_use=None):
+		check_backtrack=False, check_autounmask_breakage=False, show_req_use=None,
+		collect_use_changes=False):
 		"""
 		When check_backtrack=True, no output is produced and
 		the method either returns or raises _backtrack_mask if
@@ -4962,15 +4982,28 @@
 									"defined by %s: '%s'" % (myparent.cpv, \
 									human_readable_required_use(required_use))
 
+					target_use = {}
 					for flag in involved_flags:
 						if flag in self._pkg_use_enabled(myparent):
+							target_use[flag] = False
 							changes.append(colorize("blue", "-" + flag))
 						else:
+							target_use[flag] = True
 							changes.append(colorize("red", "+" + flag))
+
+					if collect_use_changes and not required_use_warning:
+						previous_changes = self._dynamic_config._needed_use_config_changes.get(myparent)
+						self._pkg_use_enabled(myparent, target_use=target_use)
+						if previous_changes is not self._dynamic_config._needed_use_config_changes.get(myparent):
+							return True
+
 					mreasons.append("Change USE: %s" % " ".join(changes) + required_use_warning)
 					if (myparent, mreasons) not in missing_use_reasons:
 						missing_use_reasons.append((myparent, mreasons))
 
+		if collect_use_changes:
+			return False
+
 		unmasked_use_reasons = [(pkg, mreasons) for (pkg, mreasons) \
 			in missing_use_reasons if pkg not in masked_pkg_instances]
 
diff --git a/pym/portage/tests/resolver/test_autounmask_parent.py b/pym/portage/tests/resolver/test_autounmask_parent.py
new file mode 100644
index 0000000..042acab
--- /dev/null
+++ b/pym/portage/tests/resolver/test_autounmask_parent.py
@@ -0,0 +1,43 @@
+# Copyright 2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+from portage.tests import TestCase
+from portage.tests.resolver.ResolverPlayground import (
+	ResolverPlayground,
+	ResolverPlaygroundTestCase,
+)
+
+class AutounmaskParentTestCase(TestCase):
+
+	def testAutounmaskParentUse(self):
+
+		ebuilds = {
+			"dev-libs/B-1": {
+				"EAPI": "5",
+				"DEPEND": "dev-libs/D[foo(-)?,bar(-)?]",
+				"IUSE": "+bar +foo",
+			},
+			"dev-libs/D-1": {},
+		}
+
+		test_cases = (
+			# Test bug 566704
+			ResolverPlaygroundTestCase(
+				["=dev-libs/B-1"],
+				options={"--autounmask": True},
+				success=False,
+				use_changes={
+					"dev-libs/B-1": {
+						"foo": False,
+						"bar": False,
+					}
+				}),
+		)
+
+		playground = ResolverPlayground(ebuilds=ebuilds)
+		try:
+			for test_case in test_cases:
+				playground.run_TestCase(test_case)
+				self.assertEqual(test_case.test_success, True, test_case.fail_msg)
+		finally:
+			playground.cleanup()