aboutsummaryrefslogtreecommitdiff
blob: c58243e372eafdf77ff6332c52b4157dc5f9f881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Copyright(c) 2005 Jason Stubbs <jstubbs@gentoo.org>
# Copyright(c) 2005-2006 Brian Harring <ferringb@gmail.com>
# Copyright(c) 2009-2010 Gentoo Foundation
#
# Licensed under the GNU General Public License, v2

"""Provides attributes and methods for a category/package-version string."""

__all__ = ("CPV", "compare_strs", "split_cpv")

# =======
# Imports
# =======

import re

from portage.versions import catpkgsplit, vercmp, pkgcmp

from gentoolkit import errors

# =======
# Globals
# =======

isvalid_version_re = re.compile(
    r"^(?:cvs\.)?(?:\d+)(?:\.\d+)*[a-z]?" r"(?:_(p(?:re)?|beta|alpha|rc)\d*)*$"
)
isvalid_cat_re = re.compile(r"^(?:[a-zA-Z0-9][-a-zA-Z0-9+._]*(?:/(?!$))?)+$")
_pkg_re = re.compile(r"^[a-zA-Z0-9+._]+$")
# Prefix specific revision is of the form -r0<digit>+.<digit>+
isvalid_rev_re = re.compile(r"(\d+|0\d+\.\d+)")

# =======
# Classes
# =======


class CPV:
    """Provides methods on a category/package-version string.

    Will also correctly split just a package or package-version string.

    Example usage:
            >>> from gentoolkit.cpv import CPV
            >>> cpv = CPV('sys-apps/portage-2.2-r1')
            >>> cpv.category, cpv.name, cpv.fullversion
            ('sys-apps', 'portage', '2.2-r1')
            >>> str(cpv)
            'sys-apps/portage-2.2-r1'
            >>> # An 'rc' (release candidate) version is less than non 'rc' version:
            ... CPV('sys-apps/portage-2') > CPV('sys-apps/portage-2_rc10')
            True
    """

    def __init__(self, cpv, validate=False):
        self.cpv = cpv
        self._category = None
        self._name = None
        self._version = None
        self._revision = None
        self._cp = None
        self._fullversion = None

        self.validate = validate
        if validate and not self.name:
            raise errors.GentoolkitInvalidCPV(cpv)

    @property
    def category(self):
        if self._category is None:
            self._set_cpv_chunks()
        return self._category

    @property
    def name(self):
        if self._name is None:
            self._set_cpv_chunks()
        return self._name

    @property
    def version(self):
        if self._version is None:
            self._set_cpv_chunks()
        return self._version

    @property
    def revision(self):
        if self._revision is None:
            self._set_cpv_chunks()
        return self._revision

    @property
    def cp(self):
        if self._cp is None:
            sep = "/" if self.category else ""
            self._cp = sep.join((self.category, self.name))
        return self._cp

    @property
    def fullversion(self):
        if self._fullversion is None:
            sep = "-" if self.revision else ""
            self._fullversion = sep.join((self.version, self.revision))
        return self._fullversion

    def _set_cpv_chunks(self):
        chunks = split_cpv(self.cpv, validate=self.validate)
        self._category = chunks[0]
        self._name = chunks[1]
        self._version = chunks[2]
        self._revision = chunks[3]

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False
        return self.cpv == other.cpv

    def __hash__(self):
        return hash(self.cpv)

    def __ne__(self, other):
        return not self == other

    def __lt__(self, other):
        if not isinstance(other, self.__class__):
            raise TypeError(
                f"other isn't of {self.__class__} type, is {other.__class__}"
            )

        if self.category != other.category:
            return self.category < other.category
        elif self.name != other.name:
            return self.name < other.name
        else:
            # FIXME: this cmp() hack is for vercmp not using -1,0,1
            # See bug 266493; this was fixed in portage-2.2_rc31
            # return vercmp(self.fullversion, other.fullversion)
            return vercmp(self.fullversion, other.fullversion) < 0

    def __gt__(self, other):
        if not isinstance(other, self.__class__):
            raise TypeError(
                f"other isn't of {self.__class__} type, is {other.__class__}"
            )
        return not self <= other

    def __le__(self, other):
        if not isinstance(other, self.__class__):
            raise TypeError(
                f"other isn't of {self.__class__} type, is {other.__class__}"
            )
        return self < other or self == other

    def __ge__(self, other):
        if not isinstance(other, self.__class__):
            raise TypeError(
                f"other isn't of {self.__class__} type, is {other.__class__}"
            )
        return self > other or self == other

    def __repr__(self):
        return f"<{self.__class__.__name__} {str(self)!r}>"

    def __str__(self):
        return self.cpv


# =========
# Functions
# =========


def compare_strs(pkg1, pkg2):
    """Similar to the builtin cmp, but for package strings. Usually called
    as: package_list.sort(cpv.compare_strs)

    An alternative is to use the CPV descriptor from gentoolkit.cpv:
    >>> package_list = ['sys-apps/portage-9999', 'media-video/ffmpeg-9999']
    >>> cpvs = sorted(CPV(x) for x in package_list)

    @see: >>> help(cmp)
    """

    pkg1 = catpkgsplit(pkg1)
    pkg2 = catpkgsplit(pkg2)
    if pkg1[0] != pkg2[0]:
        return -1 if pkg1[0] < pkg2[0] else 1
    elif pkg1[1] != pkg2[1]:
        return -1 if pkg1[1] < pkg2[1] else 1
    else:
        return pkgcmp(pkg1[1:], pkg2[1:])


def split_cpv(cpv, validate=True):
    """Split a cpv into category, name, version and revision.

    Modified from pkgcore.ebuild.cpv

    @type cpv: str
    @param cpv: pkg, cat/pkg, pkg-ver, cat/pkg-ver
    @rtype: tuple
    @return: (category, pkg_name, version, revision)
            Each tuple element is a string or empty string ("").
    """

    category = name = version = revision = ""

    try:
        category, pkgver = cpv.rsplit("/", 1)
    except ValueError:
        pkgver = cpv
    if validate and category and not isvalid_cat_re.match(category):
        raise errors.GentoolkitInvalidCPV(cpv)
    pkg_chunks = pkgver.split("-")
    lpkg_chunks = len(pkg_chunks)
    if lpkg_chunks == 1:
        return (category, pkg_chunks[0], version, revision)
    if isvalid_rev(pkg_chunks[-1]):
        if lpkg_chunks < 3:
            # needs at least ('pkg', 'ver', 'rev')
            raise errors.GentoolkitInvalidCPV(cpv)
        rev = pkg_chunks.pop(-1)
        if rev:
            revision = rev

    if isvalid_version_re.match(pkg_chunks[-1]):
        version = pkg_chunks.pop(-1)

    if not isvalid_pkg_name(pkg_chunks):
        raise errors.GentoolkitInvalidCPV(cpv)
    name = "-".join(pkg_chunks)

    return (category, name, version, revision)


def isvalid_pkg_name(chunks):
    if not chunks[0]:
        # this means a leading -
        return False
    mf = _pkg_re.match
    if not all(not s or mf(s) for s in chunks):
        return False
    if len(chunks) > 1 and chunks[-1].isdigit():
        # not allowed.
        return False
    return True


def isvalid_rev(s):
    return s and s[0] == "r" and isvalid_rev_re.match(s[1:])


# vim: set ts=4 sw=4 tw=79: