2021-11-04 12:38:55 +00:00
|
|
|
import os
|
|
|
|
import re
|
2022-02-12 10:20:09 +00:00
|
|
|
import sys
|
|
|
|
import json
|
2021-11-04 12:38:55 +00:00
|
|
|
|
|
|
|
from .constants import BASE_DIR
|
|
|
|
try:
|
2022-02-09 19:57:39 +00:00
|
|
|
from importlib.metadata import version
|
2021-11-04 12:38:55 +00:00
|
|
|
importlib = True
|
|
|
|
ImportNotFound = BaseException
|
|
|
|
except ImportError:
|
|
|
|
importlib = False
|
2022-02-12 10:20:09 +00:00
|
|
|
version = None
|
2021-11-04 12:38:55 +00:00
|
|
|
|
|
|
|
if not importlib:
|
|
|
|
try:
|
|
|
|
import pkg_resources
|
|
|
|
from pkg_resources import DistributionNotFound as ImportNotFound
|
|
|
|
pkgresources = True
|
|
|
|
except ImportError as e:
|
|
|
|
pkgresources = False
|
|
|
|
|
2022-05-01 10:36:35 +00:00
|
|
|
|
|
|
|
def load_dependencies(optional=False):
|
2021-11-06 16:17:00 +00:00
|
|
|
deps = list()
|
2022-02-12 10:20:09 +00:00
|
|
|
if getattr(sys, 'frozen', False):
|
2022-02-12 10:58:56 +00: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 12:38:55 +00: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 16:17:00 +00: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 10:20:09 +00:00
|
|
|
if getattr(sys, 'frozen', False):
|
2022-05-21 20:52:59 +00:00
|
|
|
dep_version = exe_deps[res.group(1).lower().replace('_', '-')]
|
2021-11-06 16:17:00 +00:00
|
|
|
else:
|
2022-02-12 10:20:09 +00:00
|
|
|
if importlib:
|
|
|
|
dep_version = version(res.group(1))
|
|
|
|
else:
|
|
|
|
dep_version = pkg_resources.get_distribution(res.group(1)).version
|
2022-02-12 10:58:56 +00:00
|
|
|
except (ImportNotFound, KeyError):
|
2021-11-06 16:17:00 +00:00
|
|
|
if optional:
|
|
|
|
continue
|
2021-11-06 17:17:48 +00:00
|
|
|
dep_version = "not installed"
|
2021-11-06 16:17:00 +00: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 12:38:55 +00:00
|
|
|
|
2021-11-06 16:17:00 +00:00
|
|
|
def dependency_check(optional=False):
|
|
|
|
d = list()
|
2024-06-18 18:13:26 +00:00
|
|
|
dep_version_int = None
|
|
|
|
low_check = None
|
2022-05-01 10:36:35 +00:00
|
|
|
deps = load_dependencies(optional)
|
2021-11-06 16:17:00 +00:00
|
|
|
for dep in deps:
|
|
|
|
try:
|
2023-10-29 15:45:09 +00:00
|
|
|
dep_version_int = [int(x) if x.isnumeric() else 0 for x in dep[0].split('.')]
|
2021-11-06 17:17:48 +00:00
|
|
|
low_check = [int(x) for x in dep[3].split('.')]
|
2021-11-06 16:17:00 +00:00
|
|
|
high_check = [int(x) for x in dep[5].split('.')]
|
|
|
|
except AttributeError:
|
2022-05-02 15:42:06 +00:00
|
|
|
high_check = []
|
2021-11-06 17:17:48 +00:00
|
|
|
except ValueError:
|
|
|
|
d.append({'name': dep[1],
|
2022-05-01 10:36:35 +00:00
|
|
|
'target': "available",
|
|
|
|
'found': "Not available"
|
|
|
|
})
|
2021-11-06 17:17:48 +00:00
|
|
|
continue
|
|
|
|
|
2021-11-06 16:17:00 +00:00
|
|
|
if dep[2].strip() == "==":
|
|
|
|
if dep_version_int != low_check:
|
|
|
|
d.append({'name': dep[1],
|
2022-05-01 10:36:35 +00:00
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
2021-11-06 16:17:00 +00:00
|
|
|
continue
|
|
|
|
elif dep[2].strip() == ">=":
|
|
|
|
if dep_version_int < low_check:
|
|
|
|
d.append({'name': dep[1],
|
2022-05-01 10:36:35 +00:00
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
2021-11-06 16:17:00 +00:00
|
|
|
continue
|
|
|
|
elif dep[2].strip() == ">":
|
|
|
|
if dep_version_int <= low_check:
|
|
|
|
d.append({'name': dep[1],
|
2022-05-01 10:36:35 +00:00
|
|
|
'found': dep[0],
|
|
|
|
"target": dep[2] + dep[3]})
|
2021-11-06 16:17:00 +00:00
|
|
|
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
|