##// END OF EJS Templates
SendToServer:...
Miguel Valdez -
r633:bcf0a5d4812c
parent child
Show More
@@ -11,20 +11,19 try:
11 except:
11 except:
12 print "You should install paramiko if you will use SSH protocol to upload files to a server"
12 print "You should install paramiko if you will use SSH protocol to upload files to a server"
13
13
14 import multiprocessing
15
16 import time
14 import time
17 import threading
18
19
15
20 try:
16 import threading
21 from gevent import sleep
17 Thread = threading.Thread
22 except:
18
23 from time import sleep
19 # try:
20 # from gevent import sleep
21 # except:
22 from time import sleep
24
23
25 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
24 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
26
25
27 class Remote(threading.Thread):
26 class Remote(Thread):
28 """
27 """
29 Remote is a parent class used to define the behaviour of FTP and SSH class. These clases are
28 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.
29 used to upload or download files remotely.
@@ -49,8 +48,7 class Remote(threading.Thread):
49
48
50 def __init__(self, server, username, password, remotefolder, period=60):
49 def __init__(self, server, username, password, remotefolder, period=60):
51
50
52 threading.Thread.__init__(self)
51 Thread.__init__(self)
53 self._stop = threading.Event()
54
52
55 self.setDaemon(True)
53 self.setDaemon(True)
56
54
@@ -66,9 +64,12 class Remote(threading.Thread):
66 if self.open(server, username, password, remotefolder):
64 if self.open(server, username, password, remotefolder):
67 print "[Remote Server] %s server was opened successfully" %server
65 print "[Remote Server] %s server was opened successfully" %server
68
66
67 self.mutex = threading.Lock()
68
69 def stop(self):
69 def stop(self):
70
70
71 self.stopFlag = True
71 self.stopFlag = True
72 self.join(10)
72
73
73 def open(self):
74 def open(self):
74 """
75 """
@@ -158,15 +159,16 class Remote(threading.Thread):
158 if fileList == self.fileList:
159 if fileList == self.fileList:
159 return 0
160 return 0
160
161
161 init = time.time()
162 self.mutex.acquire()
162
163 # init = time.time()
163 while(self.bussy):
164 #
164 sleep(0.1)
165 # while(self.bussy):
165 if time.time() - init > 2*self.period:
166 # sleep(0.1)
166 return 0
167 # if time.time() - init > 2*self.period:
168 # return 0
167
169
168 self.fileList = fileList
170 self.fileList = fileList
169
171 self.mutex.release()
170 return 1
172 return 1
171
173
172 def run(self):
174 def run(self):
@@ -182,19 +184,25 class Remote(threading.Thread):
182
184
183 while True:
185 while True:
184
186
185 sleep(self.period)
187 for i in range(self.period):
188 if self.stopFlag:
189 break
190 sleep(1)
186
191
187 self.bussy = True
192 if self.stopFlag:
193 break
194
195 # self.bussy = True
196 self.mutex.acquire()
188
197
189 for thisFile in self.fileList:
198 for thisFile in self.fileList:
190 sts = self.upload(thisFile, self.remotefolder)
199 sts = self.upload(thisFile, self.remotefolder)
191 if not sts: break
200 if not sts: break
192
201
193 self.bussy = False
202 self.mutex.release()
194
203 # self.bussy = False
195 if not sts: break
196
204
197 if self.stopFlag:
205 if not sts:
198 break
206 break
199
207
200 print "[Remote Server] Thread stopped successfully"
208 print "[Remote Server] Thread stopped successfully"
@@ -581,10 +589,15 class SendToServer(ProcessingUnit):
581 else:
589 else:
582 folderList = self.localfolder
590 folderList = self.localfolder
583
591
592 #Remove duplicate items
593 folderList = list(set(folderList))
594
584 fullfilenameList = []
595 fullfilenameList = []
585
596
586 for thisFolder in folderList:
597 for thisFolder in folderList:
587
598
599 print "[Remote Server]: Searching files on %s" %thisFolder
600
588 filenameList = glob.glob1(thisFolder, '*%s' %self.ext)
601 filenameList = glob.glob1(thisFolder, '*%s' %self.ext)
589
602
590 if len(filenameList) < 1:
603 if len(filenameList) < 1:
@@ -592,6 +605,14 class SendToServer(ProcessingUnit):
592
605
593 for thisFile in filenameList:
606 for thisFile in filenameList:
594 fullfilename = os.path.join(thisFolder, thisFile)
607 fullfilename = os.path.join(thisFolder, thisFile)
608
609 if fullfilename in fullfilenameList:
610 continue
611
612 #Only files modified in the last 30 minutes are considered
613 if os.path.getmtime(fullfilename) < time.time() - 30*60:
614 continue
615
595 fullfilenameList.append(fullfilename)
616 fullfilenameList.append(fullfilename)
596
617
597 return fullfilenameList
618 return fullfilenameList
@@ -929,6 +950,8 class SendByFTP(Operation):
929 if not(self.status):
950 if not(self.status):
930 return
951 return
931
952
953 import multiprocessing
954
932 p = multiprocessing.Process(target=self.worker_ftp, args=(server, username, password, remotefolder, self.filenameList,))
955 p = multiprocessing.Process(target=self.worker_ftp, args=(server, username, password, remotefolder, self.filenameList,))
933 p.start()
956 p.start()
934
957
General Comments 0
You need to be logged in to leave comments. Login now