2021-11-04 05:38:55 -07:00
|
|
|
import os
|
|
|
|
import re
|
2022-02-12 02:20:09 -08:00
|
|
|
import sys
|
|
|
|
import json
|
2021-11-04 05:38:55 -07:00
|
|
|
|
|
|
|
from .constants import BASE_DIR
|
|
|
|
try:
|
|
|
|
from importlib_metadata import version
|
|
|
|
importlib = True
|
|
|
|
ImportNotFound = BaseException
|
|
|
|
except ImportError:
|
|
|
|
importlib = False
|
2022-02-12 02:20:09 -08:00
|
|
|
version = None
|
2021-11-04 05:38:55 -07:00
|
|
|
|
|
|
|
if not importlib:
|
|
|
|
try:
|
|
|
|
import pkg_resources
|
|
|
|
from pkg_resources import DistributionNotFound as ImportNotFound
|
|
|
|
pkgresources = True
|
|
|
|
except ImportError as e:
|
|
|
|
pkgresources = False
|
|
|
|
|
2021-11-06 09:17:00 -07:00
|
|
|
def load_dependencys(optional=False):
|
|
|
|
deps = list()
|
2022-02-12 02:20:09 -08:00
|
|
|
if getattr(sys, 'frozen', False):
|
2022-02-12 02:58:56 -08:00
|
|
|
pip_installed = os.path.join(BASE_DIR, ".pip_installed")
|
|
|
|
if os.path.exists(pip_installed):
|
|
|
|
with open(pip_installed) as f:
|
|
|
|
exe_deps = json.loads("".join(f.readlines()))
|
|
|
|
else:
|
|
|
|
return deps
|
2021-11-04 05:38:55 -07:00
|
|
|
if importlib or pkgresources:
|
|
|
|
if optional:
|
|
|
|
req_path = os.path.join(BASE_DIR, "optional-requirements.txt")
|
|
|
|
else:
|
|
|
|
req_path = os.path.join(BASE_DIR, "requirements.txt")
|
|
|
|
if os.path.exists(req_path):
|
2021-11-06 09:17:00 -07:00
|
|
|
with open(req_path, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
if not line.startswith('#') and not line == '\n' and not line.startswith('git'):
|
|
|
|
res = re.match(r'(.*?)([<=>\s]+)([\d\.]+),?\s?([<=>\s]+)?([\d\.]+)?', line.strip())
|
|
|
|
try:
|
2022-02-12 02:20:09 -08:00
|
|
|
if getattr(sys, 'frozen', False):
|
2022-02-12 02:58:56 -08:00
|
|
|
dep_version = exe_deps[res.group(1).lower().replace('_','-')]
|
2021-11-06 09:17:00 -07:00
|
|
|
else:
|
2022-02-12 02:20:09 -08:00
|
|
|
if importlib:
|
|
|
|
dep_version = version(res.group(1))
|
|
|
|
else:
|
|
|
|
dep_version = pkg_resources.get_distribution(res.group(1)).version
|
2022-02-12 02:58:56 -08:00
|
|
|
except (ImportNotFound, KeyError):
|
2021-11-06 09:17:00 -07:00
|
|
|
if optional:
|
|
|
|
continue
|
2021-11-06 10:17:48 -07:00
|
|
|
dep_version = "not installed"
|
2021-11-06 09:17:00 -07:00
|
|
|
deps.append([dep_version, res.group(1), res.group(2), res.group(3), res.group(4), res.group(5)])
|
|
|
|
return deps
|
|
|
|
|
2021-11-04 05:38:55 -07:00
|
|
|
|
2021-11-06 09:17:00 -07:00
|
|
|
def dependency_check(optional=False):
|
|
|
|
d = list()
|
|
|
|
deps = load_dependencys(optional)
|
|
|
|
for dep in deps:
|
|
|
|
try:
|
2021-11-06 10:17:48 -07:00
|
|
|
dep_version_int = [int(x) for x in dep[0].split('.')]
|
|
|
|
low_check = [int(x) for x in dep[3].split('.')]
|
2021-11-06 09:17:00 -07:00
|
|
|
high_check = [int(x) for x in dep[5].split('.')]
|
|
|
|
except AttributeError:
|
|
|
|
high_check = None
|
2021-11-06 10:17:48 -07:00
|
|
|
except ValueError:
|
|
|
|
d.append({'name': dep[1],
|
|
|
|
'target': "available",
|
|
|
|
'found': "Not available"
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
|
2021-11-06 09:17:00 -07:00
|
|
|
if dep[2].strip() == "==":
|
|
|
|
if dep_version_int != low_check:
|
|
|
|
d.append({'name': dep[1],
|
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
|
|
|
continue
|
|
|
|
elif dep[2].strip() == ">=":
|
|
|
|
if dep_version_int < low_check:
|
|
|
|
d.append({'name': dep[1],
|
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
|
|
|
continue
|
|
|
|
elif dep[2].strip() == ">":
|
|
|
|
if dep_version_int <= low_check:
|
|
|
|
d.append({'name': dep[1],
|
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
|
|
|
continue
|
|
|
|
if dep[4] and dep[5]:
|
|
|
|
if dep[4].strip() == "<":
|
|
|
|
if dep_version_int >= high_check:
|
|
|
|
d.append(
|
|
|
|
{'name': dep[1],
|
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[4] + dep[5]})
|
|
|
|
continue
|
|
|
|
elif dep[4].strip() == "<=":
|
|
|
|
if dep_version_int > high_check:
|
|
|
|
d.append(
|
|
|
|
{'name': dep[1],
|
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[4] + dep[5]})
|
|
|
|
continue
|
|
|
|
return d
|