2020-05-12 06:13:42 +00:00
|
|
|
"""
|
|
|
|
auto_update.py
|
|
|
|
checks version and auto updates
|
|
|
|
"""
|
2020-05-23 22:14:09 +00:00
|
|
|
import logging
|
2020-04-19 11:52:42 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import urllib.request
|
|
|
|
from os import execl
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2021-05-09 07:05:51 +00:00
|
|
|
|
2021-02-14 18:09:29 +00:00
|
|
|
def _hr_version(v):
|
|
|
|
return '.'.join([str(x) for x in v])
|
|
|
|
|
2020-04-19 11:52:42 +00:00
|
|
|
|
|
|
|
def _normalize_version(v):
|
2020-05-12 06:13:42 +00:00
|
|
|
"""
|
|
|
|
converts version string into an "normalized" of versions which is a list of version codes,
|
|
|
|
eg, input: '0.3.0', output: [0,3,0]
|
|
|
|
this is done so that, versions can be compared easily
|
|
|
|
:param v: string
|
|
|
|
:return: list
|
|
|
|
"""
|
2020-04-19 11:52:42 +00:00
|
|
|
rv = []
|
|
|
|
for x in v.split("."):
|
|
|
|
try:
|
|
|
|
rv.append(int(x))
|
|
|
|
except ValueError:
|
|
|
|
for y in re.split("([0-9]+)", x):
|
|
|
|
try:
|
|
|
|
if y != '':
|
|
|
|
rv.append(int(y))
|
|
|
|
except ValueError:
|
|
|
|
rv.append(y)
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
|
|
def _get_highest_version(index, pkg):
|
2020-05-12 06:13:42 +00:00
|
|
|
"""
|
|
|
|
Crawls web for latest version name then returns latest version
|
|
|
|
:param index: website to check
|
|
|
|
:param pkg: package name
|
|
|
|
:return: latest version normalized
|
|
|
|
"""
|
2020-04-19 11:52:42 +00:00
|
|
|
url = "{}/{}/".format(index, pkg)
|
|
|
|
html = urllib.request.urlopen(url)
|
|
|
|
if html.getcode() != 200:
|
|
|
|
raise Exception # not found
|
2020-06-25 18:43:53 +00:00
|
|
|
soup = BeautifulSoup(html.read(), "html.parser")
|
2020-04-19 11:52:42 +00:00
|
|
|
versions = []
|
|
|
|
for link in soup.find_all('a'):
|
|
|
|
text = link.get_text()
|
|
|
|
try:
|
2020-10-14 02:29:06 +00:00
|
|
|
version = re.search(pkg + r'-(.*)\.tar\.gz', text).group(1)
|
2020-04-19 11:52:42 +00:00
|
|
|
versions.append(_normalize_version(version))
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if len(versions) == 0:
|
|
|
|
raise Exception # no version
|
|
|
|
return max(versions)
|
|
|
|
|
|
|
|
|
2020-06-25 18:43:53 +00:00
|
|
|
def _get_current_version():
|
2020-05-12 06:13:42 +00:00
|
|
|
"""
|
|
|
|
Gets the current version of the package installed
|
|
|
|
:return: version normalized
|
|
|
|
"""
|
2020-06-25 18:43:53 +00:00
|
|
|
import fishy
|
|
|
|
return _normalize_version(fishy.__version__)
|
2020-04-19 11:52:42 +00:00
|
|
|
|
|
|
|
|
2021-02-14 18:09:29 +00:00
|
|
|
index = "https://pypi.python.org/simple"
|
|
|
|
pkg = "fishy"
|
|
|
|
|
2021-05-09 09:09:26 +00:00
|
|
|
|
2021-02-14 18:09:29 +00:00
|
|
|
def versions():
|
|
|
|
return _hr_version(_get_current_version()), _hr_version(_get_highest_version(index, pkg))
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade_avail():
|
|
|
|
"""
|
|
|
|
Checks if update is available
|
|
|
|
:return: boolean
|
|
|
|
"""
|
|
|
|
return _get_current_version() < _get_highest_version(index, pkg)
|
|
|
|
|
|
|
|
|
2022-02-02 21:10:22 +00:00
|
|
|
def update_now(version):
|
2020-05-12 06:13:42 +00:00
|
|
|
"""
|
|
|
|
public function,
|
|
|
|
compares current version with the latest version (from web),
|
|
|
|
if current version is older, then it updates and restarts the script
|
|
|
|
"""
|
2021-02-14 18:09:29 +00:00
|
|
|
logging.info(f"Updating to v{version}, Please Wait...")
|
|
|
|
subprocess.call(["python", '-m', 'pip', 'install', '--upgrade', 'fishy', '--user'])
|
2021-02-15 14:49:10 +00:00
|
|
|
execl(sys.executable, *([sys.executable, '-m', 'fishy'] + sys.argv[1:]))
|