From 7f04adcb7b30607e59b2770f51fb252183af56ae Mon Sep 17 00:00:00 2001 From: Porter Libby <pelibby16@earlham.edu> Date: Fri, 25 Oct 2024 16:33:10 -0400 Subject: [PATCH] Update 2 files - /pylist_all.sh - /pylist.py --- pylist.py | 20 ++++++++++++++++++++ pylist_all.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 pylist.py create mode 100644 pylist_all.sh diff --git a/pylist.py b/pylist.py new file mode 100644 index 0000000..2df0b0e --- /dev/null +++ b/pylist.py @@ -0,0 +1,20 @@ +import pkg_resources, socket, sys, platform, json +""" +Create an object containing all packages from the host python environment +Prints object to stout, for use in Bash script. +""" +def get_packages(): + pyversion = { + 'package_list': [], + 'python_version': platform.python_version(), + 'python_location': sys.executable, + 'host_machine': socket.gethostname() + } + for m in pkg_resources.working_set: + pyversion['package_list'].append({ + 'name': m.project_name, + 'version': m.version + }) + return json.dumps(pyversion) + +print(get_packages()) \ No newline at end of file diff --git a/pylist_all.sh b/pylist_all.sh new file mode 100644 index 0000000..95af5c2 --- /dev/null +++ b/pylist_all.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Hide Python Warnings +export PYTHONWARNINGS=ignore + +# Source bashrc file if it exists +if [ -f /etc/bashrc ]; then + source /etc/bashrc +fi +if [ -f /etc/bash.bashrc ]; then + source /etc/bash.bashrc +fi +if [ -f /etc/profile.d/modules.sh ]; then + source /etc/profile.d/modules.sh +fi + + +# Collect data +DATA="["; +while read -r line ; do + module load $line &> /dev/null; + NEWOBJ=$(python /etc/snmp/scripts/pylist.py); + DATA+=$NEWOBJ; + DATA+=","; + module unload $line &> /dev/null; +done < <(module paths python/) +DATA=${DATA::-1} +DATA+="]"; + +# Output data +echo $DATA \ No newline at end of file -- GitLab