Skip to content

Commit

Permalink
Merge pull request #168 from NiceGuyIT/script/python-module-manager
Browse files Browse the repository at this point in the history
Add Python Module Manager 'check' if modules are installed
  • Loading branch information
wh1te909 authored Jul 31, 2023
2 parents 7854469 + 6876f8a commit ba4fd79
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
4 changes: 2 additions & 2 deletions community_scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1678,8 +1678,8 @@
"filename": "all_python_module_manager.py",
"submittedBy": "https://github.com/NiceGuyIT",
"name": "Python - Module Manager",
"description": "List/Install/Remove/Update modules in the Python distribution",
"syntax": "help\ninfo [--verbose|--no-verbose]\nlist [--format=<string>]\ninstall <string>...\nuninstall <string>...\nupgrade <string>...",
"description": "List/Check/Install/Remove/Update modules in the Python distribution",
"syntax": "help\ninfo [--verbose|--no-verbose]\nlist [--format=<string>]\ncheck <string>...\ninstall <string>...\nuninstall <string>...\nupgrade <string>...",
"args": [],
"default_timeout": "60",
"shell": "python",
Expand Down
65 changes: 64 additions & 1 deletion scripts/all_python_module_manager.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

__version__ = "0.1.1"
__version__ = "0.1.2"
__license__ = "MIT"
__authors__ = "NiceGuyIT, silversword411"

Expand Down Expand Up @@ -44,6 +44,17 @@
--------
** Check if the Python modules are installed
python python_module_manager.py check module1 module2
This will check if the Python modules are installed. Use this as a "check" in TRMM to check for necessary modules.
For example, to check if the "dataclasses" (core) and "requests" (non-core) modules are installed, run:
python python_module_manager.py check dataclassses requests
--------
** Install one or more Python modules
python python_module_manager.py install numpy pandas
Expand Down Expand Up @@ -108,6 +119,7 @@
set log level
"""
import argparse
import importlib.util
import logging
import subprocess
import sys
Expand Down Expand Up @@ -149,6 +161,44 @@ def pip_install_modules(modules, logger=logging.getLogger(), upgrade=False):
logger.error(err)
exit(1)

def check_modules(modules, logger=logging.getLogger()):
"""
:param modules: list of modules to check if they are installed
:type modules: list
:param logger: Logging instance
:type logger: logging.Logger
:return:
:rtype:
"""
"""
Install or upgrade the specified Python modules using 'pip install'.
:param modules: set of modules to install/upgrade
:param logger: logging instance of the root logger
:return: None
"""
if not modules:
return
required_modules = set(modules)
logger.debug(f"Checking modules: {required_modules}")
ok = True
try:
for module in modules:
# Check if the library exists
if (spec := importlib.util.find_spec(module)) is not None:
print(f'Module {module!r} exists in sys.modules')
else:
print(f'Module {module!r} was not found in sys.modules')
ok = False
except:
logger.error(f'Failed to check if the required modules are installed. required_modules: {required_modules}')
logger.error(traceback.format_exc())
exit(1)

if not ok:
print(f'One or more modules were not found. Exiting with failure code.')
exit(1)

def pip_uninstall_modules(modules, logger=logging.getLogger()):
"""
Uninstall the specified Python modules using 'pip uninstall'.
Expand Down Expand Up @@ -263,6 +313,10 @@ def main():
list_parser.add_argument("--format", default="columns", choices=["columns", "freeze", "json"],
help="Same as python -m pip list --format option")

check_parser = subparsers.add_parser("check", help="Check if the specified modules are installed")
check_parser.add_argument("modules", nargs="+",
help="A (space separated) list of modules to check")

install_parser = subparsers.add_parser("install", help="Install the specified modules")
install_parser.add_argument("modules", nargs="+",
help="A (space separated) list of modules to install")
Expand Down Expand Up @@ -314,6 +368,15 @@ def main():
logger.debug(f"Installed modules (format: {args.format}):")
print(module_list)

elif args.command == "check":
logger.debug(f"Checking modules: {args.modules}")
check_modules(
**{
"modules": args.modules,
"logger": top_logger,
}
)

elif args.command == "install":
logger.debug(f"Installing modules: {args.modules}")
pip_install_modules(
Expand Down

0 comments on commit ba4fd79

Please sign in to comment.