repoman: populate implicit IUSE for empty profile (bug 660982)

For the empty profile that's used to check dependencies of
ebuilds that have empty KEYWORDS, populate implicit IUSE
from all of the make.defaults files found in the relevant
repositories (this should take less than 1 second on most
hardware). Since the IUSE.missing check cannot be performed
without implicit IUSE settings, this makes the IUSE.missing
check work for ebuilds with empty KEYWORDS.

Bug: https://bugs.gentoo.org/660982
diff --git a/pym/portage/dbapi/__init__.py b/pym/portage/dbapi/__init__.py
index 61d3018..6fca609 100644
--- a/pym/portage/dbapi/__init__.py
+++ b/pym/portage/dbapi/__init__.py
@@ -219,17 +219,13 @@
 	def _repoman_iuse_implicit_cnstr(self, pkg, metadata):
 		"""
 		In repoman's version of _iuse_implicit_cnstr, account for modifications
-		of the self.settings reference between calls, and treat all flags as
-		valid for the empty profile because it does not have any implicit IUSE
-		settings. See bug 660982.
+		of the self.settings reference between calls.
 		"""
 		eapi_attrs = _get_eapi_attrs(metadata["EAPI"])
 		if eapi_attrs.iuse_effective:
-			iuse_implicit_match = lambda flag: (True if not self.settings.profile_path
-				else self.settings._iuse_effective_match(flag))
+			iuse_implicit_match = lambda flag: self.settings._iuse_effective_match(flag)
 		else:
-			iuse_implicit_match = lambda flag: (True if not self.settings.profile_path
-				else self.settings._iuse_implicit_match(flag))
+			iuse_implicit_match = lambda flag: self.settings._iuse_implicit_match(flag)
 		return iuse_implicit_match
 
 	def _iuse_implicit_cnstr(self, pkg, metadata):
diff --git a/repoman/pym/repoman/modules/scan/depend/profile.py b/repoman/pym/repoman/modules/scan/depend/profile.py
index 8e0a22f..d980f4e 100644
--- a/repoman/pym/repoman/modules/scan/depend/profile.py
+++ b/repoman/pym/repoman/modules/scan/depend/profile.py
@@ -2,6 +2,7 @@
 
 
 import copy
+import os
 from pprint import pformat
 
 from _emerge.Package import Package
@@ -12,6 +13,8 @@
 from repoman.modules.scan.depend._depend_checks import _depend_checks
 from repoman.modules.scan.depend._gen_arches import _gen_arches
 from portage.dep import Atom
+from portage.package.ebuild.profile_iuse import iter_iuse_vars
+from portage.util import getconfig
 
 
 def sort_key(item):
@@ -102,6 +105,11 @@
 					local_config=False,
 					_unmatched_removal=self.options.unmatched_removal,
 					env=self.env, repositories=self.repo_settings.repoman_settings.repositories)
+
+				if not prof.abs_path:
+					self._populate_implicit_iuse(dep_settings,
+						self.repo_settings.repo_config.eclass_db.porttrees)
+
 				dep_settings.categories = self.repo_settings.repoman_settings.categories
 				if self.options.without_mask:
 					dep_settings._mask_manager_obj = \
@@ -257,3 +265,31 @@
 	def runInEbuilds(self):
 		'''Ebuild level scans'''
 		return (True, [self.check])
+
+	@staticmethod
+	def _populate_implicit_iuse(config, repo_locations):
+		"""
+		Populate implicit IUSE for the empty profile, see bug 660982.
+
+		@param config: config instance for the empty profile
+		@type config: portage.config
+		@param repo_locations: locations of repositories containing relevant
+			implicit IUSE settings
+		@type repo_locations: list
+		"""
+		dest = config.configdict['defaults']
+		for location in repo_locations:
+			for parent_dir, dirs, files in os.walk(os.path.join(location, 'profiles')):
+				src = getconfig(os.path.join(parent_dir, 'make.defaults'))
+				if not src:
+					continue
+				for k, v in iter_iuse_vars(src):
+					v_before = dest.get(k)
+					if v_before is not None:
+						merged_values = set(v_before.split())
+						merged_values.update(v.split())
+						v = ' '.join(sorted(merged_values))
+					dest[k] = v
+
+		config.regenerate()
+		config._init_iuse()