| 1 |
#!/bin/sh |
| 2 |
# -*- python -*- |
| 3 |
|
| 4 |
################################################################################ |
| 5 |
# This file is python 2/3 bilingual. |
| 6 |
# The line """:" starts a comment in python and is a no-op in shell. |
| 7 |
""":" |
| 8 |
# Shell code to find and run a suitable python interpreter. |
| 9 |
for cmd in python3 python python2; do |
| 10 |
command -v > /dev/null $cmd && exec $cmd $0 "$@" |
| 11 |
done |
| 12 |
|
| 13 |
echo "Error: Could not find a valid python interpreter --> exiting!" >&2 |
| 14 |
exit 2 |
| 15 |
":""" # this line ends the python comment and is a no-op in shell. |
| 16 |
################################################################################ |
| 17 |
|
| 18 |
from __future__ import print_function |
| 19 |
import os, sys, re, time, datetime, argparse |
| 20 |
from LMODdb import LMODdb |
| 21 |
from BeautifulTbl import BeautifulTbl |
| 22 |
|
| 23 |
class CmdLineOptions(object): |
| 24 |
""" Command line Options class """ |
| 25 |
|
| 26 |
def __init__(self): |
| 27 |
""" Empty Ctor """ |
| 28 |
pass |
| 29 |
|
| 30 |
def execute(self): |
| 31 |
""" Specify command line arguments and parse the command line""" |
| 32 |
parser = argparse.ArgumentParser() |
| 33 |
parser.add_argument("--dbname", dest='dbname', action="store", default = "lmodV2", help="lmod db name") |
| 34 |
parser.add_argument("--syshost", dest='syshost', action="store", default = "%", help="system host name") |
| 35 |
parser.add_argument("--start", dest='startDate', action="store", default = "unknown", help="start date") |
| 36 |
parser.add_argument("--end", dest='endDate', action="store", default = "unknown", help="end date") |
| 37 |
parser.add_argument("--allmodulesFn", dest='allmodulesFn', action="store", default = None, help="File containing all modules") |
| 38 |
parser.add_argument("--sqlPattern", dest='sqlPattern', action="store", default = "/opt/%", help="sql pattern for matching") |
| 39 |
parser.add_argument("cmdA", nargs="+", help="commands: counts, usernames, modules_used_by") |
| 40 |
|
| 41 |
args = parser.parse_args() |
| 42 |
return args |
| 43 |
|
| 44 |
def dbConfigFn(dbname): |
| 45 |
""" |
| 46 |
Build config file name from dbname. |
| 47 |
@param dbname: db name |
| 48 |
""" |
| 49 |
return dbname + "_db.conf" |
| 50 |
|
| 51 |
def main(): |
| 52 |
start = time.time() |
| 53 |
args = CmdLineOptions().execute() |
| 54 |
configFn = dbConfigFn(args.dbname) |
| 55 |
lmod = LMODdb(configFn) |
| 56 |
cmdFnd = False |
| 57 |
|
| 58 |
if (args.cmdA[0] == "counts"): |
| 59 |
cmdFnd = True |
| 60 |
resultA = lmod.counts(args.sqlPattern, args.syshost, args.startDate, args.endDate, args.allmodulesFn) |
| 61 |
num = len(resultA) - 2 |
| 62 |
|
| 63 |
bt = BeautifulTbl(tbl=resultA, gap = 4, justify = "llr") |
| 64 |
|
| 65 |
|
| 66 |
if (args.cmdA[0] == "usernames"): |
| 67 |
cmdFnd = True |
| 68 |
resultA = lmod.usernames(args.sqlPattern, args.syshost, args.startDate, args.endDate) |
| 69 |
num = len(resultA) - 2 |
| 70 |
|
| 71 |
bt = BeautifulTbl(tbl=resultA, gap = 4, justify = "lll") |
| 72 |
|
| 73 |
if (args.cmdA[0] == "modules_used_by"): |
| 74 |
cmdFnd = True |
| 75 |
resultA = lmod.modules_used_by(args.sqlPattern, args.syshost, args.startDate, args.endDate) |
| 76 |
num = len(resultA) - 2 |
| 77 |
|
| 78 |
bt = BeautifulTbl(tbl=resultA, gap = 4, justify = "lll") |
| 79 |
|
| 80 |
if (not cmdFnd): |
| 81 |
print("Missing command: please specify a command: counts, usernames, modules_used_by") |
| 82 |
sys.exit(1) |
| 83 |
|
| 84 |
print("\n") |
| 85 |
print(bt.build_tbl()) |
| 86 |
print("\n\nNumber of entries: ",num) |
| 87 |
end = time.time() |
| 88 |
runTime = end - start |
| 89 |
print ("\nTime: ",datetime.timedelta(seconds=runTime)) |
| 90 |
|
| 91 |
if ( __name__ == '__main__'): main() |