Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/send_alert.py
Дата изменения: Fri Feb 28 14:46:11 2014
Дата индексирования: Sat Mar 1 15:15:41 2014
Кодировка:
#
#MODULE send_alert
#
#***********************************************************************
"""
**PURPOSE** --
Send alert e-mail to TelAlert.


**DEVELOPER** --
Gary Bower

**MODIFICATION HISTORY** --

o Initial implementation 07/07/11
o Fix variable assignment and add test option. gab 07/12/11
o Fix cc option, and use a cc distribution list. gab 08/23/11
o Fix typo in output. gab 09/28/11
o Fixup handling of the contents of the DISTRIBUTION list. mdr 11/3/11
o modify for running from test account drc 1/8/14
"""
#***********************************************************************
import configure_util, file_util, os, spss_sys_util
import spst_getopt, string, sys, time_util

__version__ = "14.01.08"

# Constants
DISTRIBUTION = 'alertinc@hst.nasa.gov'
SUBJECT = 'TA_response_alert'
if os.environ['LOGNAME'] == 'planinst':
TEST_ACCOUNT = False
PLANINST = 'planinst@stsci.edu'
else:
TEST_ACCOUNT = True
PLANINST = os.environ['LOGNAME'] + '@stsci.edu'

def run(*args):
"""Send alert e-mail msg to TelAlert.

Usage:
do send_alert [-cc= -type=[deliver, intercept, or support] -sms=sms_id
-time= -load=
-generic|urgent -contact_number=phone_number -test]

example: do send_alert -type=deliver -sms=sa178v00_f

This example would send an e-mail to TelAlert informing the FOT
that SMS products are ready to be delivered.

example: do send_alert -type=intercept -sms=sa178v00_f -time= delivery of intercept SMS> -load=

This example would send an e-mail to TelAlert informing the FOT
that the currently flying SMS will be intercepted.

example: do send_alert -type=support -generic|urgent

This example would send an e-mail to TelAlert informing the FOT
of a request for operations support. One of the options generic or
urgent can be specified.

If -cc is included, then its value is used as the cc list.

If -deliver is included, then -sms should be included and specified
(if not, the user is prompted).

If -test is included, the user is prompted to provide the destination
of the message. Otherwise, the defined constant DISTRIBUTION is the
destination of the message.

sms_id must be the name of the SMS to be delivered.



"""

# Parse the arguments
allowed_options = ['cc=', 'type=', 'sms=', 'time=', 'load=', 'generic', 'urgent',
'contact_number=', 'test']
options, parms = spst_getopt.spst_getopt(args, allowed_options)

# Set variables based on arguments except -cc.
if '-type' not in options.keys():
print "Must specify -type.\n"
print run.__doc__
return not spss_sys_util.SUCCESS
else:
type = string.lower(options['-type'])
if type not in ['deliver', 'intercept', 'support']:
raise ValueError ("-type must be deliver, intercept, or support")
if type == 'deliver':
deliver = True
intercept = False
support = False
elif type == 'intercept':
deliver = False
intercept = True
support = False
if '-time' in options.keys():
time = time_util.spss_time(options['-time'])
else:
raise TypeError ("Must specify -time with -type=intercept.")
if '-load' in options.keys():
load = string.upper(options['-load'])
else:
raise TypeError ("Must specify -load with -type=intercept.")
else:
deliver = False
intercept = False
support = True
if not (options.has_key('-generic') or options.has_key('-urgent')):
raise TypeError ("Must specify either -generic or -urgent on command line.")
if '-contact_number' not in options.keys():
raise TypeError ("Must specify -contact_number= on command line.")
else:
phone = options['-contact_number']
if '-sms' in options.keys():
sms = string.lower(options['-sms'])
else:
sms = None

# Test interaction of certain options.
if (deliver or intercept) and not sms:
sms = raw_input("Enter PASS SMS name: ")
elif support and '-generic' in options.keys():
generic = True
urgent = False
elif support and '-urgent' in options.keys():
generic = False
urgent = True

