Skip to content

Commit

Permalink
add find_latest_version
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco086 committed Jan 29, 2024
1 parent 60a5bc0 commit 1a6ec88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/dac/_version_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
import subprocess
from typing import Optional


def find_latest_version(library_name: str, major: Optional[int] = None) -> str:
output = subprocess.check_output(
[
"pip",
"install",
"--no-deps",
"--ignore-installed",
"--no-cache-dir",
"--dry-run",
f"{library_name}{f'=={major}.*' if major is not None else ''}",
],
stderr=subprocess.DEVNULL,
)
last_line = output.decode("utf-8").splitlines()[-1]
regex_rule = f"{library_name.replace('_', '-')}-{major if major is not None else ''}.[^ ]+"
match = re.search(regex_rule, last_line)
assert match is not None
return match[0][len(f"{library_name}-") :]
15 changes: 15 additions & 0 deletions test/integration_test/version_management_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from dac._version_management import find_latest_version


def test_if_find_latest_version_is_called_then_return_latest_version():
assert "0.5" == find_latest_version(library_name="rainbow-server")


def test_if_find_latest_version_is_called_with_major_constraint_then_return_latest_major_version():
assert "0.25.3" == find_latest_version(library_name="pandas", major=0)


def test_if_pkg_does_not_exist_then_find_package_return_none():
with pytest.raises(Exception):
find_latest_version(library_name="non-existing-package")

0 comments on commit 1a6ec88

Please sign in to comment.