From 3cd0f1a35de2684b315bc1952ba1ef520885b914 Mon Sep 17 00:00:00 2001 From: ronny abraham Date: Wed, 10 Jan 2024 04:57:26 +0200 Subject: [PATCH] added an uninstall command as well as a check_package command --- modules/pip.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/modules/pip.py b/modules/pip.py index dfa330b..fba5305 100644 --- a/modules/pip.py +++ b/modules/pip.py @@ -1,5 +1,6 @@ from fabric.api import env, task from fabric.operations import run +from fabric.context_managers import hide, settings import sys import os @@ -145,6 +146,29 @@ def install(package=None): run(pipinstall_cmd) +@task +def uninstall(package_name, silent=False): + """ + Uninstalls a specified package from the virtual environment. + + This method activates the virtual environment and uses pip to uninstall the + specified package. It can optionally suppress command line output. + + :param package_name: Name of the package to uninstall. + :type package_name: str + + :param silent: Suppress command line output if True, defaults to False. + :type silent: bool + :return: None + """ + with virtualenv(): + with settings( + hide('warnings', 'running', 'stdout', 'stderr'), + warn_only=True): + + run(f"pip uninstall -y {package_name}", quiet=silent) + + @task def freeze(param='help'): """ @@ -252,3 +276,31 @@ def copyto(branch): use_sudo=False, backup=True, template_dir=configuration.virtualenv.requirements.local) + + +def check_package_installed(package_name, silent=False): + """ + Checks if a specified package is installed in the virtual environment. + + Activates the virtual environment and uses 'pip list' to check if the + specified package is installed. Can optionally suppress command line + output. + + :param package_name: Name of the package to check. + :type package_name: str + + :param silent: Suppress command line output if True, defaults to False. + :type silent: bool + + :return: True if installed, False otherwise. + :rtype: bool + """ + with virtualenv(): + + with settings( + hide('warnings', 'running', 'stdout', 'stderr'), + warn_only=True): + + output = run(f"pip list | grep {package_name}", quiet=silent) + + return package_name in output