From 528990c3279dcb2b37062f447cfda80730ead6b7 Mon Sep 17 00:00:00 2001
From: Zac Medico <zmedico@gentoo.org>
Date: Sun, 13 May 2012 16:40:29 -0700
Subject: [PATCH] _pkg_str: validate repo (move code from Package)

---
 pym/_emerge/Package.py        | 17 +++++++----------
 pym/portage/dbapi/__init__.py |  2 --
 pym/portage/dep/__init__.py   |  4 +---
 pym/portage/versions.py       |  8 +++++++-
 4 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/pym/_emerge/Package.py b/pym/_emerge/Package.py
index 84a2cbced..c04fa1f8f 100644
--- a/pym/_emerge/Package.py
+++ b/pym/_emerge/Package.py
@@ -8,12 +8,10 @@ from portage import _encodings, _unicode_decode, _unicode_encode
 from portage.cache.mappings import slot_dict_class
 from portage.const import EBUILD_PHASES
 from portage.dep import Atom, check_required_use, use_reduce, \
-	paren_enclose, _slot_re, _slot_separator, _repo_separator, \
-	_unknown_repo
-from portage.versions import _pkg_str
+	paren_enclose, _slot_re, _slot_separator, _repo_separator
+from portage.versions import _pkg_str, _unknown_repo
 from portage.eapi import eapi_has_iuse_defaults, eapi_has_required_use
 from portage.exception import InvalidDependString
-from portage.repository.config import _gen_valid_repo
 from _emerge.Task import Task
 
 if sys.hexversion >= 0x3000000:
@@ -58,12 +56,11 @@ class Package(Task):
 			# Avoid an InvalidAtom exception when creating slot_atom.
 			# This package instance will be masked due to empty SLOT.
 			slot = '0'
-		repo = _gen_valid_repo(self.metadata.get('repository', ''))
-		if not repo:
-			repo = self.UNKNOWN_REPO
-		self.metadata['repository'] = repo
-		self.cpv = _pkg_str(self.cpv, slot=slot, repo=repo)
+		self.cpv = _pkg_str(self.cpv, slot=slot,
+			repo=self.metadata.get('repository', ''))
 		self.cp = self.cpv.cp
+		# sync metadata with validated repo (may be UNKNOWN_REPO)
+		self.metadata['repository'] = self.cpv.repo
 		if (self.iuse.enabled or self.iuse.disabled) and \
 			not eapi_has_iuse_defaults(self.metadata["EAPI"]):
 			if not self.installed:
@@ -87,7 +84,7 @@ class Package(Task):
 
 		self._hash_key = Package._gen_hash_key(cpv=self.cpv,
 			installed=self.installed, onlydeps=self.onlydeps,
-			operation=self.operation, repo_name=repo,
+			operation=self.operation, repo_name=self.cpv.repo,
 			root_config=self.root_config,
 			type_name=self.type_name)
 		self._hash_value = hash(self._hash_key)
diff --git a/pym/portage/dbapi/__init__.py b/pym/portage/dbapi/__init__.py
index 7a4c823ab..a1c5c5674 100644
--- a/pym/portage/dbapi/__init__.py
+++ b/pym/portage/dbapi/__init__.py
@@ -160,8 +160,6 @@ class dbapi(object):
 			try:
 				metadata = dict(zip(aux_keys,
 					self.aux_get(cpv, aux_keys, myrepo=atom.repo)))
-				if not metadata["repository"]:
-					del metadata["repository"]
 			except KeyError:
 				continue
 
diff --git a/pym/portage/dep/__init__.py b/pym/portage/dep/__init__.py
index 38382170a..55ed4810c 100644
--- a/pym/portage/dep/__init__.py
+++ b/pym/portage/dep/__init__.py
@@ -43,7 +43,7 @@ from portage.eapi import eapi_has_slot_deps, eapi_has_src_uri_arrows, \
 from portage.exception import InvalidAtom, InvalidData, InvalidDependString
 from portage.localization import _
 from portage.versions import catpkgsplit, catsplit, \
-	pkgcmp, vercmp, ververify, _cp, _cpv, _pkg_str
+	vercmp, ververify, _cp, _cpv, _pkg_str, _unknown_repo
 import portage.cache.mappings
 
 if sys.hexversion >= 0x3000000:
@@ -54,8 +54,6 @@ if sys.hexversion >= 0x3000000:
 # stable keywords, make these warnings unconditional.
 _internal_warnings = False
 
-_unknown_repo = "__unknown__"
-
 def cpvequal(cpv1, cpv2):
 	"""
 	
diff --git a/pym/portage/versions.py b/pym/portage/versions.py
index 298b7aaca..db14e9988 100644
--- a/pym/portage/versions.py
+++ b/pym/portage/versions.py
@@ -19,13 +19,16 @@ else:
 
 import portage
 portage.proxy.lazyimport.lazyimport(globals(),
-	'portage.util:cmp_sort_key'
+	'portage.repository.config:_gen_valid_repo',
+	'portage.util:cmp_sort_key',
 )
 from portage import _unicode_decode
 from portage.eapi import eapi_allows_dots_in_PN
 from portage.exception import InvalidData
 from portage.localization import _
 
+_unknown_repo = "__unknown__"
+
 # \w is [a-zA-Z0-9_]
 
 # 2.1.1 A category name may contain any of the characters [A-Za-z0-9+_.-].
@@ -347,6 +350,9 @@ class _pkg_str(_unicode):
 		if slot is not None:
 			self.__dict__['slot'] = slot
 		if repo is not None:
+			repo = _gen_valid_repo(repo)
+			if not repo:
+				repo = _unknown_repo
 			self.__dict__['repo'] = repo
 
 	def __setattr__(self, name, value):
-- 
2.26.2