This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
semverpy.py
142 lines (113 loc) · 3.74 KB
/
semverpy.py
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
import re
__version__ = '0.1.1'
__author__ = 'Kasper Jacobsen'
__license__ = 'MIT'
__copyright__ = 'Copyright 2014 Kasper Jacobsen'
_start = r'^[v=]?'
_major = r'(?P<major>\d+)'
_minor = r'(\.(?P<minor>(\d+|[x*])))?'
_patch = r'(\.(?P<patch>(\d+|[x*])))?'
_build = r'(?:[:+-](?P<build>\w+))?'
_end = r'$'
_regex = _start + _major + _minor + _patch + _build + _end
class InvalidVersionException(Exception):
pass
class SemVerPy():
_pattern = re.compile(_regex, re.IGNORECASE)
def __init__(self, version, dependency=False):
version = self._parse(version, dependency)
self.dependency = dependency
if self.dependency:
fill = None
else:
fill = 0
self._major = version['major']
self._minor = version['minor'] or fill
self._patch = version['patch'] or fill
self._build = version['build']
def __str__(self):
res = '{major}.{minor}.{patch}'.format(
major=self._major,
minor=self._minor if self._minor is not None else 'x',
patch=self._patch if self._patch is not None else 'x',
)
if self._build:
res += '-{}'.format(self._build)
return res
def __repr__(self):
return '<{name}({info})>'.format(
name=self.__class__.__name__,
info=str(self),
)
def _tuple(self, fill=0):
return (
self._major or fill,
self._minor or fill,
self._patch or fill,
self._build or fill,
)
def satisfies(self, item):
for s, o in zip(self._tuple(fill='x'), item._tuple(fill='x')):
if o != 'x' and s != o:
return False
return True
def __eq__(self, other):
if not isinstance(other, SemVerPy):
return False
else:
return self._tuple() == other._tuple()
def __ne__(self, other):
return self._tuple() != other._tuple()
def __lt__(self, other):
if not isinstance(other, SemVerPy):
return False
else:
return self._tuple() < other._tuple()
def __gt__(self, other):
if not isinstance(other, SemVerPy):
return False
else:
return self._tuple() > other._tuple()
def __le__(self, other):
return self._tuple() <= other._tuple()
def __ge__(self, other):
return self._tuple() >= other._tuple()
def _get_dict(self, string):
return self._pattern.search(string).groupdict()
def _parse(self, version_string, dependency):
res = self._pattern.search(version_string)
if res is None:
msg = 'Not a valid version: {}'.format(version_string)
raise InvalidVersionException(msg)
else:
return self._convert_fields(res.groupdict(), dependency)
def _convert_fields(self, version_dict, dependency):
for key in ['major', 'minor', 'patch']:
value = version_dict[key]
if dependency:
if value == 'x':
version_dict[key] = None
else:
try:
version_dict[key] = int(version_dict[key])
except TypeError:
pass
return version_dict
def bump_major(self, build=None):
self._major += 1
self._minor = 0
self._patch = 0
self._build = build
return self
def bump_minor(self, build=None):
self._minor += 1
self._patch = 0
self._build = build
return self
def bump_patch(self, build=None):
self._patch += 1
self._build = build
return self
def set_build(self, build_string):
self._build = build_string
return self