Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/check_snas_replaces.py
Дата изменения: Fri Apr 8 12:46:10 2016
Дата индексирования: Mon Apr 11 04:45:46 2016
Кодировка:
#
#MODULE check_snas_replaces
#
#****************************************************************************
"""Confirms that the current schedule incorporates the most recent replaces
and/or deletes

TITLE: check_snas_replaces.py
DEVELOPER: Alan Patterson, 11-Oct-2012

PURPOSE: The tool checks the most recent schedule against previous
replaces and deletes.

USAGE: do check_snas_replaces [-select_sched=]

where: is the MS run name.
is the number of the TDRS schedule
(see the last 3 chars of the file name)
[default is the last one i.e. -1]

RETURNS: Nothing

MOD HISTORY:
o Conversion from check_ups_replaces.py app 10/11/12
o Code cleanup; adapted for checklist use drc 2/11/15
o exit cleanly when no sars are found drc 3/09/15

"""
#****************************************************************************
import sys
import spss_sys_util
import spst_getopt
import os
import sms_util
import time_util
import tdrs_util

PASSOPS = spss_sys_util.get_environ_variable("PASSOPS")[0]

__author__ = "Alan Patterson"
__version__ = "15.03.09"


def run(*args):
"""The tool checks the most recent TDRS schedule processed by SCHEDMAN
against previous replace and delete submissions.

Usage: do check_snas_replaces

where: is the MS run name.
"""
if len(args) == 0 or (len(args) == 1 and args[0] is None):
print run.__doc__
return not spss_sys_util.SUCCESS

allowed_options = ['verbose', 'outfile=']
options, parms = spst_getopt.spst_getopt(args, allowed_options)
if len(parms) == 0:
print 'The MS run name must be provided'
return not spss_sys_util.SUCCESS
msrunname = parms[0].lower()
if '-verbose' in options:
verbose = True
else:
verbose = False

if '-outfile' in options:
outfileId = open(options['-outfile'], 'w')
else:
outfileId = sys.stdout

sms = sms_util.sms(msrunname, pass_name=True)
# Get list of replace/delete SARs sent to NCC
smsstart = sms.get_begin_time()
smsend = sms.get_end_time()
dur = smsend - smsstart
mid = smsstart + 0.5*dur - 7*86400
schedweek = str(mid.get_weekid())
weekwin = time_util.window(smsstart, smsend)
if verbose:
outfileId.write('\nMS run name: %s\n' % msrunname)
outfileId.write('TDRS schedule name week: %s\n' % schedweek)
#passops = spss_sys_util.get_environ_variable("PASSOPS")[0]

# get current schedule lis files
#glob_sch = os.path.join(passops,'sched','sch*%s*.lis' % schedweek)
#schfiles = spss_sys_util.glob(glob_sch)
schlist = get_sch_list('sch*.lis')
schfiles = []
for sch in schlist:
lis_1 = tdrs_util.tdrslis_file(sch[1])
h = lis_1.get_header_info()
lis_1.close()
liswin = time_util.window(h['start'], h['end'])
if liswin.overlaps(weekwin):
schfiles.append(sch)
schfiles.sort()
tdrslist = []
if verbose:
outfileId.write('\nOverlapping TDRS lis files, ordered by time\n')
outfileId.write('Created File\n')
for tdrs_lis_file_create_time, tdrs_lis_path in schfiles:
tdrslist.append(spss_sys_util.basename(tdrs_lis_path))
if verbose:
outfileId.write(str(tdrs_lis_file_create_time) + " " + tdrs_lis_path + "\n")
#
sarlist = get_sarlist(msrunname)
if not sarlist:
outfileId.write("\n\nNo SARs found! Exiting...\n")
return -1
if verbose:
outfileId.write('\n\nList of SAR submissions to NCC\n')
for sardir, sar_time, SARfiles in sarlist:
sardir_basename = spss_sys_util.basename(sardir)
sarfiles_str = ''
for sf in SARfiles:
sarfiles_str += " " + spss_sys_util.basename(sf)
outfileId.write('%6s %s %s\n' % (sardir_basename, sar_time, sarfiles_str))
outfileId.write('\nUsing SAR submission at ' + str(sarlist[-1][1]) + "\n")
#sardir = os.path.join(PASSOPS, 'sars', msrunname, sarlist[-1][0])

