Skip to content

Commit 30430bc

Browse files
committed
MNT: Vendor distutils.version to ease transition
1 parent f662acf commit 30430bc

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

nipype/external/version.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# This module has been vendored from CPython distutils/version.py
2+
# last updated in 662db125cddbca1db68116c547c290eb3943d98e
3+
#
4+
# It is licensed according to the Python Software Foundation License Version 2
5+
# which may be found in full in the following (hopefully persistent) locations:
6+
#
7+
# https://github.com/python/cpython/blob/main/LICENSE
8+
# https://spdx.org/licenses/Python-2.0.html
9+
#
10+
# The following changes have been made:
11+
#
12+
# 2022.04.27 - Minor changes are made to the comments,
13+
# - The StrictVersion class was removed
14+
# - Black styling was applied
15+
#
16+
17+
# distutils/version.py
18+
#
19+
# Implements multiple version numbering conventions for the
20+
# Python Module Distribution Utilities.
21+
22+
"""Provides classes to represent module version numbers (one class for
23+
each style of version numbering). There are currently two such classes
24+
implemented: StrictVersion and LooseVersion.
25+
26+
Every version number class implements the following interface:
27+
* the 'parse' method takes a string and parses it to some internal
28+
representation; if the string is an invalid version number,
29+
'parse' raises a ValueError exception
30+
* the class constructor takes an optional string argument which,
31+
if supplied, is passed to 'parse'
32+
* __str__ reconstructs the string that was passed to 'parse' (or
33+
an equivalent string -- ie. one that will generate an equivalent
34+
version number instance)
35+
* __repr__ generates Python code to recreate the version number instance
36+
* _cmp compares the current instance with either another instance
37+
of the same class or a string (which will be parsed to an instance
38+
of the same class, thus must follow the same rules)
39+
"""
40+
41+
import re
42+
43+
44+
class Version:
45+
"""Abstract base class for version numbering classes. Just provides
46+
constructor (__init__) and reproducer (__repr__), because those
47+
seem to be the same for all version numbering classes; and route
48+
rich comparisons to _cmp.
49+
"""
50+
51+
def __init__(self, vstring=None):
52+
if vstring:
53+
self.parse(vstring)
54+
55+
def __repr__(self):
56+
return "%s ('%s')" % (self.__class__.__name__, str(self))
57+
58+
def __eq__(self, other):
59+
c = self._cmp(other)
60+
if c is NotImplemented:
61+
return c
62+
return c == 0
63+
64+
def __lt__(self, other):
65+
c = self._cmp(other)
66+
if c is NotImplemented:
67+
return c
68+
return c < 0
69+
70+
def __le__(self, other):
71+
c = self._cmp(other)
72+
if c is NotImplemented:
73+
return c
74+
return c <= 0
75+
76+
def __gt__(self, other):
77+
c = self._cmp(other)
78+
if c is NotImplemented:
79+
return c
80+
return c > 0
81+
82+
def __ge__(self, other):
83+
c = self._cmp(other)
84+
if c is NotImplemented:
85+
return c
86+
return c >= 0
87+
88+
89+
# The rules according to Greg Stein:
90+
# 1) a version number has 1 or more numbers separated by a period or by
91+
# sequences of letters. If only periods, then these are compared
92+
# left-to-right to determine an ordering.
93+
# 2) sequences of letters are part of the tuple for comparison and are
94+
# compared lexicographically
95+
# 3) recognize the numeric components may have leading zeroes
96+
#
97+
# The LooseVersion class below implements these rules: a version number
98+
# string is split up into a tuple of integer and string components, and
99+
# comparison is a simple tuple comparison. This means that version
100+
# numbers behave in a predictable and obvious way, but a way that might
101+
# not necessarily be how people *want* version numbers to behave. There
102+
# wouldn't be a problem if people could stick to purely numeric version
103+
# numbers: just split on period and compare the numbers as tuples.
104+
# However, people insist on putting letters into their version numbers;
105+
# the most common purpose seems to be:
106+
# - indicating a "pre-release" version
107+
# ('alpha', 'beta', 'a', 'b', 'pre', 'p')
108+
# - indicating a post-release patch ('p', 'pl', 'patch')
109+
# but of course this can't cover all version number schemes, and there's
110+
# no way to know what a programmer means without asking him.
111+
#
112+
# The problem is what to do with letters (and other non-numeric
113+
# characters) in a version number. The current implementation does the
114+
# obvious and predictable thing: keep them as strings and compare
115+
# lexically within a tuple comparison. This has the desired effect if
116+
# an appended letter sequence implies something "post-release":
117+
# eg. "0.99" < "0.99pl14" < "1.0", and "5.001" < "5.001m" < "5.002".
118+
#
119+
# However, if letters in a version number imply a pre-release version,
120+
# the "obvious" thing isn't correct. Eg. you would expect that
121+
# "1.5.1" < "1.5.2a2" < "1.5.2", but under the tuple/lexical comparison
122+
# implemented here, this just isn't so.
123+
#
124+
# Two possible solutions come to mind. The first is to tie the
125+
# comparison algorithm to a particular set of semantic rules, as has
126+
# been done in the StrictVersion class above. This works great as long
127+
# as everyone can go along with bondage and discipline. Hopefully a
128+
# (large) subset of Python module programmers will agree that the
129+
# particular flavour of bondage and discipline provided by StrictVersion
130+
# provides enough benefit to be worth using, and will submit their
131+
# version numbering scheme to its domination. The free-thinking
132+
# anarchists in the lot will never give in, though, and something needs
133+
# to be done to accommodate them.
134+
#
135+
# Perhaps a "moderately strict" version class could be implemented that
136+
# lets almost anything slide (syntactically), and makes some heuristic
137+
# assumptions about non-digits in version number strings. This could
138+
# sink into special-case-hell, though; if I was as talented and
139+
# idiosyncratic as Larry Wall, I'd go ahead and implement a class that
140+
# somehow knows that "1.2.1" < "1.2.2a2" < "1.2.2" < "1.2.2pl3", and is
141+
# just as happy dealing with things like "2g6" and "1.13++". I don't
142+
# think I'm smart enough to do it right though.
143+
#
144+
# In any case, I've coded the test suite for this module (see
145+
# ../test/test_version.py) specifically to fail on things like comparing
146+
# "1.2a2" and "1.2". That's not because the *code* is doing anything
147+
# wrong, it's because the simple, obvious design doesn't match my
148+
# complicated, hairy expectations for real-world version numbers. It
149+
# would be a snap to fix the test suite to say, "Yep, LooseVersion does
150+
# the Right Thing" (ie. the code matches the conception). But I'd rather
151+
# have a conception that matches common notions about version numbers.
152+
153+
154+
class LooseVersion(Version):
155+
156+
"""Version numbering for anarchists and software realists.
157+
Implements the standard interface for version number classes as
158+
described above. A version number consists of a series of numbers,
159+
separated by either periods or strings of letters. When comparing
160+
version numbers, the numeric components will be compared
161+
numerically, and the alphabetic components lexically. The following
162+
are all valid version numbers, in no particular order:
163+
164+
1.5.1
165+
1.5.2b2
166+
161
167+
3.10a
168+
8.02
169+
3.4j
170+
1996.07.12
171+
3.2.pl0
172+
3.1.1.6
173+
2g6
174+
11g
175+
0.960923
176+
2.2beta29
177+
1.13++
178+
5.5.kw
179+
2.0b1pl0
180+
181+
In fact, there is no such thing as an invalid version number under
182+
this scheme; the rules for comparison are simple and predictable,
183+
but may not always give the results you want (for some definition
184+
of "want").
185+
"""
186+
187+
component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE)
188+
189+
def __init__(self, vstring=None):
190+
if vstring:
191+
self.parse(vstring)
192+
193+
def parse(self, vstring):
194+
# I've given up on thinking I can reconstruct the version string
195+
# from the parsed tuple -- so I just store the string here for
196+
# use by __str__
197+
self.vstring = vstring
198+
components = [x for x in self.component_re.split(vstring) if x and x != '.']
199+
for i, obj in enumerate(components):
200+
try:
201+
components[i] = int(obj)
202+
except ValueError:
203+
pass
204+
205+
self.version = components
206+
207+
def __str__(self):
208+
return self.vstring
209+
210+
def __repr__(self):
211+
return "LooseVersion ('%s')" % str(self)
212+
213+
def _cmp(self, other):
214+
if isinstance(other, str):
215+
other = LooseVersion(other)
216+
elif not isinstance(other, LooseVersion):
217+
return NotImplemented
218+
219+
if self.version == other.version:
220+
return 0
221+
if self.version < other.version:
222+
return -1
223+
if self.version > other.version:
224+
return 1

0 commit comments

Comments
 (0)