#!/usr/bin/env python3 # use the API instead of xdusage # https://xsede-xdcdb-api.xsede.org/ import os import re import sys import datetime import argparse import urllib.request # import ast import json import math CHARGE_NUMBERS = ['TG-DEB090011', 'sds121', ] def getdatetime(datestring): # API date format: 2020-03-30T09:38:47.000-07:00 # startdt = datetime("%Y-%m-%dT%H:%M:%S.000-07:00 # startdt = datetime.strptime("%Y-%m-%dT%H:%M:%S.000%z", job_mo.group('starttime')) # mydt = datetime.datetime(2020,3,17) # mydt = new datetime.datetime(2020,3,17) # print(mydt.today()) # startdt = datetime.strptime(job_mo.group('starttime'), "%Y-%m-%dT%H:%M:%S.000%z") # convert CUT offset to non-: cutoffset_mo = None # sys.stderr.write('job_mo.group(\'starttime\'): ({})\n'.format(job_mo.group('starttime'))) # sys.stderr.write('job_mo.group(\'starttime\'): ({})\n'.format(datestring)) # cutoffset_mo = cutoffset_reo.match(job_mo.group('starttime')) cutoffset_mo = cutoffset_reo.match(datestring) if cutoffset_mo == None: # sys.stderr.write('failed to match cutoffset pattern for ({})\n'.format(job_mo.group('starttime'))) # sys.stderr.write('failed to match cutoffset pattern for ({})\n'.format(datestring)) # sys.stderr.write(cutoffset_pat + '\n') return (None) else: cutoffset = cutoffset_mo.group('ca') + cutoffset_mo.group('cb') # sys.stderr.write('cutoffset ({})\n'.format(cutoffset)) # startdt = mydt.strptime(cutoffset_mo.group('uncut') + cutoffset, "%Y-%m-%dT%H:%M:%S%z") startdt = datetime.datetime.strptime(cutoffset_mo.group('uncut') + cutoffset, "%Y-%m-%dT%H:%M:%S%z") # sys.stderr.write('startdt ({}) for starttime ({})\n'.format(startdt, job_mo.group('starttime'))) # check for aware datetime object # sys.stderr.write('startdt.tzinfo ({}) startdt.tzinfo.utcoffset(startdt) ({})\n'.format(startdt.tzinfo, startdt.tzinfo.utcoffset(startdt))) return (startdt) # main() parser = argparse.ArgumentParser() parser.add_argument('-j', '--jobs', action='store_true') parser.add_argument('--username') parser.add_argument('--begindate') parser.add_argument('--enddate') argspace = parser.parse_args() # if 'username' in argspace: # username = argspace.username # else: # sys.stderr.write('username not in argspace ({})\n'.format(argspace)) # convert cipres-tgusage --begindate to xdusage -s # cipres-tgusage format: +%m-%d-%Y # xdusage -s format: %Y-%m-%d cipresdf = '%m-%d-%Y%z' # no TZ in cipres data spec, assume UTC if 'begindate' in argspace: cipresbd = argspace.begindate + '+0000' begindt = datetime.datetime.strptime(cipresbd, cipresdf) # sys.stderr.write('cipresbd: ({}) begindt: ({})\n'.format(cipresbd, begindt)) # sys.stderr.write('begindt.tzinfo ({}) begindt.tzinfo.utcoffset(begindt) ({})\n'.format(begindt.tzinfo, begindt.tzinfo.utcoffset(begindt))) xdstart = begindt.strftime('%Y-%m-%d') # sys.stderr.write('xdstart: ({})\n'.format(xdstart)) if 'enddate' in argspace: cipresbd = argspace.enddate + '+0000' enddt = datetime.datetime.strptime(cipresbd, cipresdf) # sys.stderr.write('enddt.tzinfo ({}) enddt.tzinfo.utcoffset(enddt) ({})\n'.format(enddt.tzinfo, enddt.tzinfo.utcoffset(enddt))) xdend = enddt.strftime('%Y-%m-%d') # no local charge from xdusage? set to huge: # localcharge = None # project_pat = 'Project: (?P[^/]+)/(?P\S+)\s*' # project_reo = re.compile(project_pat) # job_pat = r"\s*job id=(?P\S+) jobname=(?P\S+) resource=(?P