-
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.
- Loading branch information
Showing
7 changed files
with
217 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
from distutils.version import LooseVersion | ||
from threading import Thread | ||
|
||
import pkg_resources | ||
import requests | ||
|
||
|
||
class Updater(Thread): | ||
def __init__(self): | ||
super().__init__() | ||
self.daemon = True | ||
self.latest_version = None | ||
self.installed_version = None | ||
self.finished = False | ||
self.start() | ||
|
||
def run(self): | ||
self._get_latest_version() | ||
self._get_installed_version() | ||
self.finished = True | ||
|
||
@property | ||
def needs_update(self) -> bool: | ||
if not self.latest_version or not self.installed_version: | ||
return False | ||
|
||
return self.latest_version > self.installed_version | ||
|
||
def _get_latest_version(self): | ||
try: | ||
r = requests.get("https://api.github.com/repos/OTTOMATIC-IO/ocopy/releases/latest") | ||
r.raise_for_status() | ||
self.latest_version = LooseVersion(r.json().get("tag_name")) | ||
except requests.exceptions.RequestException: | ||
self.finished = True | ||
|
||
def _get_installed_version(self): | ||
try: | ||
self.installed_version = LooseVersion(pkg_resources.get_distribution("ocopy").version) | ||
except pkg_resources.DistributionNotFound: | ||
self.finished = True |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import time | ||
from distutils.version import LooseVersion | ||
|
||
import requests | ||
|
||
|
||
def test_updater(requests_mock, mocker): | ||
class Distribution: | ||
def __init__(self, _): | ||
self.version = "0.0.1" | ||
|
||
mocker.patch("pkg_resources.get_distribution", Distribution) | ||
requests_mock.get("https://api.github.com/repos/OTTOMATIC-IO/ocopy/releases/latest", json={"tag_name": "0.6.5"}) | ||
|
||
from importlib import reload | ||
import ocopy.cli.update | ||
|
||
reload(ocopy.cli.update) | ||
|
||
updater = ocopy.cli.update.Updater() | ||
while not updater.finished: | ||
time.sleep(0.1) | ||
|
||
assert updater.latest_version == LooseVersion("0.6.5") | ||
assert updater.installed_version == LooseVersion("0.0.1") | ||
assert updater.needs_update is True | ||
|
||
|
||
def test_updater_timeout(requests_mock, mocker): | ||
class Distribution: | ||
def __init__(self, _): | ||
self.version = "0.0.1" | ||
|
||
mocker.patch("pkg_resources.get_distribution", Distribution) | ||
|
||
requests_mock.get( | ||
"https://api.github.com/repos/OTTOMATIC-IO/ocopy/releases/latest", exc=requests.exceptions.ConnectTimeout | ||
) | ||
|
||
from importlib import reload | ||
import ocopy.cli.update | ||
|
||
reload(ocopy.cli.update) | ||
|
||
updater = ocopy.cli.update.Updater() | ||
while not updater.finished: | ||
time.sleep(0.1) | ||
|
||
assert updater.latest_version is None | ||
assert updater.needs_update is False |