if deliver or intercept:
# Get pickled mscl run.
pickle_in_directory = spss_sys_util.get_environ_variable(
"PASS_INPUT_PICKLES")[0]
print '\nLooking for mscl pickles in %s...' % pickle_in_directory
pickle_lib_list = configure_util.pickle_library_list(pickle_in_directory)
mscl_pickle_list = pickle_lib_list.get_pickle_list().filter(pickle_type="mscl",
reg_exp=sms.lower())
if len(mscl_pickle_list) == 0:
raise IOError('\n\nNo Pickles found in %s that match %s!' % (pickle_in_directory,
sms.lower()))
mscl_pickle_list.sort(configure_util.cmp_pickle_by_create)
mscl_pickle_path = mscl_pickle_list.pick_a_pickle()
mscl_object = configure_util.get_pickled_object(mscl_pickle_path)
if not mscl_object:
raise IOError('\n\nUnable to unpickle %s!' % mscl_pickle_path)
# Find SMS honcho.
honcho_tup = mscl_object.get_span().starttime().get_weekid().get_honcho()
if honcho_tup:
#honcho = honcho_tup[1]
phone = honcho_tup[2]
email = honcho_tup[3]
else:
print "SMS honcho data not found."
print "Please enter your phone and e-mail address (comma separated)."
print "e.g., 410-338-1234, doe@stsci.edu"
honcho_tup = raw_input("").split(',')
#honcho = honcho_tup[0].strip()
phone = honcho_tup[0].strip()
email = honcho_tup[1].strip()

# Open a message file.
output = file_util.tempfile()
f = open(output,'w')

# If deliver is True, then write the deliver message to it.
if deliver:
f.write("[group: FOT-SS_OC] OC SS, please call\n")
f.write("%s to confirm readiness to receive delivery of\n" % phone)
f.write("SMS %s products.\n" % sms)
# If intercept is True, then write the intercept message to it.
elif intercept:
f.write("[group: FOT-SS_OC] NOTICE of pending intercept of SMS %s;\n" % sms)
f.write("Expected SMS product delivery time: %s \n" % time)
f.write("Expected first changed load: %s \n" % load)
f.write("P&S Contact number: %s\n" % phone)
elif support and urgent:
f.write("[group: FOT-SS_OC] URGENT: OC SS, PLEASE CALL\n")
f.write("P&S at %s AS SOON AS POSSIBLE." % phone)
elif support and generic:
f.write("[group: FOT-SS_OC] OC SS, please call\n")
f.write("%s at your earliest convenience.\n" % phone)

# Close the message file.
f.close()

# Edit the file to confirm content of notice.
editor = spss_sys_util.get_environ_variable('EDITOR')
if editor:
do_edit = spss_sys_util.command(string.join(editor) + ' ' + output)
if do_edit[0]:
print "The editor command"
print string.join(editor) + ' ' + output
print "failed."
return not spss_sys_util.SUCCESS
ready_flag = raw_input("Send the notification (y|n)?")
if not string.lower(ready_flag[0]) == 'y':
print "Notification was not sent. It has been"
print "saved in the file %s." % output
return not spss_sys_util.SUCCESS
else:
print "EDITOR environment variable was not found. Abort now ..."
return not spss_sys_util.SUCCESS

# Construct cc_file from a distribution list and convert it to a list.
cc_file = spss_sys_util.resolver('PE_DAT', 'send_alert.dis')
cc_list = open(cc_file,'r').read().strip().split('\n')


# If deliver or intercept are true, then append SMS honcho to the cc list.
# If cc option is specified then append its value to the cc list.
# In any case, append PLANINST to the cc list.
if deliver or intercept:
cc_list.append(email)
if '-cc' in options.keys():
cc_list.append(string.lower(options['-cc']))
cc_list.append(PLANINST)

# E-mail the output file to the distribution.
if '-test' in options.keys() or TEST_ACCOUNT:
distr = raw_input("Destination e-mail address of message?")
spss_sys_util.mail(distr,SUBJECT,output)
else:
spss_sys_util.mail(DISTRIBUTION,SUBJECT,output,cc=cc_list)


# Clean up.
os.remove(output)

if '-test' in options.keys() or TEST_ACCOUNT:
print "Test notification to %s was sent." % distr
else:
print "Notification to %s with cc to %s was sent." % (DISTRIBUTION, cc_list)

return spss_sys_util.SUCCESS

if __name__ == '__main__':
if len(sys.argv) > 1:
apply(run, tuple(sys.argv[1:]))
else:
run()