testlis = None
for sch in schfiles:
if sch[0] > sarlist[-1][1]:
testlis = sch[1]
schtime = sch[0]
break
if testlis is None:
outfileId.write('Error finding TDRS lis file\n')
if schfiles:
#print schfiles
outfileId.write('First: %s\n' % schfiles[0][1])
outfileId.write('Last : %s\n' % schfiles[-1][1])
else:
outfileId.write('No TDRS lis file could be found!\n')
outfileId.write('SSR time: %s\n' % sarlist[-1][1])
outfileId.write('\nThere seems to be no TDRS lis file more recent than\n')
outfileId.write('the SAR submission\n')
return -1

ncclist = get_ncclist(msrunname)
if verbose:
outfileId.write('\n\nList of RSV files and NCC schedules used\n')
for sd in ncclist:
bn = spss_sys_util.basename(sd[0])
rsvfil = spss_sys_util.basename(sd[2])
nccfil = spss_sys_util.basename(sd[3])
outfileId.write('%6s %s %s %s\n' % (bn, sd[1], rsvfil, nccfil))

#
outfileId.write('\nExamining TDRS schedule %s\n' % testlis)
sch_evs_1 = tdrs_util.tdrslis_file(testlis)
sch_events = sch_evs_1.get_tdrs_events()
sch_reqids = sch_events.keys()
if verbose:
outfileId.write('Schedule has %i events\n' % len(sch_reqids))
#print sch_reqids
preceding_sars = None
for sarl in sarlist:
if sarl[1] < schtime:
preceding_sars = sarl
if preceding_sars is None:
outfileId.write('\nThere are no NCC submissions for this SMS that\n')
outfileId.write(' precede the specified TDRS schedule\n')
outfileId.write('Exiting...\n')
return -1

if verbose:
bn = spss_sys_util.basename(preceding_sars[0])
outfileId.write('Preceding SSR submission is %s\n' % bn)
sarfiles = ''
for sf in preceding_sars[2]:
sarfiles = sarfiles + " " + spss_sys_util.basename(sf)
outfileId.write('Files %s\n' % sarfiles)

sdr_reqids = []
ssr_reqids = []
for sf in preceding_sars[2]:
outfileId.write('**%s\n' % sf)
# look in both the .ssr and .sdr files for Replace events (PASS 33.84)
if os.path.splitext(sf)[1] in ['.ssr', '.sdr']:
ssr_evs_1 = tdrs_util.ssr_file(sf)
ssr_evs = ssr_evs_1.get_tdrs_events()
for k in ssr_evs.keys():
try:
ssr_reqids.append((ssr_evs[k].get_event_id(), k))
except:
outfileId.write('Problem with event id\n')
outfileId.write('%s %s\n' % (k, ssr_evs[k]))
outfileId.write('Number of replace requests: %i\n' % len(ssr_reqids))
ssr_evs_1.close()
if os.path.splitext(sf)[1] == '.sdr':
for line in open(sf).readlines():
# ony interpret the lines that are coded as Deletes
if line[16:18] == '11':
sdr_reqids.append(int(line.split()[-1]))

outfileId.write('Starting analysis\n')
sdrbad = []
#print sdr_reqids
for req in sdr_reqids:
if req in sch_reqids:
sdrbad.append(req)
if verbose:
outfileId.write('Examining delete request id: %s\n' % req)
outfileId.write('Number of delete request ids found in TDRS schedule: %i\n' % len(sdrbad))
if len(sdrbad) > 0:
outfileId.write('\nThe events whose Request IDs follow here must be\n')
outfileId.write(' Deleted manually:\n')
for sdrb in sdrbad:
outfileId.write(' %s\n' % sdrb)
outfileId.write('\n')

ssrbad = []
#print ssr_reqids
for req in ssr_reqids:
if req[0] in sch_reqids:
ssrbad.append(req)
if verbose:
outfileId.write('Examining replace request id: %s\n' % req)
outfileId.write('Number of replace request ids found in TDRS schedule %i\n' % len(ssrbad))
if len(ssrbad) > 0:
outfileId.write('\nThe events whose Request IDs follow here must be\n')
outfileId.write(' Modified manually\n')
for ssrb in ssrbad:
outfileId.write(' %s %s\n' % (ssrb[0], ssr_evs[ssrb[1]]))
for srv in ssr_evs[ssrb[1]].get_services():
outfileId.write(' %s\n' % srv)

if len(sdrbad) == 0 and len(ssrbad) == 0:
outfileId.write('\n\nAll events are OK - there are no incomplete Deletes\n')
outfileId.write(' or Replaces\n\n')

return len(sdrbad) + len(ssrbad)


