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

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

**PURPOSE** --
Creates a local copy of the TUT file available on the web.

**DEVELOPER** --
Alan Patterson

**MODIFICATION HISTORY** --
Initial implementation app 05/08/03
Modified to spoof user-agent dc 08/07/03
rename dc 10/13/03
use urillib3 or urllib2 based on
the python version dc 11/04/03
Update default output file name app 01/19/04
remove urllib3 dc 9/21/04
new location for tut file app 03/03/05
add verbose, update for OBS app 12/05/06
make services case insensitive app 05/23/07
option to access published TUT app 09/05/07
add TDS, 171 and 275 to default list app 06/27/08
ensure ssh usage app 05/07/09
update for SNAS website (SWSI going away) app 10/14/10
update for use outside of SOGS drc 03/24/11
update to get TDRS from tdrsuse file via ephem_util drc 11/25/14
replace yoda and palpatine with tugger and dunwell drc 1/7/16
"""
#***********************************************************************
__author__ = "Alan Patterson"
__version__ = "16.01.07"

import spss_sys_util
import sys
import time_util
import urllib2
import spst_getopt
import string
import socket
import os
from pass_environ_setup import PASS_SYSTEM
from ephem_util import TDRS_LIST, NOMINAL_TDRS_COMBO

NODELIST = ['holst.sogs.stsci.edu']
OBSNODES = spss_sys_util.OBS_NODES
TESTNODES = ['tugger.stsci.edu', 'dunwell.stsci.edu']

if PASS_SYSTEM == 'ops':
if spss_sys_util.on_obs():
NODES = OBSNODES
else:
NODES = NODELIST
else:
NODES = TESTNODES


def run(*args):
"""Get the TUT file from its website

do get_web_tut [-tdrs=] [-services=]
[-start=] [-end=]
[-outfile=] [-get] [-verbose]
[-published] [-machine=]

