Skip to content

Commit 61e8760

Browse files
committed
Add our own version class
1 parent ae27829 commit 61e8760

File tree

2 files changed

+104
-16
lines changed

2 files changed

+104
-16
lines changed

relenv/build/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
"""
44
The ``relenv build`` command.
55
"""
6-
try:
7-
from packaging.version import Version
8-
except ImportError:
9-
raise RuntimeError(
10-
"Required dependencies not found. Please pip install relenv[pyversions]"
11-
)
126
import sys
137
import random
148
import codecs
@@ -17,7 +11,7 @@
1711
from . import linux, darwin, windows
1812
from .common import builds, CHECK_VERSIONS_SUPPORT
1913

20-
from ..pyversions import python_versions
14+
from ..pyversions import python_versions, Version
2115

2216
from ..common import build_arch
2317

@@ -166,7 +160,6 @@ def main(args):
166160
# print(pyversions[0].post)
167161
# print(pyversions)
168162
print(f"Build Python {build_version}")
169-
sys.exit()
170163

171164
# XXX
172165
build = builds.builds[sys.platform]

relenv/pyversions.py

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"""
44
Versions utility.
55
"""
6-
try:
7-
from packaging.version import Version
8-
except ImportError:
9-
raise RuntimeError(
10-
"Required dependencies not found. Please pip install relenv[pyversions]"
11-
)
12-
6+
# try:
7+
# from packaging.version import Version
8+
# except ImportError:
9+
# raise RuntimeError(
10+
# "Required dependencies not found. Please pip install relenv[pyversions]"
11+
# )
12+
#
1313

1414
import hashlib
1515
import json
@@ -31,6 +31,8 @@
3131
"pgp.mit.edu",
3232
]
3333

34+
ARCHIVE = "https://www.python.org/ftp/python/{version}/Python-{version}.{ext}"
35+
3436

3537
def _ref_version(x):
3638
_ = x.split("Python ", 1)[1].split("<", 1)[0]
@@ -41,7 +43,100 @@ def _ref_path(x):
4143
return x.split('href="')[1].split('"')[0]
4244

4345

44-
ARCHIVE = "https://www.python.org/ftp/python/{version}/Python-{version}.{ext}"
46+
class Version:
47+
"""
48+
Version comparrisons.
49+
"""
50+
51+
def __init__(self, data):
52+
self.major, self.minor, self.micro = self.parse_string(data)
53+
self._data = data
54+
55+
def __str__(self):
56+
"""
57+
Version as string.
58+
"""
59+
_ = f"{self.major}"
60+
if self.minor is not None:
61+
_ += f".{self.minor}"
62+
if self.micro is not None:
63+
_ += f".{self.micro}"
64+
# XXX What if minor was None but micro was an int.
65+
return _
66+
67+
@staticmethod
68+
def parse_string(data):
69+
"""
70+
Parse a version string into major, minor, and micro integers.
71+
"""
72+
parts = data.split(".")
73+
if len(parts) == 1:
74+
return int(parts[0]), None, None
75+
elif len(parts) == 2:
76+
return int(parts[0]), int(parts[1]), None
77+
elif len(parts) == 3:
78+
return int(parts[0]), int(parts[1]), int(parts[2])
79+
else:
80+
raise RuntimeError("Too many parts to parse")
81+
82+
def __eq__(self, other):
83+
"""
84+
Equality comparrison.
85+
"""
86+
mymajor = 0 if self.major is None else self.major
87+
myminor = 0 if self.minor is None else self.minor
88+
mymicro = 0 if self.micro is None else self.micro
89+
major = 0 if other.major is None else other.major
90+
minor = 0 if other.minor is None else other.minor
91+
micro = 0 if other.micro is None else other.micro
92+
return mymajor == major and myminor == minor and mymicro == micro
93+
94+
def __lt__(self, other):
95+
"""
96+
Less than comparrison.
97+
"""
98+
mymajor = 0 if self.major is None else self.major
99+
myminor = 0 if self.minor is None else self.minor
100+
mymicro = 0 if self.micro is None else self.micro
101+
major = 0 if other.major is None else other.major
102+
minor = 0 if other.minor is None else other.minor
103+
micro = 0 if other.micro is None else other.micro
104+
if mymajor < major:
105+
return True
106+
elif mymajor == major:
107+
if myminor < minor:
108+
return True
109+
if myminor == minor and mymicro < micro:
110+
return True
111+
return False
112+
113+
def __le__(self, other):
114+
"""
115+
Less than or equal to comparrison.
116+
"""
117+
mymajor = 0 if self.major is None else self.major
118+
myminor = 0 if self.minor is None else self.minor
119+
mymicro = 0 if self.micro is None else self.micro
120+
major = 0 if other.major is None else other.major
121+
minor = 0 if other.minor is None else other.minor
122+
micro = 0 if other.micro is None else other.micro
123+
if mymajor <= major:
124+
if myminor <= minor:
125+
if mymicro <= micro:
126+
return True
127+
return False
128+
129+
def __gt__(self, other):
130+
"""
131+
Greater than comparrison.
132+
"""
133+
return not self.__le__(other)
134+
135+
def __ge__(self, other):
136+
"""
137+
Greater than or equal to comparrison.
138+
"""
139+
return not self.__lt__(other)
45140

46141

47142
def _release_urls(version, gzip=False):

0 commit comments

Comments
 (0)