-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from ssciwr/fix_187_auto_update
Add update functionality
- Loading branch information
Showing
8 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"__version__", | ||
] | ||
|
||
__version__ = "0.26.0" | ||
__version__ = "0.27.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import subprocess | ||
import sys | ||
from typing import Tuple | ||
|
||
import requests | ||
import vstt | ||
from packaging import version | ||
|
||
|
||
def _get_latest_pypi_version() -> version.Version: | ||
logging.info("Requesting latest version from pypi...") | ||
r = requests.get("https://pypi.org/pypi/vstt/json", timeout=5) | ||
version_str = r.json().get("info", {}).get("version", "") | ||
ver = version.parse(version_str) | ||
logging.info(f" -> {ver}") | ||
return ver | ||
|
||
|
||
def check_for_new_version() -> Tuple[bool, str]: | ||
try: | ||
current_version = version.parse(vstt.__version__) | ||
logging.info(f"Current version: {current_version}") | ||
latest_version = _get_latest_pypi_version() | ||
logging.info(f"Latest version: {latest_version}") | ||
if latest_version > current_version: | ||
return ( | ||
True, | ||
f"Latest version of VSTT: {latest_version}.\nYou currently have version {current_version}.\nWould you like to upgrade now?", | ||
) | ||
else: | ||
return ( | ||
False, | ||
"You have the latest version of VSTT.", | ||
) | ||
except Exception as e: | ||
logging.exception(e) | ||
return ( | ||
False, | ||
"An error occurred while checking for updates, please try again later.", | ||
) | ||
|
||
|
||
def do_pip_upgrade() -> Tuple[bool, str]: | ||
logging.info(f"Doing pip upgrade using {sys.executable}...") | ||
try: | ||
subprocess.check_call( | ||
[sys.executable, "-m", "pip", "install", "--upgrade", "vstt"] | ||
) | ||
logging.info("done.") | ||
return ( | ||
True, | ||
"VSTT has been updated.\nPlease close the program and open it again to see the latest version.", | ||
) | ||
except Exception as e: | ||
logging.exception(e) | ||
return False, f"An error occurred when trying to update VSTT: {e}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
|
||
import vstt | ||
from pytest import MonkeyPatch | ||
from vstt.update import check_for_new_version | ||
from vstt.update import do_pip_upgrade | ||
|
||
|
||
def test_new_version_available(monkeypatch: MonkeyPatch) -> None: | ||
monkeypatch.setattr(vstt, "__version__", "0.0.0") | ||
available, message = check_for_new_version() | ||
assert available is True | ||
assert "0.0.0" in message | ||
assert "upgrade" in message | ||
monkeypatch.setattr(vstt, "__version__", "9999.0.0") | ||
available, message = check_for_new_version() | ||
assert available is False | ||
assert "latest version" in message | ||
monkeypatch.setattr(vstt, "__version__", "imnot-a-valid-version!") | ||
available, message = check_for_new_version() | ||
assert available is False | ||
assert "error" in message | ||
|
||
|
||
def test_do_pip_upgrade(monkeypatch: MonkeyPatch) -> None: | ||
monkeypatch.setattr(sys, "executable", "abc123_i_dont_exist") | ||
success, message = do_pip_upgrade() | ||
assert success is False | ||
assert "error" in message |