where -tdrs=tde,tdw,.. indicates which TDRS satellites
to limit the request to.
(TDE,TDW,171,275, etc.)
-services=mar,sa indicates which services to limit
the request to.
(MAR,MAF,SA,SMAR,SMAF)
-start= indicates the start of the time
window for which to report events
-end= indicates the end of the time
window for which to report events
-outfile= specifies the output file
name. It defaults to tut_yyyy_mmdd_HHMM.tmp
-get request data from the web rather than
printing documentation (which would be
the default action with no args)
-verbose provides informational messages
during execution
-published retrieve published (full) TUT file
Options start, end, tdrs and services
will be ignored
-machine= Indicate machine to execute the web
request from. Default is current node,
or holst if the current node is not in
the acceptable NODELIST
"""
#if len(args) == 0 or (len(args) == 1 and args[0] is None):
# print run.__doc__
# return not spss_sys_util.SUCCESS
low_args = []
for a in args:
b = string.split(a, '=')[0]
low_args.append(string.replace(a, b, string.lower(b)))
allowed_options = ['tdrs=',
'services=',
'start=',
'end=',
'outfile=',
'get',
'verbose',
'published',
'machine=']
options, parms = spst_getopt.spst_getopt(low_args, allowed_options)

# machine check
myname = socket.getfqdn(socket.gethostname())
if '-machine' in options:
try_node = options['-machine']
else:
try_node = NODES[0]

if myname not in NODES or '-machine' in options:
#print 'Access to TUT authorized elgar, copland, verdi, vivaldi'
#print ' wagner, gershwin, beethoven, holst and potter'
#print 'Please try again from any of those machines'
print 'Executing command on %s' % try_node
argdata = ''
for a in parms:
argdata = argdata + a + ' '
for k in options.keys():
a = options[k]
if k != '-machine':
argdata = argdata + k
if a == '':
argdata = argdata + ' '
else:
argdata = argdata + '=' + a + ' '
# setup for remote command execution
comm = 'ssh %s "cd %s ; do get_web_tut %s"' % (try_node,
os.getcwd(),
argdata)
#print comm
stat, res = spss_sys_util.command(comm)
print res
return spss_sys_util.SUCCESS

# check other arguements
if '-outfile' not in options:
tutfilename = "tut_" + time_util.spss_time().strftime("%Y_%m%d_%H%M") + ".tmp"
else:
tutfilename = options['-outfile']
print 'Output file %s' % tutfilename

if '-published' in options:
pubfile = True
else:
pubfile = False
if '-start' in options:
start = time_util.spss_time(options['-start'])
if pubfile:
print 'Start time ignored'
else:
start = time_util.spss_time()
start_string = start.strftime("&Start_Time=%Y%%2F%j%%2F%H%%3A%M")
if '-end' in options:
end = time_util.spss_time(options['-end'])
if pubfile:
print 'End time ignored'
else:
end = time_util.spss_time() + 2*time_util.SECONDS_IN_A_WEEK
end = end.get_weekid().guess_starttime()
end_string = end.strftime("&Stop_Time=%Y%%2F%j%%2F%H%%3A%M")
min_dur = '&Min_Duration=00%3A01'
allowed_services = ['mar', 'maf', 'sa', 'smar', 'smaf']
if '-services' in options:
servlist = []
for srv in string.split(options['-services'], ','):
servlist.append(srv.lower())
#servlist = string.split(options['-services'], ',')
servstring = ''
servdata = ''
for s in allowed_services:
if s in servlist:
servdata = servdata + '%s ' % s.upper()
servstring = servstring + '&RES%i=%s' % (allowed_services.index(s),
s.upper())
if pubfile:
print 'Service selection ignored'
else:
servstring = '&RES0=MAR&RES1=MAF&RES2=SA&RES3=SMAR&RES4=SMAF'
servdata = 'MAR MAF SA SMAR SMAF'
#allowed_tdrs = ['171','275','tde','tds','tdw']
allowed_tdrs = TDRS_LIST
if '-tdrs' in options:
tdrslist = string.split(options['-tdrs'], ',')
tdrslist = [t.upper() for t in tdrslist]
tdrsstring = ''
tdrsdata = ''
for t in tdrslist:
if t in allowed_tdrs:
tdrsdata += '%s ' % t
tdrsstring = tdrsstring + '&TDR%i=%s' % (list(allowed_tdrs).index(t),
t.upper())
else:
print "WARNING: Unknown TDRS: %s" % t
if pubfile:
print 'TDRS selection ignored'
else:
# Full range &TDR0=171&TDR1=275&TDR2=TDE&TDR3=TDS&TDR4=TDW
#tdrsstring = '&TDR0=TDE&TDR1=TDW'
tdrsstring = '&TDR0=%s&TDR1=%s&TDR2=%s&TDR3=%s' % NOMINAL_TDRS_COMBO
#tdrsdata = 'TDE TDW 171 TDS 275'
tdrsdata = ' '.join(NOMINAL_TDRS_COMBO)
if pubfile:
print 'Output includes data for all TDRS satellites'
tdrsdata = 'All TDRS'

# setup access to file on web
tut_tempfile_1 = 'https://snas-www.nascom.nasa.gov/tut/ops/temp/1'
tut_publish = 'https://snas-www.nascom.nasa.gov/tut/ops/data/newtut.dat'

#myname = socket.getfqdn(socket.gethostname())
if spss_sys_util.on_obs():
#When on OBS use modify hostname to get correct translation
# through the vugate firewall
##myname = 'vugate.hst.nasa.gov'
myname = string.replace(myname, '.ccs', '')
myaddr = socket.gethostbyname(myname)
# name of temporary file created by first call to server
tut_tempfile_2 = '%st6.txt' % myaddr
create_file_1 = 'https://snas-www.nascom.nasa.gov/tut/ops/cgi-bin/tutrest.cgi?TutType=1'
tutfile = tut_tempfile_1 + tut_tempfile_2
tutrequ = create_file_1 + start_string + end_string + min_dur + servstring + tdrsstring

if '-verbose' in options:
print
print 'Accessing web from machine : %s' % myname
print 'Using IP address : %s' % myaddr
print 'Start time : %s' % start
print 'End time : %s' % end
print 'Services : %s' % servdata
print 'TDRS satellites : %s' % tdrsdata
if pubfile:
print 'File selection : Full Published TUT'
else:
print 'File selection : Web Page Selected TUT'

if not pubfile:
#create new TUT file and web page with request
request = urllib2.Request(tutrequ, None)
response = urllib2.urlopen(request)
#access created TUT file
request2 = urllib2.Request(tutfile, None)
else:
request2 = urllib2.Request(tut_publish, None)
response2 = urllib2.urlopen(request2)
# read lines of web file
# and create local copy of web file
open(tutfilename, 'w').write(response2.read())
if not pubfile:
response.close()
return spss_sys_util.SUCCESS

if __name__ == "__main__":
if len(sys.argv) <= 1:
run()
else:
run(*tuple(sys.argv[1:]))