Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/schedman.py
Дата изменения: Fri Apr 8 12:46:13 2016
Дата индексирования: Sun Apr 10 20:11:02 2016
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п
#
#MODULE schedman
#
#***********************************************************************
"""

**PURPOSE** --
A module for generating the pass tdrs schedules

**DEVELOPER** --
Greg Wenzel

**MODIFICATION HISTORY** --

o initial implementation GWW 04/17/02
o Modified to accomodate changes to sched_util. drc 4/23/03
o Modified to accomodate changes to configure_util. gww & drc 8/13/03
o Modified for new pickle format. GWW 10/29/03
o readded pick_a_pickle for non batch runs GWW 11/03/03
o compare cfm files by modification time. drc 1/03/05
o add the intercept option. drc 4/12/06
o add running of tdrs_lis_check. drc 5/25/06
o modified for archiving cfm and log files. drc 3/26/12
o updated pickle comment; code cleanup. drc 4/13/15
"""
#***********************************************************************
import sched_util
import spst_getopt
import sys
import spss_sys_util
import os
import configure_util
import pass_util
import shutil
import archive_util

PASS_ARCHIVE_DIR = spss_sys_util.resolver("PASS_ARCHIVE_DIR")
ADD_PASS_ARCHIVE_DIR = os.path.join(PASS_ARCHIVE_DIR, 'add')
PASS_TO_SPUNIX_DIR = spss_sys_util.resolver("PASS_TO_SPUNIX_DIR")

__version__ = "15.04.13"


def run(*args):
"""Tdrs schedule generation tool.

Usage:
do schedman [-option= or ]
[-intercept=]
[-software_version= ]
[-batch]
[-verbose]

-intercept= -option= replace creates a new schedule file with contents of input cfm.
-option= update creates a new schedule file with contents of input cfm
updating the overlapping portion of the old schedule file.

-software_version= defaults to 'ops' , others are 'old','new'
-batch make all file and cms/pickle selections automatically.
-verbose write debuging info to screen
"""
#default input values
option = 'replace'
sw_version = 'ops'
verbose = False
batch = False
intercept_mscl_run = ''

if not spss_sys_util.on_sun():
raise OSError('\n\nPlease run on UNIX!')

#get input options.
allowed_options = ['option=',
'software_version=',
'verbose',
'batch',
'intercept=']
try:
options, parms = spst_getopt.spst_getopt(args, allowed_options)
except Exception, e:
e = '%s\n%s' % (e, run.__doc__)
raise IOError(e)

if '-option' in options.keys():
option = options['-option'].lower()

if '-intercept' in options.keys():
intercept_mscl_run = options['-intercept']

if '-software_version' in options.keys():
sw_version = options['-software_version'].lower()

if '-verbose' in options.keys():
verbose = True

if '-batch' in options.keys():
batch = True

if batch and option != 'replace':
error_txt = "\n\nOnly option = 'replace' can be run in batch mode!"
raise IOError(error_txt)

if intercept_mscl_run and option != 'update':
error_txt = "\n\nswitch -intercept is only valid with -option = 'update'!"
raise IOError(error_txt)

#get input cfm file
cfm_path = get_cfmfile(batch)
if cfm_path:
#set root output directory. Output will be in sched directory under the
#root defined here
root_output_path = spss_sys_util.resolver("PASSOUT")
tdrs_schedule = sched_util.tdrs_schedule(option, root_output_path, cfm_path)
else:
raise IOError(
'\n\nCFM file not found!\nTDRS schedule generation terminated!')

if verbose:
print '\nSelected CFM: %s' % tdrs_schedule.get_cfm_file()
print 'CFM file time span: %s' % tdrs_schedule.cfm_span
print 'CFM selected east tdrs: %s' % tdrs_schedule.tdrs_a
print 'CFM selected west tdrs: %s' % tdrs_schedule.tdrs_b

#define pickle lib.
pickle_in_directory_list = spss_sys_util.get_environ_variable(
"PASS_INPUT_PICKLES")
pickle_directory = spss_sys_util.resolver("PASS_OUTPUT_PICKLES")

