added an uninstall command as well as a check_package command

This commit is contained in:
ronny abraham 2024-01-10 04:57:26 +02:00
parent f281062060
commit 3cd0f1a35d

View file

@ -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