##// END OF EJS Templates
A new SendToServer Unit has been created to upload files to a remote server....
Miguel Valdez -
r573:e93e8802031b
parent child
Show More
@@ -0,0 +1,27
1 '''
2 @author: roj-idl71
3 '''
4 #USED IN jroplot_spectra.py
5 RTI_CODE = 0 #Range time intensity (RTI).
6 SPEC_CODE = 1 #Spectra (and Cross-spectra) information.
7 CROSS_CODE = 2 #Cross-Correlation information.
8 COH_CODE = 3 #Coherence map.
9 BASE_CODE = 4 #Base lines graphic.
10 ROW_CODE = 5 #Row Spectra.
11 TOTAL_CODE = 6 #Total Power.
12 DRIFT_CODE = 7 #Drifts graphics.
13 HEIGHT_CODE = 8 #Height profile.
14 PHASE_CODE = 9 #Signal Phase.
15
16 POWER_CODE = 16
17 NOISE_CODE = 17
18 BEACON_CODE = 18
19
20 #USED IN jroplot_parameters.py
21
22 MOMENTS_CODE = 20
23 SKYMAP_CODE = 21
24 WIND_CODE = 22
25 PARMS_CODE = 23
26 SPECFIT_CODE = 24
27 EWDRIFT_CODE = 25
@@ -0,0 +1,464
1 # scp.py
2 # Copyright (C) 2008 James Bardin <j.bardin@gmail.com>
3
4 """
5 Utilities for sending files over ssh using the scp1 protocol.
6 """
7
8 __version__ = '0.10.0'
9
10 import locale
11 import os
12 import re
13 from socket import timeout as SocketTimeout
14
15
16 # this is quote from the shlex module, added in py3.3
17 _find_unsafe = re.compile(br'[^\w@%+=:,./~-]').search
18
19
20 def _sh_quote(s):
21 """Return a shell-escaped version of the string `s`."""
22 if not s:
23 return b""
24 if _find_unsafe(s) is None:
25 return s
26
27 # use single quotes, and put single quotes into double quotes
28 # the string $'b is then quoted as '$'"'"'b'
29 return b"'" + s.replace(b"'", b"'\"'\"'") + b"'"
30
31
32 # Unicode conversion functions; assume UTF-8
33
34 def asbytes(s):
35 """Turns unicode into bytes, if needed.
36 Assumes UTF-8.
37 """
38 if isinstance(s, bytes):
39 return s
40 else:
41 return s.encode('utf-8')
42
43
44 def asunicode(s):
45 """Turns bytes into unicode, if needed.
46 Uses UTF-8.
47 """
48 if isinstance(s, bytes):
49 return s.decode('utf-8', 'replace')
50 else:
51 return s
52
53
54 # os.path.sep is unicode on Python 3, no matter the platform
55 bytes_sep = asbytes(os.path.sep)
56
57
58 # Unicode conversion function for Windows
59 # Used to convert local paths if the local machine is Windows
60
61 def asunicode_win(s):
62 """Turns bytes into unicode, if needed.
63 """
64 if isinstance(s, bytes):
65 return s.decode(locale.getpreferredencoding())
66 else:
67 return s
68
69
70 class SCPClient(object):
71 """
72 An scp1 implementation, compatible with openssh scp.
73 Raises SCPException for all transport related errors. Local filesystem
74 and OS errors pass through.
75 Main public methods are .put and .get
76 The get method is controlled by the remote scp instance, and behaves
77 accordingly. This means that symlinks are resolved, and the transfer is
78 halted after too many levels of symlinks are detected.
79 The put method uses os.walk for recursion, and sends files accordingly.
80 Since scp doesn't support symlinks, we send file symlinks as the file
81 (matching scp behaviour), but we make no attempt at symlinked directories.
82 """
83 def __init__(self, transport, buff_size=16384, socket_timeout=5.0,
84 progress=None, sanitize=_sh_quote):
85 """
86 Create an scp1 client.
87 @param transport: an existing paramiko L{Transport}
88 @type transport: L{Transport}
89 @param buff_size: size of the scp send buffer.
90 @type buff_size: int
91 @param socket_timeout: channel socket timeout in seconds
92 @type socket_timeout: float
93 @param progress: callback - called with (filename, size, sent) during
94 transfers
95 @param sanitize: function - called with filename, should return
96 safe or escaped string. Uses _sh_quote by default.
97 @type progress: function(string, int, int)
98 """
99 self.transport = transport
100 self.buff_size = buff_size
101 self.socket_timeout = socket_timeout
102 self.channel = None
103 self.preserve_times = False
104 self._progress = progress
105 self._recv_dir = b''
106 self._rename = False
107 self._utime = None
108 self.sanitize = sanitize
109 self._dirtimes = {}
110
111 def __enter__(self):
112 self.channel = self._open()
113 return self
114
115 def __exit__(self, type, value, traceback):
116 self.close()
117
118 def put(self, files, remote_path=b'.',
119 recursive=False, preserve_times=False):
120 """
121 Transfer files to remote host.
122 @param files: A single path, or a list of paths to be transfered.
123 recursive must be True to transfer directories.
124 @type files: string OR list of strings
125 @param remote_path: path in which to receive the files on the remote
126 host. defaults to '.'
127 @type remote_path: str
128 @param recursive: transfer files and directories recursively
129 @type recursive: bool
130 @param preserve_times: preserve mtime and atime of transfered files
131 and directories.
132 @type preserve_times: bool
133 """
134 self.preserve_times = preserve_times
135 self.channel = self._open()
136 self._pushed = 0
137 self.channel.settimeout(self.socket_timeout)
138 scp_command = (b'scp -t ', b'scp -r -t ')[recursive]
139 self.channel.exec_command(scp_command +
140 self.sanitize(asbytes(remote_path)))
141 self._recv_confirm()
142
143 if not isinstance(files, (list, tuple)):
144 files = [files]
145
146 if recursive:
147 self._send_recursive(files)
148 else:
149 self._send_files(files)
150
151 self.close()
152
153 def get(self, remote_path, local_path='',
154 recursive=False, preserve_times=False):
155 """
156 Transfer files from remote host to localhost
157 @param remote_path: path to retreive from remote host. since this is
158 evaluated by scp on the remote host, shell wildcards and
159 environment variables may be used.
160 @type remote_path: str
161 @param local_path: path in which to receive files locally
162 @type local_path: str
163 @param recursive: transfer files and directories recursively
164 @type recursive: bool
165 @param preserve_times: preserve mtime and atime of transfered files
166 and directories.
167 @type preserve_times: bool
168 """
169 if not isinstance(remote_path, (list, tuple)):
170 remote_path = [remote_path]
171 remote_path = [self.sanitize(asbytes(r)) for r in remote_path]
172 self._recv_dir = local_path or os.getcwd()
173 self._rename = (len(remote_path) == 1 and
174 not os.path.isdir(os.path.abspath(local_path)))
175 if len(remote_path) > 1:
176 if not os.path.exists(self._recv_dir):
177 raise SCPException("Local path '%s' does not exist" %
178 asunicode(self._recv_dir))
179 elif not os.path.isdir(self._recv_dir):
180 raise SCPException("Local path '%s' is not a directory" %
181 asunicode(self._recv_dir))
182 rcsv = (b'', b' -r')[recursive]
183 prsv = (b'', b' -p')[preserve_times]
184 self.channel = self._open()
185 self._pushed = 0
186 self.channel.settimeout(self.socket_timeout)
187 self.channel.exec_command(b"scp" +
188 rcsv +
189 prsv +
190 b" -f " +
191 b' '.join(remote_path))
192 self._recv_all()
193 self.close()
194
195 def _open(self):
196 """open a scp channel"""
197 if self.channel is None:
198 self.channel = self.transport.open_session()
199
200 return self.channel
201
202 def close(self):
203 """close scp channel"""
204 if self.channel is not None:
205 self.channel.close()
206 self.channel = None
207
208 def _read_stats(self, name):
209 """return just the file stats needed for scp"""
210 if os.name == 'nt':
211 name = asunicode(name)
212 stats = os.stat(name)
213 mode = oct(stats.st_mode)[-4:]
214 size = stats.st_size
215 atime = int(stats.st_atime)
216 mtime = int(stats.st_mtime)
217 return (mode, size, mtime, atime)
218
219 def _send_files(self, files):
220 for name in files:
221 basename = asbytes(os.path.basename(name))
222 (mode, size, mtime, atime) = self._read_stats(name)
223 if self.preserve_times:
224 self._send_time(mtime, atime)
225 file_hdl = open(name, 'rb')
226
227 # The protocol can't handle \n in the filename.
228 # Quote them as the control sequence \^J for now,
229 # which is how openssh handles it.
230 self.channel.sendall(("C%s %d " % (mode, size)).encode('ascii') +
231 basename.replace(b'\n', b'\\^J') + b"\n")
232 self._recv_confirm()
233 file_pos = 0
234 if self._progress:
235 if size == 0:
236 # avoid divide-by-zero
237 self._progress(basename, 1, 1)
238 else:
239 self._progress(basename, size, 0)
240 buff_size = self.buff_size
241 chan = self.channel
242 while file_pos < size:
243 chan.sendall(file_hdl.read(buff_size))
244 file_pos = file_hdl.tell()
245 if self._progress:
246 self._progress(basename, size, file_pos)
247 chan.sendall('\x00')
248 file_hdl.close()
249 self._recv_confirm()
250
251 def _chdir(self, from_dir, to_dir):
252 # Pop until we're one level up from our next push.
253 # Push *once* into to_dir.
254 # This is dependent on the depth-first traversal from os.walk
255
256 # add path.sep to each when checking the prefix, so we can use
257 # path.dirname after
258 common = os.path.commonprefix([from_dir + bytes_sep,
259 to_dir + bytes_sep])
260 # now take the dirname, since commonprefix is character based,
261 # and we either have a seperator, or a partial name
262 common = os.path.dirname(common)
263 cur_dir = from_dir.rstrip(bytes_sep)
264 while cur_dir != common:
265 cur_dir = os.path.split(cur_dir)[0]
266 self._send_popd()
267 # now we're in our common base directory, so on
268 self._send_pushd(to_dir)
269
270 def _send_recursive(self, files):
271 for base in files:
272 if not os.path.isdir(base):
273 # filename mixed into the bunch
274 self._send_files([base])
275 continue
276 last_dir = asbytes(base)
277 for root, dirs, fls in os.walk(base):
278 self._chdir(last_dir, asbytes(root))
279 self._send_files([os.path.join(root, f) for f in fls])
280 last_dir = asbytes(root)
281 # back out of the directory
282 while self._pushed > 0:
283 self._send_popd()
284
285 def _send_pushd(self, directory):
286 (mode, size, mtime, atime) = self._read_stats(directory)
287 basename = asbytes(os.path.basename(directory))
288 if self.preserve_times:
289 self._send_time(mtime, atime)
290 self.channel.sendall(('D%s 0 ' % mode).encode('ascii') +
291 basename.replace(b'\n', b'\\^J') + b'\n')
292 self._recv_confirm()
293 self._pushed += 1
294
295 def _send_popd(self):
296 self.channel.sendall('E\n')
297 self._recv_confirm()
298 self._pushed -= 1
299
300 def _send_time(self, mtime, atime):
301 self.channel.sendall(('T%d 0 %d 0\n' % (mtime, atime)).encode('ascii'))
302 self._recv_confirm()
303
304 def _recv_confirm(self):
305 # read scp response
306 msg = b''
307 try:
308 msg = self.channel.recv(512)
309 except SocketTimeout:
310 raise SCPException('Timout waiting for scp response')
311 # slice off the first byte, so this compare will work in py2 and py3
312 if msg and msg[0:1] == b'\x00':
313 return
314 elif msg and msg[0:1] == b'\x01':
315 raise SCPException(asunicode(msg[1:]))
316 elif self.channel.recv_stderr_ready():
317 msg = self.channel.recv_stderr(512)
318 raise SCPException(asunicode(msg))
319 elif not msg:
320 raise SCPException('No response from server')
321 else:
322 raise SCPException('Invalid response from server', msg)
323
324 def _recv_all(self):
325 # loop over scp commands, and receive as necessary
326 command = {b'C': self._recv_file,
327 b'T': self._set_time,
328 b'D': self._recv_pushd,
329 b'E': self._recv_popd}
330 while not self.channel.closed:
331 # wait for command as long as we're open
332 self.channel.sendall('\x00')
333 msg = self.channel.recv(1024)
334 if not msg: # chan closed while recving
335 break
336 assert msg[-1:] == b'\n'
337 msg = msg[:-1]
338 code = msg[0:1]
339 try:
340 command[code](msg[1:])
341 except KeyError:
342 raise SCPException(asunicode(msg[1:]))
343 # directory times can't be set until we're done writing files
344 self._set_dirtimes()
345
346 def _set_time(self, cmd):
347 try:
348 times = cmd.split(b' ')
349 mtime = int(times[0])
350 atime = int(times[2]) or mtime
351 except:
352 self.channel.send(b'\x01')
353 raise SCPException('Bad time format')
354 # save for later
355 self._utime = (atime, mtime)
356
357 def _recv_file(self, cmd):
358 chan = self.channel
359 parts = cmd.strip().split(b' ', 2)
360
361 try:
362 mode = int(parts[0], 8)
363 size = int(parts[1])
364 if self._rename:
365 path = self._recv_dir
366 self._rename = False
367 elif os.name == 'nt':
368 path = os.path.join(asunicode_win(self._recv_dir),
369 parts[2].decode('utf-8'))
370 else:
371 path = os.path.join(asbytes(self._recv_dir),
372 parts[2])
373 except:
374 chan.send('\x01')
375 chan.close()
376 raise SCPException('Bad file format')
377
378 try:
379 file_hdl = open(path, 'wb')
380 except IOError as e:
381 chan.send(b'\x01' + str(e).encode('utf-8'))
382 chan.close()
383 raise
384
385 if self._progress:
386 if size == 0:
387 # avoid divide-by-zero
388 self._progress(path, 1, 1)
389 else:
390 self._progress(path, size, 0)
391 buff_size = self.buff_size
392 pos = 0
393 chan.send(b'\x00')
394 try:
395 while pos < size:
396 # we have to make sure we don't read the final byte
397 if size - pos <= buff_size:
398 buff_size = size - pos
399 file_hdl.write(chan.recv(buff_size))
400 pos = file_hdl.tell()
401 if self._progress:
402 self._progress(path, size, pos)
403
404 msg = chan.recv(512)
405 if msg and msg[0:1] != b'\x00':
406 raise SCPException(asunicode(msg[1:]))
407 except SocketTimeout:
408 chan.close()
409 raise SCPException('Error receiving, socket.timeout')
410
411 file_hdl.truncate()
412 try:
413 os.utime(path, self._utime)
414 self._utime = None
415 os.chmod(path, mode)
416 # should we notify the other end?
417 finally:
418 file_hdl.close()
419 # '\x00' confirmation sent in _recv_all
420
421 def _recv_pushd(self, cmd):
422 parts = cmd.split(b' ', 2)
423 try:
424 mode = int(parts[0], 8)
425 if self._rename:
426 path = self._recv_dir
427 self._rename = False
428 elif os.name == 'nt':
429 path = os.path.join(asunicode_win(self._recv_dir),
430 parts[2].decode('utf-8'))
431 else:
432 path = os.path.join(asbytes(self._recv_dir),
433 parts[2])
434 except:
435 self.channel.send(b'\x01')
436 raise SCPException('Bad directory format')
437 try:
438 if not os.path.exists(path):
439 os.mkdir(path, mode)
440 elif os.path.isdir(path):
441 os.chmod(path, mode)
442 else:
443 raise SCPException('%s: Not a directory' % path)
444 self._dirtimes[path] = (self._utime)
445 self._utime = None
446 self._recv_dir = path
447 except (OSError, SCPException) as e:
448 self.channel.send(b'\x01' + asbytes(str(e)))
449 raise
450
451 def _recv_popd(self, *cmd):
452 self._recv_dir = os.path.split(self._recv_dir)[0]
453
454 def _set_dirtimes(self):
455 try:
456 for d in self._dirtimes:
457 os.utime(d, self._dirtimes[d])
458 finally:
459 self._dirtimes = {}
460
461
462 class SCPException(Exception):
463 """SCP exception class"""
464 pass No newline at end of file
@@ -378,15 +378,6 class ProcUnitConf():
378 378 self.name = upElement.get('name')
379 379 self.datatype = upElement.get('datatype')
380 380 self.inputId = upElement.get('inputId')
381
382 #Compatible with old signal chain version
383 if self.ELEMENTNAME == ReadUnitConf().getElementName():
384 if 'Reader' not in self.name:
385 self.name += 'Reader'
386
387 if self.ELEMENTNAME == ProcUnitConf().getElementName():
388 if 'Proc' not in self.name:
389 self.name += 'Proc'
390 381
391 382 self.opConfObjList = []
392 383
@@ -445,7 +436,13 class ProcUnitConf():
445 436 finalSts = finalSts or sts
446 437
447 438 return finalSts
448
439
440 def close(self):
441
442 self.procUnitObj.close()
443
444 return
445
449 446 class ReadUnitConf(ProcUnitConf):
450 447
451 448 path = None
@@ -559,7 +556,7 class Project():
559 556
560 557 return readUnitConfObj
561 558
562 def addProcUnit(self, inputId, datatype=None, name=None):
559 def addProcUnit(self, inputId=0, datatype=None, name=None):
563 560
564 561 #Compatible with old signal chain version
565 562 if datatype==None and name==None:
@@ -707,7 +704,21 class Project():
707 704 if not(finalSts):
708 705 print "Every process unit have finished"
709 706 break
710
707
708 #Closing every process
709 for procKey in keyList:
710 procUnitConfObj = self.procUnitConfObjDict[procKey]
711 procUnitConfObj.close()
712
713 def start(self, filename):
714
715 self.writeXml(filename)
716 self.readXml(filename)
717
718 self.createObjects()
719 self.connectObjects()
720 self.run()
721
711 722 if __name__ == '__main__':
712 723
713 724 desc = "Segundo Test"
@@ -39,6 +39,10 class Figure:
39 39 xmin = None
40 40 xmax = None
41 41
42 counter_imagwr = 0
43
44 figfile = None
45
42 46 def __init__(self):
43 47
44 48 raise ValueError, "This method is not implemented"
@@ -175,6 +179,7 class Figure:
175 179 show=show)
176 180
177 181 self.axesObjList = []
182 self.counter_imagwr = 0
178 183
179 184
180 185 def setDriver(self, driver=mpldriver):
@@ -224,8 +229,45 class Figure:
224 229
225 230 self.__driver.saveFigure(self.fig, filename, *args)
226 231
227
228
232 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
233
234 if not save:
235 return
236
237 if figfile == None:
238
239 if not thisDatetime:
240 raise ValueError, "Saving figure: figfile or thisDatetime should be defined"
241 return
242
243 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
244 figfile = self.getFilename(name = str_datetime)
245
246 if self.figfile == None:
247 self.figfile = figfile
248
249 if update_figfile:
250 self.figfile = figfile
251
252 self.counter_imagwr += 1
253
254 if self.counter_imagwr<wr_period:
255 return
256
257 # store png plot to local folder
258 self.saveFigure(figpath, self.figfile)
259 self.counter_imagwr = 0
260
261 if not ftp:
262 return
263
264 if not thisDatetime:
265 return
266
267 # store png plot to FTP server according to RT-Web format
268 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
269 ftp_filename = os.path.join(figpath, name)
270 self.saveFigure(figpath, ftp_filename)
229 271
230 272 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
231 273 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
@@ -180,19 +180,9 class CorrelationPlot(Figure):
180 180
181 181 self.draw()
182 182
183 if save:
184
185 if figfile == None:
186 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
187 figfile = self.getFilename(name = str_datetime)
188
189 self.counter_imagwr += 1
190 if (self.counter_imagwr>=wr_period):
191 # store png plot to local folder
192 self.saveFigure(figpath, figfile)
193 # store png plot to FTP server according to RT-Web format
194 if ftp:
195 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
196 ftp_filename = os.path.join(figpath, name)
197 self.saveFigure(figpath, ftp_filename)
198 self.counter_imagwr = 0
183 self.save(figpath=figpath,
184 figfile=figfile,
185 save=save,
186 ftp=ftp,
187 wr_period=wr_period,
188 thisDatetime=thisDatetime)
@@ -156,21 +156,12 class SpectraHeisScope(Figure):
156 156
157 157 self.draw()
158 158
159 if save:
160
161 if figfile == None:
162 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
163 figfile = self.getFilename(name = str_datetime)
164
165 self.counter_imagwr += 1
166 if (self.counter_imagwr>=wr_period):
167 # store png plot to local folder
168 self.saveFigure(figpath, figfile)
169 # store png plot to FTP server according to RT-Web format
170 #name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
171 #ftp_filename = os.path.join(figpath, name)
172 #self.saveFigure(figpath, ftp_filename)
173 self.counter_imagwr = 0
159 self.save(figpath=figpath,
160 figfile=figfile,
161 save=save,
162 ftp=ftp,
163 wr_period=wr_period,
164 thisDatetime=thisDatetime)
174 165
175 166 class RTIfromSpectraHeis(Figure):
176 167
@@ -310,19 +301,12 class RTIfromSpectraHeis(Figure):
310 301 del self.xdata
311 302 del self.ydata
312 303 self.__isConfig = False
313
314 if save:
304 self.figfile = None
315 305
316 if self.figfile == None:
317 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
318 self.figfile = self.getFilename(name = str_datetime)
319
320 self.counter_imagwr += 1
321 if (self.counter_imagwr>=wr_period):
322 # store png plot to local folder
323 self.saveFigure(figpath, self.figfile)
324 # store png plot to FTP server according to RT-Web format
325 #name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
326 #ftp_filename = os.path.join(figpath, name)
327 #self.saveFigure(figpath, ftp_filename)
328 self.counter_imagwr = 0
306 self.save(figpath=figpath,
307 figfile=figfile,
308 save=save,
309 ftp=ftp,
310 wr_period=wr_period,
311 thisDatetime=thisDatetime,
312 update_figfile=False)
@@ -3,6 +3,7 import datetime
3 3 import numpy
4 4
5 5 from figure import Figure, isRealtime
6 from plotting_codes import *
6 7
7 8 class MomentsPlot(Figure):
8 9
@@ -24,7 +25,8 class MomentsPlot(Figure):
24 25 self.HEIGHTPROF = 0
25 26 self.counter_imagwr = 0
26 27
27 self.PLOT_CODE = 1
28 self.PLOT_CODE = MOMENTS_CODE
29
28 30 self.FTP_WEI = None
29 31 self.EXP_CODE = None
30 32 self.SUB_EXP_CODE = None
@@ -179,23 +181,13 class MomentsPlot(Figure):
179 181 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
180 182
181 183 self.draw()
182
183 if save:
184
185 if figfile == None:
186 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
187 figfile = self.getFilename(name = str_datetime)
188 184
189 self.counter_imagwr += 1
190 if (self.counter_imagwr>=wr_period):
191 # store png plot to local folder
192 self.saveFigure(figpath, figfile)
193 self.counter_imagwr = 0
194 # store png plot to FTP server according to RT-Web format
195 if ftp:
196 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
197 ftp_filename = os.path.join(figpath, name)
198 self.saveFigure(figpath, ftp_filename)
185 self.save(figpath=figpath,
186 figfile=figfile,
187 save=save,
188 ftp=ftp,
189 wr_period=wr_period,
190 thisDatetime=thisDatetime)
199 191
200 192
201 193
@@ -221,7 +213,8 class SkyMapPlot(Figure):
221 213 self.HEIGHTPROF = 0
222 214 self.counter_imagwr = 0
223 215
224 self.PLOT_CODE = 1
216 self.PLOT_CODE = SKYMAP_CODE
217
225 218 self.FTP_WEI = None
226 219 self.EXP_CODE = None
227 220 self.SUB_EXP_CODE = None
@@ -324,35 +317,13 class SkyMapPlot(Figure):
324 317 ticksize=9, cblabel='')
325 318
326 319 self.draw()
327
328 if save:
329 320
330 self.counter_imagwr += 1
331 if (self.counter_imagwr==wr_period):
332
333 if figfile == None:
334 figfile = self.getFilename(name = self.name)
335
336 self.saveFigure(figpath, figfile)
337 self.counter_imagwr = 0
338
339 if ftp:
340 #provisionalmente envia archivos en el formato de la web en tiempo real
341 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
342 path = '%s%03d' %(self.PREFIX, self.id)
343 ftp_file = os.path.join(path,'ftp','%s.png'%name)
344 self.saveFigure(figpath, ftp_file)
345 ftp_filename = os.path.join(figpath,ftp_file)
346
347
348 try:
349 self.sendByFTP(ftp_filename, server, folder, username, password)
350 except:
351 self.counter_imagwr = 0
352 raise ValueError, 'Error FTP'
353
354
355
321 self.save(figpath=figpath,
322 figfile=figfile,
323 save=save,
324 ftp=ftp,
325 wr_period=wr_period,
326 thisDatetime=thisDatetime)
356 327
357 328 class WindProfilerPlot(Figure):
358 329
@@ -375,7 +346,8 class WindProfilerPlot(Figure):
375 346 self.HEIGHTPROF = 0
376 347 self.counter_imagwr = 0
377 348
378 self.PLOT_CODE = 0
349 self.PLOT_CODE = WIND_CODE
350
379 351 self.FTP_WEI = None
380 352 self.EXP_CODE = None
381 353 self.SUB_EXP_CODE = None
@@ -559,32 +531,19 class WindProfilerPlot(Figure):
559 531
560 532 self.draw()
561 533
562 if save:
563
564 if self.figfile == None:
565 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
566 self.figfile = self.getFilename(name = str_datetime)
567
568 self.counter_imagwr += 1
569
570 if (self.counter_imagwr>=wr_period):
571 # store png plot to local folder
572 self.saveFigure(figpath, self.figfile)
573 self.counter_imagwr = 0
574
575 if ftp:
576 # store png plot to FTP server according to RT-Web format
577 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
578 ftp_filename = os.path.join(figpath, name)
579 self.saveFigure(figpath, ftp_filename)
580
581
582
583 534 if x[1] >= self.axesList[0].xmax:
584 535 self.counter_imagwr = wr_period
585 536 self.__isConfig = False
586 537 self.figfile = None
587 538
539 self.save(figpath=figpath,
540 figfile=figfile,
541 save=save,
542 ftp=ftp,
543 wr_period=wr_period,
544 thisDatetime=thisDatetime,
545 update_figfile=False)
546
588 547
589 548 class ParametersPlot(Figure):
590 549
@@ -607,7 +566,8 class ParametersPlot(Figure):
607 566 self.HEIGHTPROF = 0
608 567 self.counter_imagwr = 0
609 568
610 self.PLOT_CODE = 0
569 self.PLOT_CODE = PARMS_CODE
570
611 571 self.FTP_WEI = None
612 572 self.EXP_CODE = None
613 573 self.SUB_EXP_CODE = None
@@ -786,31 +746,19 class ParametersPlot(Figure):
786 746
787 747 self.draw()
788 748
789 if save:
790
791 if self.figfile == None:
792 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
793 self.figfile = self.getFilename(name = str_datetime)
794
795 self.counter_imagwr += 1
796
797 if (self.counter_imagwr>=wr_period):
798 # store png plot to local folder
799 self.saveFigure(figpath, self.figfile)
800 self.counter_imagwr = 0
801
802 if ftp:
803 # store png plot to FTP server according to RT-Web format
804 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
805 ftp_filename = os.path.join(figpath, name)
806 self.saveFigure(figpath, ftp_filename)
807
808 749 if x[1] >= self.axesList[0].xmax:
809 750 self.counter_imagwr = wr_period
810 751 self.__isConfig = False
811 752 self.figfile = None
812 753
813
754 self.save(figpath=figpath,
755 figfile=figfile,
756 save=save,
757 ftp=ftp,
758 wr_period=wr_period,
759 thisDatetime=thisDatetime,
760 update_figfile=False)
761
814 762 class SpectralFittingPlot(Figure):
815 763
816 764 __isConfig = None
@@ -828,6 +776,8 class SpectralFittingPlot(Figure):
828 776 self.__isConfig = False
829 777 self.__nsubplots = 1
830 778
779 self.PLOT_CODE = SPECFIT_CODE
780
831 781 self.WIDTH = 450
832 782 self.HEIGHT = 250
833 783 self.WIDTHPROF = 0
@@ -977,13 +927,13 class SpectralFittingPlot(Figure):
977 927 linestyle='solid', grid='both')
978 928
979 929 self.draw()
980
981 if save:
982 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
983 if figfile == None:
984 figfile = self.getFilename(name = date)
985
986 self.saveFigure(figpath, figfile)
930
931 self.save(figpath=figpath,
932 figfile=figfile,
933 save=save,
934 ftp=ftp,
935 wr_period=wr_period,
936 thisDatetime=thisDatetime)
987 937
988 938
989 939 class EWDriftsPlot(Figure):
@@ -1007,7 +957,8 class EWDriftsPlot(Figure):
1007 957 self.HEIGHTPROF = 0
1008 958 self.counter_imagwr = 0
1009 959
1010 self.PLOT_CODE = 0
960 self.PLOT_CODE = EWDRIFT_CODE
961
1011 962 self.FTP_WEI = None
1012 963 self.EXP_CODE = None
1013 964 self.SUB_EXP_CODE = None
@@ -1183,26 +1134,15 class EWDriftsPlot(Figure):
1183 1134
1184 1135 self.draw()
1185 1136
1186 if save:
1187
1188 if self.figfile == None:
1189 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1190 self.figfile = self.getFilename(name = str_datetime)
1191
1192 self.counter_imagwr += 1
1193
1194 if (self.counter_imagwr>=wr_period):
1195 # store png plot to local folder
1196 self.saveFigure(figpath, self.figfile)
1197 self.counter_imagwr = 0
1198
1199 if ftp:
1200 # store png plot to FTP server according to RT-Web format
1201 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1202 ftp_filename = os.path.join(figpath, name)
1203 self.saveFigure(figpath, ftp_filename)
1204
1205 1137 if x[1] >= self.axesList[0].xmax:
1206 1138 self.counter_imagwr = wr_period
1207 1139 self.__isConfig = False
1208 self.figfile = None No newline at end of file
1140 self.figfile = None
1141
1142 self.save(figpath=figpath,
1143 figfile=figfile,
1144 save=save,
1145 ftp=ftp,
1146 wr_period=wr_period,
1147 thisDatetime=thisDatetime,
1148 update_figfile=False) No newline at end of file
@@ -8,6 +8,7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure, isRealtime
11 from plotting_codes import *
11 12
12 13 class SpectraPlot(Figure):
13 14
@@ -29,7 +30,8 class SpectraPlot(Figure):
29 30 self.HEIGHTPROF = 0
30 31 self.counter_imagwr = 0
31 32
32 self.PLOT_CODE = 1
33 self.PLOT_CODE = SPEC_CODE
34
33 35 self.FTP_WEI = None
34 36 self.EXP_CODE = None
35 37 self.SUB_EXP_CODE = None
@@ -188,31 +190,20 class SpectraPlot(Figure):
188 190 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
189 191
190 192 self.draw()
191
192 if save:
193
194 if figfile == None:
195 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
196 figfile = self.getFilename(name = str_datetime)
197 name = str_datetime
198 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
199 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
200 figfile = self.getFilename(name)
201
202 self.counter_imagwr += 1
203
204 if (self.counter_imagwr>=wr_period):
205 # store png plot to local folder
206 self.saveFigure(figpath, figfile)
207 self.counter_imagwr = 0
193
194 if figfile == None:
195 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
196 name = str_datetime
197 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
198 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
199 figfile = self.getFilename(name)
208 200
209 if ftp:
210 # store png plot to FTP server according to RT-Web format
211 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
212 ftp_filename = os.path.join(figpath, name)
213 self.saveFigure(figpath, ftp_filename)
214
215
201 self.save(figpath=figpath,
202 figfile=figfile,
203 save=save,
204 ftp=ftp,
205 wr_period=wr_period,
206 thisDatetime=thisDatetime)
216 207
217 208 class CrossSpectraPlot(Figure):
218 209
@@ -235,7 +226,7 class CrossSpectraPlot(Figure):
235 226 self.WIDTHPROF = 0
236 227 self.HEIGHTPROF = 0
237 228
238 self.PLOT_CODE = 1
229 self.PLOT_CODE = CROSS_CODE
239 230 self.FTP_WEI = None
240 231 self.EXP_CODE = None
241 232 self.SUB_EXP_CODE = None
@@ -397,24 +388,12 class CrossSpectraPlot(Figure):
397 388
398 389 self.draw()
399 390
400 if save != '':
401
402 if figfile == None:
403 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
404 figfile = self.getFilename(name = str_datetime)
405
406 self.counter_imagwr += 1
407
408 if (self.counter_imagwr>=wr_period):
409 # store png plot to local folder
410 self.saveFigure(figpath, figfile)
411 self.counter_imagwr = 0
412
413 if ftp:
414 # store png plot to FTP server according to RT-Web format
415 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
416 ftp_filename = os.path.join(figpath, name)
417 self.saveFigure(figpath, ftp_filename)
391 self.save(figpath=figpath,
392 figfile=figfile,
393 save=save,
394 ftp=ftp,
395 wr_period=wr_period,
396 thisDatetime=thisDatetime)
418 397
419 398
420 399 class RTIPlot(Figure):
@@ -438,7 +417,8 class RTIPlot(Figure):
438 417 self.HEIGHTPROF = 0
439 418 self.counter_imagwr = 0
440 419
441 self.PLOT_CODE = 0
420 self.PLOT_CODE = RTI_CODE
421
442 422 self.FTP_WEI = None
443 423 self.EXP_CODE = None
444 424 self.SUB_EXP_CODE = None
@@ -603,30 +583,19 class RTIPlot(Figure):
603 583 grid='x')
604 584
605 585 self.draw()
606
607 if save:
608 586
609 if self.figfile == None:
610 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
611 self.figfile = self.getFilename(name = str_datetime)
612
613 self.counter_imagwr += 1
614
615 if (self.counter_imagwr>=wr_period):
616 # store png plot to local folder
617 self.saveFigure(figpath, self.figfile)
618 self.counter_imagwr = 0
619
620 if ftp:
621 # store png plot to FTP server according to RT-Web format
622 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
623 ftp_filename = os.path.join(figpath, name)
624 self.saveFigure(figpath, ftp_filename)
625
626 587 if x[1] >= self.axesList[0].xmax:
627 588 self.counter_imagwr = wr_period
628 589 self.__isConfig = False
629 590 self.figfile = None
591
592 self.save(figpath=figpath,
593 figfile=figfile,
594 save=save,
595 ftp=ftp,
596 wr_period=wr_period,
597 thisDatetime=thisDatetime,
598 update_figfile=False)
630 599
631 600 class CoherenceMap(Figure):
632 601 isConfig = None
@@ -647,7 +616,8 class CoherenceMap(Figure):
647 616 self.HEIGHTPROF = 0
648 617 self.counter_imagwr = 0
649 618
650 self.PLOT_CODE = 3
619 self.PLOT_CODE = COH_CODE
620
651 621 self.FTP_WEI = None
652 622 self.EXP_CODE = None
653 623 self.SUB_EXP_CODE = None
@@ -815,27 +785,18 class CoherenceMap(Figure):
815 785 if x[1] >= self.axesList[0].xmax:
816 786 self.counter_imagwr = wr_period
817 787 self.__isConfig = False
818
819 if save:
820
821 if figfile == None:
822 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
823 figfile = self.getFilename(name = str_datetime)
824
825 self.counter_imagwr += 1
788 self.figfile = None
826 789
827 if (self.counter_imagwr>=wr_period):
828 # store png plot to local folder
829 self.saveFigure(figpath, figfile)
830 self.counter_imagwr = 0
831
832 if ftp:
833 # store png plot to FTP server according to RT-Web format
834 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
835 ftp_filename = os.path.join(figpath, name)
836 self.saveFigure(figpath, ftp_filename)
790 self.save(figpath=figpath,
791 figfile=figfile,
792 save=save,
793 ftp=ftp,
794 wr_period=wr_period,
795 thisDatetime=thisDatetime,
796 update_figfile=False)
837 797
838 798 class PowerProfile(Figure):
799
839 800 isConfig = None
840 801 __nsubplots = None
841 802
@@ -847,6 +808,8 class PowerProfile(Figure):
847 808 self.isConfig = False
848 809 self.__nsubplots = 1
849 810
811 self.PLOT_CODE = POWER_CODE
812
850 813 self.WIDTH = 300
851 814 self.HEIGHT = 500
852 815 self.counter_imagwr = 0
@@ -948,18 +911,12 class PowerProfile(Figure):
948 911
949 912 self.draw()
950 913
951 if save:
952
953 if figfile == None:
954 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
955 figfile = self.getFilename(name = str_datetime)
956
957 self.counter_imagwr += 1
958
959 if (self.counter_imagwr>=wr_period):
960 # store png plot to local folder
961 self.saveFigure(figpath, figfile)
962 self.counter_imagwr = 0
914 self.save(figpath=figpath,
915 figfile=figfile,
916 save=save,
917 ftp=ftp,
918 wr_period=wr_period,
919 thisDatetime=thisDatetime)
963 920
964 921 class Noise(Figure):
965 922
@@ -981,7 +938,8 class Noise(Figure):
981 938 self.xdata = None
982 939 self.ydata = None
983 940
984 self.PLOT_CODE = 17
941 self.PLOT_CODE = NOISE_CODE
942
985 943 self.FTP_WEI = None
986 944 self.EXP_CODE = None
987 945 self.SUB_EXP_CODE = None
@@ -1137,25 +1095,15 class Noise(Figure):
1137 1095 del self.xdata
1138 1096 del self.ydata
1139 1097 self.__isConfig = False
1140
1141 if save != '':
1142
1143 if self.figfile == None:
1144 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1145 self.figfile = self.getFilename(name = str_datetime)
1146
1147 self.counter_imagwr += 1
1098 self.figfile = None
1148 1099
1149 if (self.counter_imagwr>=wr_period):
1150 # store png plot to local folder
1151 self.saveFigure(figpath, self.figfile)
1152 self.counter_imagwr = 0
1153
1154 if ftp:
1155 # store png plot to FTP server according to RT-Web format
1156 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1157 ftp_filename = os.path.join(figpath, name)
1158 self.saveFigure(figpath, ftp_filename)
1100 self.save(figpath=figpath,
1101 figfile=figfile,
1102 save=save,
1103 ftp=ftp,
1104 wr_period=wr_period,
1105 thisDatetime=thisDatetime,
1106 update_figfile=False)
1159 1107
1160 1108
1161 1109 class BeaconPhase(Figure):
@@ -1178,7 +1126,8 class BeaconPhase(Figure):
1178 1126 self.xdata = None
1179 1127 self.ydata = None
1180 1128
1181 self.PLOT_CODE = 18
1129 self.PLOT_CODE = BEACON_CODE
1130
1182 1131 self.FTP_WEI = None
1183 1132 self.EXP_CODE = None
1184 1133 self.SUB_EXP_CODE = None
@@ -1354,22 +1303,12 class BeaconPhase(Figure):
1354 1303 del self.xdata
1355 1304 del self.ydata
1356 1305 self.__isConfig = False
1357
1358 if save:
1359
1360 if self.figfile == None:
1361 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1362 self.figfile = self.getFilename(name = str_datetime)
1363
1364 self.counter_imagwr += 1
1306 self.figfile = None
1365 1307
1366 if (self.counter_imagwr>=wr_period):
1367 # store png plot to local folder
1368 self.saveFigure(figpath, self.figfile)
1369 self.counter_imagwr = 0
1370
1371 if ftp:
1372 # store png plot to FTP server according to RT-Web format
1373 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1374 ftp_filename = os.path.join(figpath, name)
1375 self.saveFigure(figpath, ftp_filename)
1308 self.save(figpath=figpath,
1309 figfile=figfile,
1310 save=save,
1311 ftp=ftp,
1312 wr_period=wr_period,
1313 thisDatetime=thisDatetime,
1314 update_figfile=False)
@@ -172,15 +172,9 class Scope(Figure):
172 172
173 173 self.draw()
174 174
175 if save:
176 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
177 if figfile == None:
178 figfile = self.getFilename(name = date)
179
180 self.saveFigure(figpath, figfile)
181
182 self.counter_imagwr += 1
183 if (ftp and (self.counter_imagwr==wr_period)):
184 ftp_filename = os.path.join(figpath,figfile)
185 self.sendByFTP_Thread(ftp_filename, server, folder, username, password)
186 self.counter_imagwr = 0
175 self.save(figpath=figpath,
176 figfile=figfile,
177 save=save,
178 ftp=ftp,
179 wr_period=wr_period,
180 thisDatetime=thisDatetime)
@@ -598,7 +598,7 class JRODataReader(JRODataIO):
598 598 self.fileSize = fileSize
599 599 self.fp = fp
600 600
601 print "Setting the file: %s"%self.filename
601 print "[Reading] Setting the file: %s"%self.filename
602 602
603 603 return 1
604 604
@@ -681,14 +681,14 class JRODataReader(JRODataIO):
681 681 if self.fp != None: self.fp.close()
682 682 self.fp = open(fullfilename, 'rb')
683 683 self.flagNoMoreFiles = 0
684 print 'Setting the file: %s' % fullfilename
684 print '[Reading] Setting the file: %s' % fullfilename
685 685 else:
686 686 self.fileSize = 0
687 687 self.filename = None
688 688 self.flagIsNewFile = 0
689 689 self.fp = None
690 690 self.flagNoMoreFiles = 1
691 print 'No more Files'
691 print '[Reading] No more files to read'
692 692
693 693 return fileOk_flag
694 694
@@ -959,7 +959,7 class JRODataReader(JRODataIO):
959 959 sleep( self.delay )
960 960
961 961 if not(fullpath):
962 print "There 'isn't any valid file in %s" % path
962 print "[Reading] There 'isn't any valid file in %s" % path
963 963 return None
964 964
965 965 self.year = year
@@ -105,7 +105,7 class USRPReaderAPI(USRPReader, threading.Thread):
105 105 def run(self):
106 106
107 107 '''
108 This method will be called many times so here you should put all your code
108 This method will be called once when start() is called
109 109 '''
110 110
111 111 if not self.isConfig:
@@ -32,7 +32,7 class ProcessingUnit(object):
32 32 self.dataIn = None
33 33 self.dataInList = []
34 34
35 self.dataOut = {}
35 self.dataOut = None
36 36
37 37 self.operations2RunDict = {}
38 38
@@ -108,7 +108,10 class ProcessingUnit(object):
108 108 #
109 109 # if name != 'run':
110 110 # return True
111
111
112 if self.dataOut == None:
113 return False
114
112 115 if self.dataOut.isEmpty():
113 116 return False
114 117
@@ -218,7 +221,11 class ProcessingUnit(object):
218 221 def run(self):
219 222
220 223 raise ValueError, "Not implemented"
221
224
225 def close(self):
226 #Close every thread, queue or any other object here is it is neccesary.
227 return
228
222 229 class Operation(object):
223 230
224 231 """
This diff has been collapsed as it changes many lines, (565 lines changed) Show them Hide them
@@ -4,9 +4,565
4 4 import os
5 5 import glob
6 6 import ftplib
7
8 try:
9 import paramiko
10 import scp
11 except:
12 print "You should install paramiko if you will use SSH protocol to upload files to a server"
13
7 14 import multiprocessing
15
16 import time
17 import threading
18
19
20 try:
21 from gevent import sleep
22 except:
23 from time import sleep
24
8 25 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
9 26
27 class Remote(threading.Thread):
28 """
29 Remote is a parent class used to define the behaviour of FTP and SSH class. These clases are
30 used to upload or download files remotely.
31
32 Non-standard Python modules used:
33 None
34
35 Written by:
36
37 "Miguel Urco":mailto:miguel.urco@jro.igp.gob.pe Jun. 03, 2015
38
39 """
40
41 server = None
42 username = None
43 password = None
44 remotefolder = None
45
46 period = 60
47 fileList = []
48 bussy = False
49
50 def __init__(self, server, username, password, remotefolder, period=60):
51
52 threading.Thread.__init__(self)
53 self._stop = threading.Event()
54
55 self.status = 0
56
57 self.period = period
58 self.fileList = []
59 self.bussy = False
60
61 self.stopFlag = False
62
63 print "[Remote Server] Opening server: %s" %server
64 if self.open(server, username, password, remotefolder):
65 print "[Remote Server] %s server was opened successfully" %server
66
67 def stop(self):
68
69 self.stopFlag = True
70
71 def open(self):
72 """
73 Connect to server and create a connection class (FTP or SSH) to remote server.
74 """
75 raise NotImplementedError, "Implement this method in child class"
76
77 def close(self):
78 """
79 Close connection to server
80 """
81 raise NotImplementedError, "Implement this method in child class"
82
83 def mkdir(self, remotefolder):
84 """
85 Create a folder remotely
86 """
87 raise NotImplementedError, "Implement this method in child class"
88
89 def cd(self, remotefolder):
90 """
91 Change working directory in remote server
92 """
93 raise NotImplementedError, "Implement this method in child class"
94
95 def download(self, filename, localfolder=None):
96 """
97 Download a file from server to local host
98 """
99 raise NotImplementedError, "Implement this method in child class"
100
101 def sendFile(self, fullfilename):
102 """
103 sendFile method is used to upload a local file to the current directory in remote server
104
105 Inputs:
106 fullfilename - full path name of local file to store in remote directory
107
108 Returns:
109 0 in error case else 1
110 """
111 raise NotImplementedError, "Implement this method in child class"
112
113 def upload(self, fullfilename, remotefolder=None):
114 """
115 upload method is used to upload a local file to remote directory. This method changes
116 working directory before sending a file.
117
118 Inputs:
119 fullfilename - full path name of local file to store in remote directory
120
121 remotefolder - remote directory
122
123 Returns:
124 0 in error case else 1
125 """
126 print "[Remote Server] Uploading %s to %s:%s" %(fullfilename, self.server, self.remotefolder)
127
128 if not self.status:
129 return 0
130
131 if remotefolder == None:
132 remotefolder = self.remotefolder
133
134 if not self.cd(remotefolder):
135 return 0
136
137 if not self.sendFile(fullfilename):
138 print "[Remote Server] Error uploading file %s" %fullfilename
139 return 0
140
141 print "[Remote Server] upload finished successfully"
142
143 return 1
144
145 def delete(self, filename):
146 """
147 Remove a file from remote server
148 """
149 pass
150
151 def updateFileList(self, fileList):
152 """
153 Remove a file from remote server
154 """
155
156 if fileList == self.fileList:
157 return 1
158
159 init = time.time()
160
161 while(self.bussy):
162 sleep(0.1)
163 if time.time() - init > 2*self.period:
164 return 0
165
166 self.fileList = fileList
167
168 return 1
169
170 def run(self):
171
172 if not self.cd(self.remotefolder):
173 raise ValueError, "It could not change to the new remote directory: %s" %remotefolder
174
175 while True:
176
177 sleep(self.period)
178
179 self.bussy = True
180
181 for thisFile in self.fileList:
182 self.upload(thisFile, self.remotefolder)
183
184 self.bussy = False
185
186 if self.stopFlag:
187 break
188
189 print "[Remote Server] Thread stopped successfully"
190
191 class FTPClient(Remote):
192
193 __ftpClientObj = None
194
195 def __init__(self, server, username, password, remotefolder, period=60):
196 """
197 """
198 Remote.__init__(self, server, username, password, remotefolder, period)
199
200 def open(self, server, username, password, remotefolder):
201
202 """
203 This method is used to set FTP parameters and establish a connection to remote server
204
205 Inputs:
206 server - remote server IP Address
207
208 username - remote server Username
209
210 password - remote server password
211
212 remotefolder - remote server current working directory
213
214 Return: void
215
216 Affects:
217 self.status - in case of error or fail connection this parameter is set to 0 else 1
218
219 """
220
221 if server == None:
222 raise ValueError, "FTP server should be defined"
223
224 if username == None:
225 raise ValueError, "FTP username should be defined"
226
227 if password == None:
228 raise ValueError, "FTP password should be defined"
229
230 if remotefolder == None:
231 raise ValueError, "FTP remote folder should be defined"
232
233 try:
234 ftpClientObj = ftplib.FTP(server)
235 except ftplib.all_errors:
236 print "FTP server connection fail: %s" %server
237 self.status = 0
238 return 0
239
240 try:
241 ftpClientObj.login(username, password)
242 except ftplib.all_errors:
243 print "FTP username or password are incorrect"
244 self.status = 0
245 return 0
246
247 if remotefolder == None:
248 remotefolder = ftpClientObj.pwd()
249 else:
250 try:
251 ftpClientObj.cwd(remotefolder)
252 except ftplib.all_errors:
253 print "FTP remote folder is invalid: %s" %remotefolder
254 remotefolder = ftpClientObj.pwd()
255
256 self.server = server
257 self.username = username
258 self.password = password
259 self.remotefolder = remotefolder
260 self.__ftpClientObj = ftpClientObj
261 self.status = 1
262
263 return 1
264
265 def close(self):
266 """
267 Close connection to remote server
268 """
269 if not self.status:
270 return 0
271
272 self.__ftpClientObj.close()
273
274 def mkdir(self, remotefolder):
275 """
276 mkdir is used to make a new directory in remote server
277
278 Input:
279 remotefolder - directory name
280
281 Return:
282 0 in error case else 1
283 """
284 if not self.status:
285 return 0
286
287 try:
288 self.__ftpClientObj.mkd(dirname)
289 except ftplib.all_errors:
290 print "Error creating remote folder: %s" %remotefolder
291 return 0
292
293 return 1
294
295 def cd(self, remotefolder):
296 """
297 cd is used to change remote working directory on server
298
299 Input:
300 remotefolder - current working directory
301
302 Affects:
303 self.remotefolder
304
305 Return:
306 0 in case of error else 1
307 """
308 if not self.status:
309 return 0
310
311 if remotefolder == self.remotefolder:
312 return 1
313
314 try:
315 self.__ftpClientObj.cwd(remotefolder)
316 except ftplib.all_errors:
317 print 'Error changing to %s' %remotefolder
318 print 'Trying to create remote folder'
319
320 if not self.mkdir(remotefolder):
321 print 'Remote folder could not be created'
322 return 0
323
324 try:
325 self.__ftpClientObj.cwd(remotefolder)
326 except ftplib.all_errors:
327 return 0
328
329 self.remotefolder = remotefolder
330
331 return 1
332
333 def sendFile(self, fullfilename):
334
335 if not self.status:
336 return 0
337
338 file = open(fullfilename, 'rb')
339
340 filename = os.path.split(fullfilename)[-1]
341
342 command = "STOR %s" %filename
343
344 try:
345 self.__ftpClientObj.storbinary(command, file)
346 except ftplib.all_errors:
347 return 0
348
349 try:
350 self.__ftpClientObj.sendcmd('SITE CHMOD 755 ' + filename)
351 except ftplib.all_errors, e:
352 print e
353
354 file.close()
355
356 return 1
357
358 class SSHClient(Remote):
359
360 __sshClientObj = None
361 __scpClientObj = None
362
363 def __init__(self, server, username, password, remotefolder, period=60):
364 """
365 """
366 Remote.__init__(self, server, username, password, remotefolder, period)
367
368 def open(self, server, username, password, remotefolder, port=22):
369
370 """
371 This method is used to set SSH parameters and establish a connection to a remote server
372
373 Inputs:
374 server - remote server IP Address
375
376 username - remote server Username
377
378 password - remote server password
379
380 remotefolder - remote server current working directory
381
382 Return: void
383
384 Affects:
385 self.status - in case of error or fail connection this parameter is set to 0 else 1
386
387 """
388
389 if server == None:
390 raise ValueError, "SSH server should be defined"
391
392 if username == None:
393 raise ValueError, "SSH username should be defined"
394
395 if password == None:
396 raise ValueError, "SSH password should be defined"
397
398 if remotefolder == None:
399 raise ValueError, "SSH remote folder should be defined"
400
401 try:
402 sshClientObj = paramiko.SSHClient()
403 except:
404 print "SSH server connection fail: %s" %server
405 self.status = 0
406 return 0
407
408 sshClientObj.load_system_host_keys()
409 sshClientObj.set_missing_host_key_policy(paramiko.WarningPolicy())
410
411 try:
412 sshClientObj.connect(server, username=username, password=password, port=port)
413 except :
414 print "SSH username or password are incorrect: %s"
415 self.status = 0
416 return 0
417
418 scpClientObj = scp.SCPClient(sshClientObj.get_transport(), socket_timeout=30)
419
420 if remotefolder == None:
421 remotefolder = self.pwd()
422
423 self.server = server
424 self.username = username
425 self.password = password
426 self.remotefolder = remotefolder
427 self.__sshClientObj = sshClientObj
428 self.__scpClientObj = scpClientObj
429 self.status = 1
430
431 return 1
432
433 def close(self):
434 """
435 Close connection to remote server
436 """
437 if not self.status:
438 return 0
439
440 self.__sshObj.close()
441
442 def mkdir(self, remotefolder):
443 """
444 mkdir is used to make a new directory in remote server
445
446 Input:
447 remotefolder - directory name
448
449 Return:
450 0 in error case else 1
451 """
452 if not self.status:
453 return 0
454
455 stdin, stdout, stderr = self.__sshClientObj.exec_command('mkdir %s' %remotefolder)
456 result = stderr.readlines()[0]
457
458 if len(result) > 1:
459 return 0
460
461 return 1
462
463 def pwd(self):
464
465 if not self.status:
466 return None
467
468 stdin, stdout, stderr = self.__sshClientObj.exec_command('pwd')
469 result = stdout.readlines()[0]
470
471 if len(result) < 1:
472 return None
473
474 return result[:-1]
475
476 def cd(self, remotefolder):
477 """
478 cd is used to change remote working directory on server
479
480 Input:
481 remotefolder - current working directory
482
483 Affects:
484 self.remotefolder
485
486 Return:
487 0 in case of error else 1
488 """
489 if not self.status:
490 return 0
491
492 if remotefolder == self.remotefolder:
493 return 1
494
495 self.remotefolder = remotefolder
496
497 return 1
498
499 def sendFile(self, fullfilename):
500
501 if not self.status:
502 return 0
503
504 try:
505 self.__scpClientObj.put(fullfilename, remote_path=self.remotefolder)
506 except:
507 return 0
508
509 return 1
510
511 class SendToServer(ProcessingUnit):
512
513 def __init__(self):
514
515 ProcessingUnit.__init__(self)
516
517 self.isConfig = False
518 self.clientObj = None
519
520 def setup(self, server, username, password, remotefolder, localfolder, ext='.png', period=60, protocol='ftp'):
521
522 self.clientObj = None
523 self.localfolder = localfolder
524 self.ext = ext
525 self.period = period
526
527 if str.lower(protocol) == 'ftp':
528 self.clientObj = FTPClient(server, username, password, remotefolder, period)
529
530 if str.lower(protocol) == 'ssh':
531 self.clientObj = SSHClient(server, username, password, remotefolder, period)
532
533 if not self.clientObj:
534 raise ValueError, "%s has been chosen as remote access protocol but it is not valid" %protocol
535
536 self.clientObj.start()
537
538 def findFiles(self):
539
540 filenameList = glob.glob1(self.localfolder, '*%s' %self.ext)
541
542 if len(filenameList) < 1:
543 return []
544
545 fullfilenameList = [os.path.join(self.localfolder, thisFile) for thisFile in filenameList]
546
547 return fullfilenameList
548
549 def run(self, **kwargs):
550
551 if not self.isConfig:
552 self.init = time.time()
553 self.setup(**kwargs)
554 self.isConfig = True
555
556 if time.time() - self.init >= self.period:
557 fullfilenameList = self.findFiles()
558 self.clientObj.updateFileList(fullfilenameList)
559 self.init = time.time()
560
561 def close(self):
562 print "[Remote Server] Stopping thread"
563 self.clientObj.stop()
564
565
10 566 class FTP(object):
11 567 """
12 568 Ftp is a public class used to define custom File Transfer Protocol from "ftplib" python module
@@ -298,20 +854,23 class FTP(object):
298 854 self.ftp.close()
299 855
300 856 class SendByFTP(Operation):
857
301 858 def __init__(self):
859
302 860 self.status = 1
303 861 self.counter = 0
304 862
305 863 def error_print(self, ValueError):
864
306 865 print ValueError, 'Error FTP'
307 866 print "don't worry the program is running..."
308 867
309 868 def worker_ftp(self, server, username, password, remotefolder, filenameList):
310 869
311 self.ftpObj = FTP(server, username, password, remotefolder)
870 self.ftpClientObj = FTP(server, username, password, remotefolder)
312 871 for filename in filenameList:
313 self.ftpObj.upload(filename)
314 self.ftpObj.close()
872 self.ftpClientObj.upload(filename)
873 self.ftpClientObj.close()
315 874
316 875 def ftp_thread(self, server, username, password, remotefolder):
317 876 if not(self.status):
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now