new_product = configure_util.configured_product('sch')

#set old schedule file.
if intercept_mscl_run:
old_product = get_pickle_from_mscl(intercept_mscl_run)
else:
pickle_lib_list = configure_util.pickle_library_list(pickle_in_directory_list)
sched_pickle_list = pickle_lib_list.get_pickle_list().filter(pickle_type="sch")
sched_pickle_list.sort(configure_util.cmp_pickle_by_start)
if batch:
sched_pickle_path = sched_pickle_list[-1]
else:
sched_pickle_path = sched_pickle_list.pick_a_pickle()
old_product = configure_util.get_pickled_object(sched_pickle_path)

if old_product:
old_tdrssched_dict = old_product.get_configured_file_dictionary()
for configured_file in old_tdrssched_dict.values():
new_product.add_configured_file(configured_file)

if 'CFG_TDRSSCHD' in old_tdrssched_dict:
tdrs_schedule.set_old_tdrs_file_path(
old_tdrssched_dict['CFG_TDRSSCHD'].get_configured_path())
elif option in ['update', 'summary']:
error_txt = '\n\nOld TDRS schedule not found!'
error_txt = error_txt + '\n%s option can not be performed!' % option
raise IOError(error_txt)
elif option in ['update', 'summary']:
tdrs_schedule.report()
error_txt = '\n\nOld TDRS schedule not found!'
error_txt = error_txt + '\n%s option can not be performed!' % option
raise IOError(error_txt)

if verbose:
tdrs_schedule.report()

if option == 'summary' and os.path.exists(old_tdrssched_dict['CFG_TDRSSCHD'].new_tdrs_list_path):
print '\nSummary file already exists!'
print 'see: ', old_tdrssched_dict['CFG_TDRSSCHD'].new_tdrs_list_path
else:
status = tdrs_schedule.run(sw_version=sw_version,
verbose=verbose)

if verbose:
tdrs_schedule.report()

#check for errors
max_error = pass_util.get_max_error_severity(tdrs_schedule.new_tdrs_log_path)
if not os.path.exists(tdrs_schedule.new_tdrs_file_path) or max_error >= 3:
error_text = '\n\nScript Status: %s' % status
error_text = error_text + '\nFiles were NOT configured!'
error_text += '\nSee log file: %s' % tdrs_schedule.new_tdrs_log_path
error_text += '\nError severity of %s' % max_error
if option == 'summary':
if os.path.exists(tdrs_schedule.new_tdrs_list_path):
print 'Summary Listing File:', tdrs_schedule.new_tdrs_list_path
else:
raise IOError(error_text)
else:
new_product.add_configured_file(tdrs_schedule)
comment = "TDRS spans: %s; GSTDN spans: %s - updated for TDRS schedule file" % (new_product.get_configured_file_dictionary()['CFG_TDRSSCHD'].get_span(),
new_product.get_configured_file_dictionary()['CFG_GSTDNCON'].get_span())
if verbose:
print "New product comment:", comment
new_product.set_comment(comment)

#update configured areas
if not batch:
while True:
yesorno = raw_input('\nConfigure New TDRS Schedule file = (y)es or n(o)? > ')
if yesorno.lower().strip() in ('n', 'no'):
configure = False
break
elif yesorno.lower().strip() in ('', 'y', 'ye', 'yes'):
configure = True
break
else:
print "Please enter yes or no."
continue
else:
configure = True

if configure:
new_product.ancillary_files = tdrs_schedule.ancillary_files
pickle_path = new_product.configure_pickle(pickle_directory, verbose)

