-
Notifications
You must be signed in to change notification settings - Fork 0
/
saltftp.py
275 lines (217 loc) · 9.47 KB
/
saltftp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
################################# LICENSE ##################################
# Copyright (c) 2009, South African Astronomical Observatory (SAAO) #
# All rights reserved. #
# #
############################################################################
#!/usr/bin/env python
"""
SALTFTP tar gzips and transfers PI-collated data to the FTP server
for collection by the PI.
Author Version Date
-----------------------------------------------
Martin Still (SAAO) 0.2 21 Jul 2006
S M Crawford (SAAO) 0.3 21 Jul 2007
UPDATES
------------------------------------------------
21 Jul 2007 --changed from PI to proposal code
--moves things to the beachhead facility rather than the
ftp site
6 Apr 2011-Updated to use the new error handling.
-Created new method for determinging the date
15 Jun 2011-Changed the call to the database to make it more robust
"""
# Ensure python 2.5 compatibility
from __future__ import with_statement
import string
from pyraf import iraf
import tarfile, glob, os, ftplib
import saltsafeio as saltio
import saltsafemysql as saltmysql
from salterror import SaltError, SaltIOError
from saltsafelog import logging
debug=True
# -----------------------------------------------------------
# core routine
def saltftp(propcode, obsdate, datapath, beachdir,
sdbhost,sdbname,sdbuser, password, cleanup=True, splitfiles=False,
clobber=True,logfile='salt.log',verbose=True):
# set up
proposers = []
propids = []
pids = []
with logging(logfile,debug) as log:
# are the arguments defined
if propcode.strip().upper()=='ALL':
pids=getproposalcodes(str(obsdate), sdbhost,sdbname,sdbuser, password)
else:
pids = saltio.argunpack('propcode',propcode)
print pids
if len(pids)==0:
#throw a warning adn exit if not data needs to be filterd
log.warning('No data to distribute\n', with_stdout=verbose)
return
#get the current working directory
curdir=os.getcwd()
#set the obsdate to a string
obsdate=str(obsdate)
# check datapath exists, ends with a "/" and convert to absolute path
if not saltio.checkfornone(datapath):
datapath='/salt/data/%s/%s/' % (obsdate[0:4], obsdate[4:8])
datapath = saltio.abspath(datapath)
# check PI directories exist
for pid in pids:
pidpath = datapath + pid.upper()
if os.path.exists(pidpath):
if splitfiles:
tarsplitfile(obsdate, pid, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber, log, verbose)
else:
tarallfiles(obsdate, pid, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber, log, verbose)
else:
log.warning('No directory exists for %s on %s at:\n%s' % (pid, obsdate, pidpath))
saltio.changedir(curdir)
def tarsplitfile(obsdate, pid, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber, log, verbose):
"""Split the directories so there is one tar file for each directory"""
pidpath = datapath + pid.upper()
for data_dir in ['product', 'raw', 'doc']:
# overwrite tar files
zipfile = '%s.%s_%s.tar.bz2' % (pid.upper(),obsdate, data_dir)
if (os.path.isfile(pidpath + zipfile) and clobber):
saltio.delete(pidpath+zipfile)
elif (os.path.isfile(pidpath+zipfile) and not clobber):
message = 'ERROR: SALTFTP -- file ' + pidpath + zipfile + ' exists. Use clobber=y'
raise SaltError(message)
#tar the files
saltio.changedir(pidpath)
tfile = tarfile.open(zipfile,'w:bz2')
tfile.dereference = True
log.message('\nSALTFTP -- tarring data for %s/%s in %s' % (pid.upper(), data_dir, zipfile))
print glob.glob('%s/*' % data_dir)
for zfile in glob.glob('%s/*' % data_dir):
if 'bz2' not in zfile:
#log.message('SALTFTP -- ' + pid.upper() + '/' + zipfile + ' <-- ' + zfile)
tfile.add(zfile)
tfile.close()
#Copy data to the beachhead location
message=movetobeachhead(pid, zipfile, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber)
log.message(message)
#delete tar files
saltio.delete(datapath+pid.upper()+'/'+zipfile)
def tarallfiles(obsdate, pid, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber, log, verbose):
"""Put all files for a proposal into a single tar file"""
pidpath = datapath + pid.upper()
# overwrite tar files
zipfile = '%s.%s.tar.bz2' % (pid.upper(),obsdate)
if (os.path.isfile(pidpath + zipfile) and clobber):
saltio.delete(pidpath+zipfile)
elif (os.path.isfile(pidpath+zipfile) and not clobber):
message = 'ERROR: SALTFTP -- file ' + pidpath + zipfile + ' exists. Use clobber=y'
raise SaltError(message)
saltio.changedir(pidpath)
tfile = tarfile.open(zipfile,'w:bz2')
tfile.dereference = True
log.message('\nSALTFTP -- tarring data for %s in %s' % (pid.upper(), zipfile))
for zfile in glob.glob('*'):
if 'bz2' not in zfile:
log.message('SALTFTP -- ' + pid.upper() + '/' + zipfile + ' <-- ' + zfile)
tfile.add(zfile)
tfile.close()
#Copy data to the beachhead location
message=movetobeachhead(pid, zipfile, datapath, beachdir, sdbhost, sdbname, sdbuser,password,clobber)
log.message(message)
#delete tar files
saltio.delete(datapath+pid.upper()+'/'+zipfile)
def getproposalcodes(obsdate, sdbhost,sdbname,sdbuser, sdbpassword):
"""Retrieve all the proposal observed on a given date"""
db=saltmysql.connectdb(sdbhost,sdbname,sdbuser,sdbpassword)
state_select='Distinct Proposal_Code'
state_from='FileData join ProposalCode using (ProposalCode_Id)'
state_logic="FileName like '%"+obsdate+"%'"
record=saltmysql.select(db,state_select,state_from,state_logic)
if len(record)==0:
return []
else:
pids=[x[0] for x in record]
pids=saltio.removebadpids(pids)
pids=saltio.removeengineeringpids(pids)
return pids
def getdatefrompath(datapath):
"""From the datapath, retrieve the date"""
date=''
for x in datapath:
if x in string.digits:
date+=x
return date
def movetobeachhead(pid,zipfile, datapath, beachdir, sdbhost, sdbname, sdbuser,password, clobber):
"""sub-routine to move the files to the mirror location for the beachhead
returns status
"""
#change to the PI directory
pipath = datapath + pid.upper()
saltio.changedir(pipath)
#log into database and determine username from proposer name
sdbpassword=password
db=saltmysql.connectdb(sdbhost,sdbname,sdbuser,sdbpassword)
state_select='p.Username'
state_from='ProposalCode as c join ProposalContact as pc using (ProposalCode_Id) join Investigator as i on (pc.Contact_Id=i.Investigator_Id) join PiptUser as p using (PiptUser_Id)'
state_logic="c.Proposal_Code='%s'" % pid
record=saltmysql.select(db,state_select,state_from,state_logic)
record=set(record)
#check to see if it was successful and raise an error if not
if len(record)<1:
message='SALTFTP--Unable to find username for %s' % pid
raise SaltError(message)
#go through and add the data to each entry in username
for entry in record:
pi_username=entry[0]
beachpath = beachdir + pi_username
#copy the zip file to the beachhead directory
if not (os.path.exists(beachpath)):
os.mkdir(beachpath)
try:
beachzipfile=beachpath+'/'+zipfile
if clobber and os.path.exists(beachzipfile): saltio.delete(beachzipfile)
#copy the file
saltio.copy(zipfile,beachzipfile)
message = 'SALTFTP -- %s/%s --> %s' % (pid.upper(), zipfile , beachzipfile)
except:
msg='SALTFTP--Could not copy %s to %s because %s does not exist' \
% (zipfile, beachpath, beachpath)
raise SaltError(msg)
return message
def movetoftpsite(pid, zipfile, datapath, server, username, password):
"""Move the data to the ftp site"""
#move into the right directory
pidpath = datapath + pid.upper()
saltio.changedir(pidpath)
#log into the ftp server
try:
ftpserv = ftplib.FTP(server,username,password)
ftpserv.set_pasv(False)
except Exception, e:
message='ERROR: SALTFTP -- cannot connect to %s due to %s' % ( server, e)
raise SaltError(message)
try:
ftpserv.cwd(pid.upper())
except:
ftpserv.mkd(pid.upper())
ftpserv.cwd(pid.upper())
try:
f = open(zipfile,'rb')
except Exception, e:
message='ERROR: SALTFTP -- cannot open %s due to %s' % (zipfile, e)
raise SaltError(message)
try:
ftpserv.storbinary('STOR ' + zipfile,f)
except Exception, e:
message = 'ERROR: SALTFTP -- cannot transfer %s to %s due to %s' % (zipfile, server, e)
raise SaltError(message)
try:
ftpserv.quit()
except Exception, e:
message='ERROR: SALTFTP -- cannot disconnect from %s due to %s' % (server, e)
return
# -----------------------------------------------------------
# main code
if not iraf.deftask('saltftp'):
parfile = iraf.osfn("pipetools$saltftp.par")
t = iraf.IrafTaskFactory(taskname="saltftp",value=parfile,function=saltftp, pkgname='pipetools')