def get_sch_list(schfile):
"""Returns a list of TDRS schedules with names similar to the given one
Each entry is (file creation time, filename)
"""
schlist = []
base = os.path.splitext(schfile)[0]
globsch = os.path.join(PASSOPS, 'sched', '%s*.lis' % base[:-3])
schfiles = spss_sys_util.glob(globsch)
for sch in schfiles:
sch_tm_sec = os.path.getctime(sch)-time_util.SYS_TO_SOGSSEC
sch_tm = time_util.spss_time(sch_tm_sec)
schlist.append((sch_tm, sch))
schlist.sort()
return schlist


def get_sarlist(msrunname):
"""For a given msrunname, identifies the SAR submission files to the NCC
Returns a time ordered list of
(SAR subdirectory, timestamp, list of delete/replace request files)

The list of delete/replace request files can contain *.ssr and *.sdr files. There will
normally be just the *.ssr file for all subdirectories except one which
will contain the *.sdr file in addition
"""
globcheck = os.path.join(PASSOPS, 'sars', msrunname, '*', '*.s*r')
globdir = os.path.join(PASSOPS, 'sars', msrunname, '*')
sardirs = spss_sys_util.glob(globdir)
tmplist = []
for sardir in sardirs:
sar_tm_sec = os.path.getctime(sardir)-time_util.SYS_TO_SOGSSEC
sar_tm = time_util.spss_time(sar_tm_sec)
globcheck = os.path.join(sardir, '*.s*r')
sarfiles = spss_sys_util.glob(globcheck)
tmplist.append((sar_tm, (sardir, sar_tm, sarfiles)))
sarlist = []
tmplist.sort()
for tm in tmplist:
sarlist.append(tm[1])
return sarlist


def get_ncclist(msrunname):
"""For a given msrunname, identifies the SAR submission files to the NCC
Returns a time ordered list of
(SAR subdirectory, timestamp, RSV file, NCC schedule)

There will be just the *.rsv file for all subdirectories and this will
identify the NCC schedule used
"""
#globcheck = os.path.join(PASSOPS,'sars',msrunname,'*','*.rsv')
globdir = os.path.join(PASSOPS, 'sars', msrunname, '*')
sardirs = spss_sys_util.glob(globdir)
tmplist = []
for sardir in sardirs:
sar_tm_sec = os.path.getctime(sardir)-time_util.SYS_TO_SOGSSEC
sar_tm = time_util.spss_time(sar_tm_sec)
globcheck = os.path.join(sardir, '*.rsv')
sarfiles = spss_sys_util.glob(globcheck)
nccsched = get_nccsched(sarfiles[0])
tmplist.append((sar_tm, (sardir, sar_tm, sarfiles[0], nccsched)))
ncclist = []
tmplist.sort()
for tm in tmplist:
ncclist.append(tm[1])
return ncclist


def get_nccsched(rsvfile):
"""Find the NCC schedule file in the RSV file header
"""
rsv_1 = tdrs_util.rsv_file(rsvfile)
hdr = rsv_1.get_header_info()
nccsched = hdr['ncc_file'][:-3] + 'lis'
rsv_1.close()
return nccsched


def get_tdrs_lis_name(msrunname):
"""Given the msrunname returns a list of the TDRS schedules created
for the PASS runs
Returns a list of
(config file date string [yyyydddhhmm], TDRS schedule base name)
"""
schedlist = []
configtemplate = os.path.join(PASSOPS, 'msin', '%s_config*.csh' % msrunname)
configs = spss_sys_util.glob(configtemplate)
for cf in configs:
stat, tdrssch = get_tdrssch(cf)
if stat == spss_sys_util.SUCCESS:
#dirname = spss_sys_util.dirname(tdrssch)
basename = spss_sys_util.basename(tdrssch)
cfdate = os.path.splitext(cf)[0][-11:]
if basename[:3] == 'sch':
schedlist.append((cfdate, basename))
return schedlist


def get_tdrssch(config):
"""Given the config file
Returns status, TDRS schedule file name

If a file name is found then SUCCESS and the file name are returned
Otherwise NOT SUCCESS and an empty string are returned
"""
stat = not spss_sys_util.SUCCESS
#tdsch = ''
for line in open(config).readlines():
if line.find('CFG_TDRSSCHD') != -1:
z = line.split()
td_sch = z[-1]
stat = spss_sys_util.SUCCESS
return stat, td_sch


# to run on commandline
if __name__ == '__main__':
if len(sys.argv) > 1:
run(*tuple(sys.argv[1:]))
else:
run()