Skip to content

Commit

Permalink
calling pip as external process as it is the recommended way: https:/…
Browse files Browse the repository at this point in the history
  • Loading branch information
fracpete committed Mar 16, 2021
1 parent 99a25f4 commit 272fb7a
Showing 1 changed file with 6 additions and 39 deletions.
45 changes: 6 additions & 39 deletions src/wai/lazypip/_check.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
from importlib import import_module
from inspect import getmembers, isclass
import logging
import pip
import subprocess
import sys
import traceback
from ._venv import is_venv


def backup_root_logger():
"""
Creates a backup of the root logger.
:return: the backup
:rtype: dict
"""
result = dict()
result['level'] = logging.root.level
result['disabled'] = logging.root.disabled
result['handlers'] = logging.root.handlers[:]
return result


def restore_root_logger(backup):
"""
Restores the root logger from the backup.
:param backup: the backup (level/disable/handlers)
:type backup: dict
"""

logging.root.level = backup['level']
logging.root.disabled = backup['disabled']
logging.root.handlers = backup['handlers']


def install_packages(packages, pip_args=None):
"""
Installs the required packages.
Expand All @@ -43,23 +16,17 @@ def install_packages(packages, pip_args=None):
:type pip_args: list
"""

# calling pip.main changes the root logger setup
backup = backup_root_logger()

print("installing", packages)
try:
args = ["install"]
cmd = [sys.executable, "-m", "pip", "install"]
if not pip_args is None:
args.extend(pip_args)
args.extend(packages)
pip.main(args)
cmd.extend(pip_args)
cmd.extend(packages)
subprocess.check_call(cmd)
except:
print("Failed to install: %s" % ",".join(packages))
print(traceback.format_exc())

# restore original settings of root logger
restore_root_logger(backup)


def check_module(module_name, packages=None, pip_args=None):
"""
Expand Down

0 comments on commit 272fb7a

Please # to comment.