Skip to content

Commit e1551de

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

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

relenv/build/__init__.py

Lines changed: 1 addition & 7 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

relenv/pyversions.py

Lines changed: 90 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,87 @@ 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+
54+
@staticmethod
55+
def parse_string(data):
56+
"""
57+
Parse a version string into major, minor, and micro integers.
58+
"""
59+
parts = data.split(".")
60+
if len(parts) == 1:
61+
return int(parts[0]), None, None
62+
elif len(parts) == 2:
63+
return int(parts[0]), int(parts[1]), None
64+
elif len(parts) == 3:
65+
return int(parts[0]), int(parts[1]), int(parts[2])
66+
else:
67+
raise RuntimeError("Too many parts to parse")
68+
69+
def __eq__(self, other):
70+
"""
71+
Equality comparrison.
72+
"""
73+
mymajor = 0 if self.major is None else self.major
74+
myminor = 0 if self.minor is None else self.minor
75+
mymicro = 0 if self.micro is None else self.micro
76+
major = 0 if other.major is None else other.major
77+
minor = 0 if other.minor is None else other.minor
78+
micro = 0 if other.micro is None else other.micro
79+
return mymajor == major and myminor == minor and mymicro == micro
80+
81+
def __lt__(self, other):
82+
"""
83+
Less than comparrison.
84+
"""
85+
mymajor = 0 if self.major is None else self.major
86+
myminor = 0 if self.minor is None else self.minor
87+
mymicro = 0 if self.micro is None else self.micro
88+
major = 0 if other.major is None else other.major
89+
minor = 0 if other.minor is None else other.minor
90+
micro = 0 if other.micro is None else other.micro
91+
if mymajor < major:
92+
return True
93+
elif mymajor == major:
94+
if myminor < minor:
95+
return True
96+
if myminor == minor and mymicro < micro:
97+
return True
98+
return False
99+
100+
def __le__(self, other):
101+
"""
102+
Less than or equal to comparrison.
103+
"""
104+
mymajor = 0 if self.major is None else self.major
105+
myminor = 0 if self.minor is None else self.minor
106+
mymicro = 0 if self.micro is None else self.micro
107+
major = 0 if other.major is None else other.major
108+
minor = 0 if other.minor is None else other.minor
109+
micro = 0 if other.micro is None else other.micro
110+
if mymajor <= major:
111+
if myminor <= minor:
112+
if mymicro <= micro:
113+
return True
114+
return False
115+
116+
def __gt__(self, other):
117+
"""
118+
Greater than comparrison.
119+
"""
120+
return not self.__le__(other)
121+
122+
def __ge__(self, other):
123+
"""
124+
Greater than or equal to comparrison.
125+
"""
126+
return not self.__lt__(other)
45127

46128

47129
def _release_urls(version, gzip=False):

0 commit comments

Comments
 (0)