Skip to content

Commit

Permalink
Refactored package installation process to avoid using --user option …
Browse files Browse the repository at this point in the history
…in virtual environment
  • Loading branch information
imbeacon committed Feb 12, 2024
1 parent bda8f12 commit 0021af2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions thingsboard_gateway/tb_utility/tb_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,32 @@ def get_values(expression, body=None, value_type="string", get_tag=False, expres

@staticmethod
def install_package(package, version="upgrade", force_install=False):
from sys import executable
from subprocess import check_call, CalledProcessError
from sys import executable, prefix, base_prefix
from subprocess import check_call
import site
from importlib import reload

result = False
installation_sign = "==" if ">=" not in version else ""

if force_install:
try:
result = check_call(
[executable, '-m', 'pip', 'install', package + '==' + version, '--force-reinstall', '--user'])
except Exception:
if prefix != base_prefix:
if force_install:
result = check_call([executable, '-m', 'pip', 'install', package + '==' + version, '--force-reinstall'])
elif version.lower() == "upgrade":
try:
result = check_call([executable, "-m", "pip", "install", package, "--upgrade", "--user"])
except CalledProcessError:
elif version.lower() == "upgrade":
result = check_call([executable, "-m", "pip", "install", package, "--upgrade"])
else:
if TBUtility.get_package_version(package) is None:
result = check_call([executable, "-m", "pip", "install", package + installation_sign + version])
else:
from pkg_resources import get_distribution
current_package_version = None
try:
current_package_version = get_distribution(package)
except Exception:
pass
if current_package_version is None or current_package_version != version:
installation_sign = "==" if ">=" not in version else ""
try:
if force_install:
result = check_call(
[executable, '-m', 'pip', 'install', package + '==' + version, '--force-reinstall', "--user"])
elif version.lower() == "upgrade":
result = check_call([executable, "-m", "pip", "install", package, "--upgrade", "--user"])
else:
if TBUtility.get_package_version(package) is None:
result = check_call(
[executable, "-m", "pip", "install", package + installation_sign + version, "--user"])
except CalledProcessError:
result = check_call([executable, "-m", "pip", "install", package + installation_sign + version])

# Because `pip` is running in a subprocess the newly installed modules and libraries are
# not immediately available to the current runtime.
Expand All @@ -179,6 +174,16 @@ def install_package(package, version="upgrade", force_install=False):

return result

@staticmethod
def get_package_version(package):
from pkg_resources import get_distribution
current_package_version = None
try:
current_package_version = get_distribution(package)
except Exception:
pass
return current_package_version

@staticmethod
def replace_params_tags(text, data):
if '${' in text:
Expand Down

0 comments on commit 0021af2

Please sign in to comment.