#copy sum file to sched area
con_sum_file = os.path.join(tdrs_schedule.get_output_directory(), 'tdrs.sum')
con_lis_file = os.path.join(tdrs_schedule.get_output_directory(), 'tdrs.lis')
new_sum_file = tdrs_schedule.get_log_path()
new_lis_file = tdrs_schedule.get_lis_path()
shutil.copy(new_sum_file, con_sum_file)
shutil.copy(new_lis_file, con_lis_file)
#send to archive
archive_util.archive_pickle(pickle_path, ADD_PASS_ARCHIVE_DIR)
#send to test system on science cluster
archive_util.archive_pickle(pickle_path, PASS_TO_SPUNIX_DIR)
print '\n\nTDRS Schedule File:', tdrs_schedule.new_tdrs_file_path
print '\nConfigured!'
print "\nNew TDRS schedule pickle file:", pickle_path
stat = os.stat(tdrs_schedule.new_tdrs_list_path)
out_mode = stat[0] | 0444 | 0111
os.chmod(tdrs_schedule.new_tdrs_list_path, out_mode)
os.chmod(tdrs_schedule.new_tdrs_file_path, out_mode)
os.chmod(tdrs_schedule.new_tdrs_log_path, out_mode)
os.chmod(con_sum_file, out_mode)
os.chmod(con_lis_file, out_mode)
if not batch:
# User has the option to run tdrs_lis_check on the new tdrs.lis file.
valid_response = False
while not valid_response:
print "\n\nWould you like to check %s" % con_lis_file
print "to ensure that there are no MAFs or SSAFs which extend past the associated MAR service?"
yesorno = raw_input(
'Enter = (y)es or n(o)? > ')
if yesorno.lower().strip() in ('n', 'no'):
valid_response = True
elif yesorno.lower().strip() in ('', 'y', 'ye', 'yes'):
import tdrs_lis_check
tdrs_lis_check.run(con_lis_file)
valid_response = True
else:
print "Please enter yes or no."
else:
print '\n\nTDRS Schedule File:', \
tdrs_schedule.new_tdrs_file_path
print '\nNot configured!'

#clean up cfm files
clean_cfmfiles(keep_num=5)
return


def clean_cfmfiles(keep_num=5):
"""remove old cfm files keeping keep_num
"""
sched_dir = spss_sys_util.resolver("PASSOPS")
cfm_test_path = os.path.join(sched_dir, 'sched/', '*.cfm')
cfm_path_list = spss_sys_util.glob(cfm_test_path)
cfm_path_list.sort(cfm_cmp)
del_cfm_path_list = cfm_path_list[:-keep_num]
for path in del_cfm_path_list:
os.remove(path)
return


def cfm_cmp(file1, file2):
"""Use this function to compare cfm files based on their modification times
"""
if os.path.getmtime(file1) < os.path.getmtime(file2):
return -1
elif os.path.getmtime(file1) == os.path.getmtime(file2):
return 0
else:
return 1


def get_cfmfile(batch=False):
"""Gets the latest CFM file for tdrs schedule generation input.
"""
sched_dir = spss_sys_util.resolver("PASSOPS")
cfm_test_path = os.path.join(sched_dir, 'sched/', '*.cfm')
cfm_path_list = spss_sys_util.glob(cfm_test_path)
cfm_path_list.sort(cfm_cmp)
cfm_file = None
if cfm_path_list:
#if in batch pick the latest one, else prompt for input.
if batch:
cfm_file = cfm_path_list[-1]
else:
print '\n\nList of CFM files:'
for file in cfm_path_list:
print file
while cfm_file not in cfm_path_list:
cfm_file = raw_input('Enter CFM file or =%s >' % cfm_path_list[-1])
if not cfm_file:
cfm_file = cfm_path_list[-1]
return cfm_file


def get_pickle_from_mscl(run_name):
"""gets pickle for update mode. helps select proper
history schedule for intercept run.
"""
mscl_pickle_list = pass_util.find_delivered_pickles(run_name, verbose=True)

if not mscl_pickle_list:
print "ERROR -- No valid pickles found for that name."
print "Exiting..."
sys.exit()

print "\n\nChoose the pickle that corresponds to the run you will be intercepting:"
mscl_pickle_path = mscl_pickle_list.pick_a_pickle()
mscl_object = configure_util.get_pickled_object(mscl_pickle_path)

config = mscl_object.get_config()
print "\n\nUsing the TDRS schedule associated with the previously chosen MSCL pickle:"
print config['sch'].get_pickle_path()
return config['sch']


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