From 9bc3d9b29d68c1df89b513270922a5402698f8cd Mon Sep 17 00:00:00 2001 From: Swann Date: Mon, 13 Jul 2020 15:12:15 +0200 Subject: [PATCH] feat: dependencies version check --- multi_user/__init__.py | 4 ++-- multi_user/environment.py | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/multi_user/__init__.py b/multi_user/__init__.py index 6753fff..f30b0ac 100644 --- a/multi_user/__init__.py +++ b/multi_user/__init__.py @@ -45,8 +45,8 @@ from . import environment, utils # TODO: remove dependency as soon as replication will be installed as a module DEPENDENCIES = { - "replication", - "deepdiff", + ("replication", '0.0.11'), + ("deepdiff", '5.0.1'), } diff --git a/multi_user/environment.py b/multi_user/environment.py index 96dcc9b..3319c9c 100644 --- a/multi_user/environment.py +++ b/multi_user/environment.py @@ -23,6 +23,9 @@ import subprocess import sys from pathlib import Path import socket +import re + +VERSION_EXPR = re.compile('\d+\.\d+\.\d+') THIRD_PARTY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libs") DEFAULT_CACHE_DIR = os.path.join( @@ -47,14 +50,22 @@ def install_pip(): subprocess.run([str(PYTHON_PATH), "-m", "ensurepip"]) -def install_package(name): - logging.debug(f"Using {PYTHON_PATH} for installation") - subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", name]) +def install_package(name, version): + logging.info(f"installing {name} version...") + subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", f"{name}=={version}"]) +def check_package_version(name, required_version): + logging.info(f"Checking {name} version...") + out = subprocess.run(f"{str(PYTHON_PATH)} -m pip show {name}", capture_output=True) -def upgrade_package(name): - logging.debug(f"Using {PYTHON_PATH} for update") - subprocess.run([str(PYTHON_PATH), "-m", "pip", "install", name, "--upgrade"]) + version = VERSION_EXPR.search(out.stdout.decode()) + + if version and version.group() == required_version: + logging.info(f"{name} is up to date") + return True + else: + logging.info(f"{name} need an update") + return False def get_ip(): """ @@ -82,9 +93,9 @@ def setup(dependencies, python_path): if not module_can_be_imported("pip"): install_pip() - for package_name in dependencies: + for package_name, package_version in dependencies: if not module_can_be_imported(package_name): - install_package(package_name) + install_package(package_name, package_version) module_can_be_imported(package_name) - else: - upgrade_package(package_name) + elif not check_package_version(package_name, package_version): + install_package(package_name, package_version)