3 # Copyright 2009-2010 Gentoo Foundation
5 # Licensed under the GNU General Public License, v2
9 """Provides attributes and methods for a category/package-version string."""
17 from portage.versions import catpkgsplit, vercmp
19 from gentoolkit import errors
26 """Provides methods on a category/package-version string.
28 Will also correctly split just a package or package-version string.
31 >>> from gentoolkit.cpv import CPV
32 >>> cpv = CPV('sys-apps/portage-2.2-r1')
33 >>> cpv.category, cpv.name, cpv.fullversion
34 ('sys-apps', 'portage', '2.2-r1')
36 'sys-apps/portage-2.2-r1'
37 >>> # An 'rc' (release candidate) version is less than non 'rc' version:
38 ... CPV('sys-apps/portage-2') > CPV('sys-apps/portage-2_rc10')
42 def __init__(self, cpv):
45 values = split_cpv(cpv)
46 self.category = values[0]
48 self.version = values[2]
49 self.revision = values[3]
53 raise errors.GentoolkitInvalidCPV(cpv)
55 sep = '/' if self.category else ''
56 self.cp = sep.join((self.category, self.name))
58 sep = '-' if self.revision else ''
59 self.fullversion = sep.join((self.version, self.revision))
62 def __eq__(self, other):
63 if not isinstance(other, self.__class__):
65 return self.cpv == other.cpv
67 def __ne__(self, other):
68 return not self == other
70 def __lt__(self, other):
71 if not isinstance(other, self.__class__):
72 raise TypeError("other isn't of %s type, is %s" % (
73 self.__class__, other.__class__)
76 if self.category != other.category:
77 return self.category < other.category
78 elif self.name != other.name:
79 return self.name < other.name
81 # FIXME: this cmp() hack is for vercmp not using -1,0,1
82 # See bug 266493; this was fixed in portage-2.2_rc31
83 #return vercmp(self.fullversion, other.fullversion)
84 result = cmp(vercmp(self.fullversion, other.fullversion), 0)
90 def __gt__(self, other):
91 if not isinstance(other, self.__class__):
92 raise TypeError("other isn't of %s type, is %s" % (
93 self.__class__, other.__class__)
95 return not self <= other
97 def __le__(self, other):
98 if not isinstance(other, self.__class__):
99 raise TypeError("other isn't of %s type, is %s" % (
100 self.__class__, other.__class__)
102 return self < other or self == other
104 def __ge__(self, other):
105 if not isinstance(other, self.__class__):
106 raise TypeError("other isn't of %s type, is %s" % (
107 self.__class__, other.__class__)
109 return self > other or self == other
112 return "<%s %r>" % (self.__class__.__name__, str(self))
123 """Split a cpv into category, name, version and revision.
125 Inlined from helpers because of circular imports.
127 @todo: this function is slow and accepts some crazy things for cpv
129 @param cpv: pkg, cat/pkg, pkg-ver, cat/pkg-ver, atom or regex
131 @return: (category, pkg_name, version, revision)
132 Each tuple element is a string or empty string ("").
135 result = catpkgsplit(cpv)
138 result = list(result)
139 if result[0] == 'null':
141 if result[3] == 'r0':
144 result = cpv.split("/")
146 result = ['', cpv, '', '']
148 result = result + ['', '']
152 # vim: set ts=4 sw=4 tw=79: