| 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, argparse, json |
| 20 |
from datetime import date |
| 21 |
from dateutil.relativedelta import relativedelta |
| 22 |
from LMODdb import LMODdb |
| 23 |
|
| 24 |
class CmdLineOptions(object): |
| 25 |
""" Command line Options class """ |
| 26 |
|
| 27 |
def __init__(self): |
| 28 |
""" Empty Ctor """ |
| 29 |
pass |
| 30 |
|
| 31 |
def execute(self): |
| 32 |
""" Specify command line arguments and parse the command line""" |
| 33 |
parser = argparse.ArgumentParser() |
| 34 |
parser.add_argument("--syshost", dest='syshost', action="store", default = "%", help="system host name") |
| 35 |
parser.add_argument("--keepMonths", dest='nMonths', action="store", default = None, help="number of months to keep") |
| 36 |
parser.add_argument("--yes", dest='yes', action="store_true", default = None, help="yes to deletions") |
| 37 |
parser.add_argument("--start", dest='startDate', action="store", default = "unknown", help="start date") |
| 38 |
parser.add_argument("--end", dest='endDate', action="store", default = "unknown", help="end date") |
| 39 |
parser.add_argument("-D", dest='debug', action="store_true", help="Debug Flag") |
| 40 |
parser.add_argument("--confFn", dest='confFn', action="store", default="lmodV2_db.conf", help="Name of the database") |
| 41 |
args = parser.parse_args() |
| 42 |
return args |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def months_back(n): |
| 47 |
today = date.today() |
| 48 |
result_date = today - relativedelta(months=n) |
| 49 |
return result_date |
| 50 |
|
| 51 |
def main(): |
| 52 |
|
| 53 |
args = CmdLineOptions().execute() |
| 54 |
lmod = LMODdb(args.confFn) |
| 55 |
|
| 56 |
startDate = args.startDate |
| 57 |
endDate = args.endDate |
| 58 |
|
| 59 |
if (args.nMonths): |
| 60 |
endDate = str(months_back(int(args.nMonths))) |
| 61 |
|
| 62 |
if (endDate == "unknown"): |
| 63 |
print("Must specify end date") |
| 64 |
sys.exit(1) |
| 65 |
|
| 66 |
dateStr = lmod.build_dateTest(startDate, endDate) |
| 67 |
dateStr = dateStr.replace("and","",1) |
| 68 |
print("Deleting records with dates: ", dateStr) |
| 69 |
if (not args.yes): |
| 70 |
ans = input("Is this OK (y/N)?") |
| 71 |
if (ans != 'y'): |
| 72 |
print("Exiting!") |
| 73 |
sys.exit(0) |
| 74 |
|
| 75 |
|
| 76 |
lmod.delete_old_records(args.debug, args.syshost, startDate, endDate) |
| 77 |
|
| 78 |
|
| 79 |
if ( __name__ == '__main__'): main() |