Skip to content

Commit 6388ad9

Browse files
authored
Development versions support (#230)
* Add dev versioning * Update tests
1 parent e100ea3 commit 6388ad9

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ install_requires =
2929
dictdiffer
3030
importlib-resources
3131
psutil
32+
packaging
3233

3334
[options.packages.find]
3435
where = src

src/sinol_make/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def main_exn():
7979
def main():
8080
new_version = None
8181
try:
82+
if util.is_dev(__version__):
83+
print(util.warning('You are using a development version of sinol-make. '
84+
'It may be unstable and contain bugs.'))
8285
new_version = util.check_for_updates(__version__)
8386
main_exn()
8487
except argparse.ArgumentError as err:
@@ -92,6 +95,12 @@ def main():
9295
'https://github.com/sio2project/sinol-make/#reporting-bugs-and-contributing-code')
9396
finally:
9497
if new_version is not None:
95-
print(util.warning(
96-
f'New version of sinol-make is available (your version: {__version__}, available version: '
97-
f'{new_version}).\nYou can update it by running `pip3 install sinol-make --upgrade`.'))
98+
if not util.is_dev(new_version):
99+
print(util.warning(
100+
f'New version of sinol-make is available (your version: {__version__}, available version: '
101+
f'{new_version}).\nYou can update it by running `pip3 install sinol-make --upgrade`.'))
102+
elif util.is_dev(new_version):
103+
print(util.warning(
104+
f'New development version of sinol-make is available (your version: {__version__}, available '
105+
f'version: {new_version}).\nYou can update it by running `pip3 install sinol-make --pre --upgrade`.'
106+
))

src/sinol_make/util.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import multiprocessing
88
import resource
99
from typing import Union
10+
from packaging.version import parse as parse_version
1011

1112
from sinol_make.contest_types import get_contest_type
1213
from sinol_make.helpers import paths, cache
@@ -150,10 +151,18 @@ def import_importlib_resources():
150151
return importlib
151152

152153

153-
def check_for_updates(current_version) -> Union[str, None]:
154+
def is_dev(version):
155+
"""
156+
Function to check if the version is a development version.
157+
"""
158+
return parse_version(version).is_devrelease
159+
160+
161+
def check_for_updates(current_version, check=True) -> Union[str, None]:
154162
"""
155163
Function to check if there is a new version of sinol-make.
156164
:param current_version: current version of sinol-make
165+
:param check: whether to check for new version
157166
:return: returns new version if there is one, None otherwise
158167
"""
159168
importlib = import_importlib_resources()
@@ -164,8 +173,9 @@ def check_for_updates(current_version) -> Union[str, None]:
164173

165174
# We check for new version asynchronously, so that it doesn't slow down the program.
166175
# If the main process exits, the check_version process will also exit.
167-
process = multiprocessing.Process(target=check_version, daemon=True)
168-
process.start()
176+
if check:
177+
process = multiprocessing.Process(target=check_version, daemon=True)
178+
process.start()
169179
version_file = data_dir.joinpath("version")
170180

171181
try:
@@ -178,7 +188,9 @@ def check_for_updates(current_version) -> Union[str, None]:
178188
return None
179189

180190
try:
181-
if compare_versions(current_version, version) == -1:
191+
if not is_dev(version) and parse_version(version) > parse_version(current_version):
192+
return version
193+
if is_dev(current_version) and is_dev(version) and parse_version(version) > parse_version(current_version):
182194
return version
183195
else:
184196
return None
@@ -217,26 +229,6 @@ def check_version():
217229
pass
218230

219231

220-
def compare_versions(version_a, version_b):
221-
"""
222-
Function to compare two versions.
223-
Returns 1 if version_a > version_b, 0 if version_a == version_b, -1 if version_a < version_b.
224-
"""
225-
226-
def convert(version):
227-
return tuple(map(int, version.split(".")))
228-
229-
version_a = convert(version_a)
230-
version_b = convert(version_b)
231-
232-
if version_a > version_b:
233-
return 1
234-
elif version_a == version_b:
235-
return 0
236-
else:
237-
return -1
238-
239-
240232
def lines_diff(lines1, lines2):
241233
"""
242234
Function to compare two lists of lines.

tests/test_util.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,6 @@ def test_file_diff():
4444
assert util.file_diff(a_file, b_file) is False
4545

4646

47-
def test_compare_versions():
48-
"""
49-
Tests for compare_versions function
50-
"""
51-
52-
assert util.compare_versions('1.0.0', '1.0.0') == 0
53-
assert util.compare_versions('1.0.0', '1.0.1') == -1
54-
assert util.compare_versions('1.0.1', '1.0.0') == 1
55-
assert util.compare_versions('1.0.0', '1.1.0') == -1
56-
assert util.compare_versions('1.1.0', '1.0.0') == 1
57-
assert util.compare_versions('1.0.0', '2.0.0') == -1
58-
assert util.compare_versions('2.0.0', '1.0.0') == 1
59-
with pytest.raises(ValueError):
60-
util.compare_versions('1.0.0', '')
61-
with pytest.raises(ValueError):
62-
util.compare_versions('', '1.0.0')
63-
with pytest.raises(ValueError):
64-
util.compare_versions('1.0.0', 'abc')
65-
with pytest.raises(ValueError):
66-
util.compare_versions('abc', '1.0.0')
67-
68-
6947
@requests_mock.Mocker(kw="mocker")
7048
def test_check_version(**kwargs):
7149
"""
@@ -74,6 +52,29 @@ def test_check_version(**kwargs):
7452
"""
7553
mocker = kwargs["mocker"]
7654

55+
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.0.dev2"}})
56+
util.check_version()
57+
version = util.check_for_updates("1.0.0.dev1", False)
58+
assert version == "1.0.0.dev2"
59+
assert util.is_dev(version)
60+
61+
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.0"}})
62+
util.check_version()
63+
version = util.check_for_updates("1.0.0.dev1", False)
64+
assert version == "1.0.0"
65+
assert not util.is_dev(version)
66+
67+
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "2.0.0.dev1"}})
68+
util.check_version()
69+
version = util.check_for_updates("1.0.0", False)
70+
assert version is None
71+
72+
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.1"}})
73+
util.check_version()
74+
version = util.check_for_updates("1.0.0", False)
75+
assert version == "1.0.1"
76+
assert not util.is_dev(version)
77+
7778
importlib = util.import_importlib_resources()
7879

7980
data_dir = importlib.files('sinol_make').joinpath("data")

0 commit comments

Comments
 (0)