Commits

Michał Górny committed a3ffdc9

Support getting slot and repository name from atoms.

Comments (0)

Files changed (5)

gentoopm/basepm/atom.py

 		@type: L{PMPackageVersion}/C{None}
 		"""
 		pass
+
+	@abstractproperty
+	def slot(self):
+		"""
+		The package slot (if specified).
+
+		@type: string/C{None}
+		"""
+		pass
+
+	@abstractproperty
+	def repository(self):
+		"""
+		The package repository name (if specified).
+
+		@type: string/C{None}
+		"""
+		pass

gentoopm/paludispm/atom.py

 		except StopIteration:
 			return None
 		return PaludisPackageVersion(vr.version_spec)
+
+	@property
+	def slot(self):
+		# XXX
+		raise NotImplementedError('PaludisAtom.slot not yet implemented.')
+
+	@property
+	def repository(self):
+		if self._atom.in_repository is None:
+			return None
+		return str(self._atom.in_repository)

gentoopm/pkgcorepm/atom.py

 # Released under the terms of the 2-clause BSD license.
 
 from pkgcore.ebuild.atom import atom
-from pkgcore.ebuild.restricts import PackageDep, VersionMatch
+from pkgcore.ebuild.restricts import PackageDep, VersionMatch, \
+		RepositoryDep, SlotDep
 from pkgcore.restrictions.boolean import AndRestriction
 from pkgcore.util.parserestrict import parse_match, ParseError
 
 		PMIncompletePackageKey
 from gentoopm.exceptions import InvalidAtomStringError
 
+def _find_res(res, cls):
+	if isinstance(res, AndRestriction):
+		restrictions = res.restrictions
+	else:
+		restrictions = (res,)
+
+	for r in restrictions:
+		if isinstance(r, cls):
+			return r
+	else:
+		return None
+
 class PkgCorePackageKey(PMPackageKey):
 	def __init__(self, atom):
 		self._atom = atom
 
 class PkgCoreIncompletePackageKey(PMIncompletePackageKey):
 	def __init__(self, r):
-		if isinstance(r, AndRestriction):
-			for r in r.restrictions:
-				if isinstance(r, PackageDep):
-					break
-			else:
-				raise AssertionError('No PackageDep in restrictions.')
-
-		self._r = r
+		self._r = _find_res(r, PackageDep)
+		if self._r is None:
+			raise AssertionError('No PackageDep in restrictions.')
 
 	@property
 	def package(self):
 
 class PkgCoreIncompletePackageVersion(PMPackageVersion):
 	def __init__(self, r):
-		if isinstance(r, AndRestriction):
-			for r in r.restrictions:
-				if isinstance(r, VersionMatch):
-					break
-			else:
-				raise AssertionError('No VersionMatch in restrictions.')
-
-		self._r = r
+		self._r = _find_res(r, VersionMatch)
+		if self._r is None:
+			raise AssertionError('No VersionMatch in restrictions.')
 
 	@property
 	def without_revision(self):
 				return PkgCoreIncompletePackageVersion(self._r)
 		except AssertionError:
 			return None
+
+	@property
+	def slot(self):
+		if self.complete:
+			return self._r.slot[0] if self._r.slot \
+					else None
+		else:
+			r = _find_res(self._r, SlotDep)
+			return r.restriction.exact if r is not None \
+					else None
+
+	@property
+	def repository(self):
+		if self.complete:
+			return self._r.repo_id
+		else:
+			r = _find_res(self._r, RepositoryDep)
+			return r.restriction.exact if r is not None \
+					else None

gentoopm/portagepm/atom.py

 		return catsplit(self._cp)[1]
 
 	def __str__(self):
-		return str(self._cp)
+		return self._cp
 
 class PortageIncompletePackageKey(PMIncompletePackageKey, PortagePackageKey):
 	pass
 		else:
 			return PortagePackageVersion(self._atom.cpv)
 
+	@property
+	def slot(self):
+		return self._atom.slot
+
+	@property
+	def repository(self):
+		return self._atom.repo
+
 class UncategorisedPackageWrapper(object):
 	def __init__(self, pkg):
 		self._pkg = pkg

gentoopm/tests/atom.py

 		self.assertEqual(str(a.unversioned), cas)
 
 	def test_atom_parts(self):
-		a = self.pm.Atom('>=app-foo/bar-19-r1')
+		a = self.pm.Atom('>=app-foo/bar-19-r1:5::baz')
 		self.assertEqual(a.key.category, 'app-foo')
 		self.assertEqual(a.key.package, 'bar')
 		self.assertEqual(a.key, 'app-foo/bar')
 		self.assertEqual(a.version.without_revision, '19')
 		self.assertEqual(a.version.revision, 1)
 		self.assertEqual(a.version, '19-r1')
+		self.assertEqual(a.slot, '5')
+		self.assertEqual(a.repository, 'baz')
 
 	def test_atom_parts_incomplete(self):
-		a = self.pm.Atom('>=bar-19-r1')
+		a = self.pm.Atom('>=bar-19-r1:5::baz')
 		self.assertTrue(a.key.category is None)
 		self.assertEqual(a.key.package, 'bar')
 		self.assertEqual(a.key, 'bar')
 		self.assertEqual(a.version.without_revision, '19')
 		self.assertEqual(a.version.revision, 1)
 		self.assertEqual(a.version, '19-r1')
+		self.assertEqual(a.slot, '5')
+		self.assertEqual(a.repository, 'baz')
 
 	def test_atom_parts_without_rev(self):
-		a = self.pm.Atom('>=app-foo/bar-19')
+		a = self.pm.Atom('>=app-foo/bar-19:5::baz')
 		self.assertEqual(a.key.category, 'app-foo')
 		self.assertEqual(a.key.package, 'bar')
 		self.assertEqual(a.key, 'app-foo/bar')
 		self.assertEqual(a.version.without_revision, '19')
 		self.assertEqual(a.version.revision, 0)
 		self.assertEqual(a.version, '19')
+		self.assertEqual(a.slot, '5')
+		self.assertEqual(a.repository, 'baz')
 
 	def test_atom_parts_without_version(self):
+		a = self.pm.Atom('app-foo/bar:5::baz')
+		self.assertEqual(a.key.category, 'app-foo')
+		self.assertEqual(a.key.package, 'bar')
+		self.assertEqual(a.key, 'app-foo/bar')
+		self.assertTrue(a.version is None)
+		self.assertEqual(a.slot, '5')
+		self.assertEqual(a.repository, 'baz')
+
+	def test_atom_parts_without_slot(self):
+		a = self.pm.Atom('app-foo/bar::baz')
+		self.assertEqual(a.key.category, 'app-foo')
+		self.assertEqual(a.key.package, 'bar')
+		self.assertEqual(a.key, 'app-foo/bar')
+		self.assertTrue(a.version is None)
+		self.assertTrue(a.slot is None)
+		self.assertEqual(a.repository, 'baz')
+
+	def test_atom_parts_unversioned(self):
 		a = self.pm.Atom('app-foo/bar')
 		self.assertEqual(a.key.category, 'app-foo')
 		self.assertEqual(a.key.package, 'bar')
 		self.assertEqual(a.key, 'app-foo/bar')
 		self.assertTrue(a.version is None)
+		self.assertTrue(a.slot is None)
+		self.assertTrue(a.repository is None)
+
+	def test_atom_parts_dumb(self):
+		a = self.pm.Atom('bar')
+		self.assertTrue(a.key.category is None)
+		self.assertEqual(a.key.package, 'bar')
+		self.assertEqual(a.key, 'bar')
+		self.assertTrue(a.version is None)
+		self.assertTrue(a.slot is None)
+		self.assertTrue(a.repository is None)
 
 	def tearDown(self):
 		pass