Skip to content

Commit e4cbc04

Browse files
committed
slurm2sql: Abstract cli db import into import_or_open_db for reuse in other scripts
1 parent dbb3c52 commit e4cbc04

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

slurm2sql.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,29 @@ def process_sacct_filter(args, sacct_filter):
996996
sacct_filter[:0] = ['--endtime=now', f'--state={COMPLETED_STATES}']
997997
return sacct_filter
998998

999+
def import_or_open_db(args, sacct_filter, csv_input=None):
1000+
"""Helper function to either open a DB or generate a new in-mem one from sacct
1001+
1002+
The `args` sholud be an argparse argument option. This function
1003+
will look at its arguments and do what it says. So, if you want
1004+
various features, you need to define these arguments in argparse:
1005+
1006+
db: filename of a database to open
1007+
1008+
"""
1009+
sacct_filter = process_sacct_filter(args, sacct_filter)
1010+
1011+
LOG.debug(f'sacct args: {sacct_filter}')
1012+
if args.db:
1013+
db = sqlite3.connect(args.db)
1014+
if sacct_filter:
1015+
LOG.warn("Warning: reading from database. Any sacct filters are ignored.")
1016+
else:
1017+
db = sqlite3.connect(':memory:')
1018+
errors = slurm2sql(db, sacct_filter=sacct_filter,
1019+
csv_input=getattr(args, 'csv_input', False) or csv_input)
1020+
return db
1021+
9991022

10001023
def update_last_timestamp(db, update_time=None):
10011024
"""Update the last update time in the database, for resuming.
@@ -1088,15 +1111,7 @@ def sacct_cli(argv=sys.argv[1:], csv_input=None):
10881111
if args.output == 'long':
10891112
args.output = SACCT_DEFAULT_FIELDS_LONG
10901113

1091-
sacct_filter = process_sacct_filter(args, sacct_filter)
1092-
1093-
LOG.debug(f'sacct args: {sacct_filter}')
1094-
if args.db:
1095-
db = sqlite3.connect(args.db)
1096-
else:
1097-
db = sqlite3.connect(':memory:')
1098-
errors = slurm2sql(db, sacct_filter=sacct_filter,
1099-
csv_input=args.csv_input or csv_input)
1114+
db = import_or_open_db(args, sacct_filter, csv_input=csv_input)
11001115

11011116
from tabulate import tabulate
11021117

@@ -1152,20 +1167,12 @@ def seff_cli(argv=sys.argv[1:], csv_input=None):
11521167
logging.lastResort.setLevel(logging.WARN)
11531168
LOG.debug(args)
11541169

1155-
sacct_filter = process_sacct_filter(args, sacct_filter)
1156-
11571170
if args.order:
11581171
order_by = f'ORDER BY {args.order}'
11591172
else:
11601173
order_by = ''
11611174

1162-
LOG.debug(f'sacct args: {sacct_filter}')
1163-
if args.db:
1164-
db = sqlite3.connect(args.db)
1165-
else:
1166-
db = sqlite3.connect(':memory:')
1167-
errors = slurm2sql(db, sacct_filter=sacct_filter,
1168-
csv_input=args.csv_input or csv_input)
1175+
db = import_or_open_db(args, sacct_filter, csv_input=csv_input)
11691176

11701177
from tabulate import tabulate
11711178

0 commit comments

Comments
 (0)