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

Поисковые слова: comet tail
#
#MODULE ftp_daemon
#
#***********************************************************************
"""

**PURPOSE** --
A general purpose tool for setting up or restarting an FTP batch job.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implementation 1/22/01
Added cluster PC. drc 12/6/01
Modified to work on Unix as well as VMS. drc 12/6/01
Add option to send files as ASCII. drc 12/7/01
Change SSSP to SSSP1. drc 5/28/03
Get CCS string names from ccs_util. drc 7/24/03
update for public key authentication. drc 6/20/13
updated for Python 3 compatibility. drc 10/9/14
add an scp option. drc 12/16/14
"""
#***********************************************************************
import spss_sys_util
import spst_getopt
import transfer_util
import batch_util
import getpass
import os
try:
from ccs_util import VALID_CCS_STRINGS
except:
VALID_CCS_STRINGS = []

__version__ = "14.12.16" # yy.mm.dd

CLUSTERS = [
'PC',
'OPUS',
'FDF',
'SPUNIX',
'SSSP1',
'STOSC']

for s in VALID_CCS_STRINGS:
CLUSTERS.append("CCS"+s)

# For Python 2 and 3 compatiblity
try:
input = raw_input
except NameError:
pass


class ftp_daemon(batch_util.batch):
def setup(self):
batch_util.batch.setup(self)
self.command = "ftp_daemon -RUN=%s" % self.jobname
self.cluster = ''
cluster = input("Enter the remote cluster name or remote host name: ").upper()
if cluster in CLUSTERS:
self.cluster = cluster
else:
self.rhost = cluster
self.user = input("Enter username: ")
self.word = getpass.getpass("Enter password: ")

self.ldir = batch_util.check_directory(
"Enter the local transfer directory: ")
self.rdir = input("Enter the remote transfer directory: ")
while 1:
print ("Enter the direction of transfer, 'in' for transfers onto")
self.dot = input(
"the local system, 'out' for transfers to the remote system: ").lower().strip()
if self.dot not in ['in', 'out']:
print ("""Direction of transfer must be 'in' or 'out'.
Try again.""")
continue
else:
break
while 1:
print ("Do you want to delete the files from the original directory")
delete = input("when the transfer completes? (y/n): ").strip().lower()
if delete and delete[0] == 'y':
self.delete = 1
break
elif delete and delete.strip().lower()[0] == 'n':
self.delete = 0
break
else:
print ("Please enter Y or N. Try again.")
continue
while 1:
trans_type = input(
"Do you want to send the files with an ascii or binary (default) transfer? (a/b): ").strip().lower()
if not trans_type:
self.trans_type = 'b'
break
elif trans_type.strip().lower()[0] in ['a', 'b']:
self.trans_type = trans_type.strip().lower()[0]
break
else:
print ("Please enter a or b. Try again.")
continue
while 1:
trans_style = input(
"Do you want to send the files with SCP or SFTP (default)? (scp/sftp): ").strip().lower()
if not trans_style:
self.trans_style = 'sftp'
break
elif trans_style.strip().lower() in ['scp', 'sftp']:
self.trans_style = trans_style.strip().lower()
break
else:
print ("Please enter scp or sftp. Try again.")
continue

print ("Enter a glob-type filter for the files to transfer or to")
self.filter = input("transfer all files: ").strip()
if not self.filter:
self.filter = '*'

def transfer(self):
"""Perform the actual file transfers.
"""
if self.cluster:
host = spss_sys_util.get_environ_variable("SPST_" + self.cluster + "_FTP_NODE")[0]
user = spss_sys_util.get_environ_variable("SPST_" + self.cluster + "_FTP_USER")[0]
try:
word = open(spss_sys_util.resolver('PE_DAT', user + '.dat'), 'r').read().strip()
except:
word = ''
else:
host = self.rhost
user = self.user
word = self.word

if 'trans_style' not in self.__dict__ or self.trans_style == 'sftp':
ftp = transfer_util.secure_spstftp(host, user, word)
ftp.set_debuglevel(1)

# Older jobs don't have the trans_type attribute, so
# assume a binary transfer for them.
if 'trans_type' in self.__dict__:
trans_type = self.trans_type
else:
trans_type = 'b'

if self.dot == 'in':
ftp.dirget(batch_util.get_directory(self.rdir),
batch_util.get_directory(self.ldir),
self.filter,
trans_type,
self.delete)
else:
ftp.dirput(batch_util.get_directory(self.rdir),
batch_util.get_directory(self.ldir),
self.filter,
trans_type,
self.delete)
else:
# An scp transfer
if self.dot == 'in':
transfer_util.scp_get(host,
user,
os.path.join(batch_util.get_directory(self.rdir), self.filter),
batch_util.get_directory(self.ldir))
else:
for File in spss_sys_util.glob(os.path.join(batch_util.get_directory(self.ldir), self.filter)):
transfer_util.scp(host,
user,
batch_util.get_directory(self.rdir),
File)
if self.delete:
os.remove(File)


def run(*args):
"""Setup or restart a batch FTP job.

Usage:
do ftp_daemon [-setup|-start=jobname|-list=jobname|-run=jobname]

The -setup option causes the program to prompt the user for all the
information neccessary for setting up and starting a regular FTP
transfer between the local system and a remote system.

When the -start option is used, the user must specify a jobname.
If an job has previously been created with this name, the information
needed to start the job will be gathered from a disk file and the job
will be started. Otherwise, the user will be prompted for information
necessary to start the job as in the -setup case.

The -list option causes the information stored about the specified
job to be printed to the screen.

The -run option causes the transfer specified by jobname to be executed.
"""
if not args:
# Spew out the usage and quit when no parameters
# are provided.
print (run.__doc__)
return spss_sys_util.SUCCESS

if len(args) > 1:
raise ValueError("Only one argument allowed.")

# Parse the arguments...
optlist = ['START=', 'LIST=', 'RUN=', 'SETUP',
'start=', 'list=', 'run=', 'setup']

#options, parms = spst_getopt.spst_getopt(map(string.lower, args), optlist)
options, parms = spst_getopt.spst_getopt(args, optlist)

if '-START' in options:
ftp_daemon(options['-START']).start()

elif '-LIST' in options:
ftp_daemon(options['-LIST'], setup=0).pprint()

elif '-RUN' in options:
ftp_daemon(options['-RUN'], setup=0).transfer()

elif '-SETUP' in options:
ftp_daemon()

elif '-start' in options:
ftp_daemon(options['-start']).start()

elif '-list' in options:
ftp_daemon(options['-list'], setup=0).pprint()

elif '-run' in options:
ftp_daemon(options['-run'], setup=0).transfer()

elif '-setup' in options:
ftp_daemon()

else:
raise ValueError(
"The only allowable options are -setup, -start, -list, and -run")

return spss_sys_util.SUCCESS


# Run from command line
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
run(sys.argv[1])
else:
run()