587
|
1 |
#============================================================================
|
|
2 |
#Name : ctc.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
"""ctc implements an uploader for MON.SYM file"""
|
|
20 |
|
|
21 |
import os
|
|
22 |
import ftplib
|
|
23 |
|
|
24 |
|
|
25 |
class MonSymFTPUploader:
|
|
26 |
""" This class implement an uploader for MON.SYM file. """
|
|
27 |
|
|
28 |
def __init__(self, server, paths, diamondsid):
|
|
29 |
""" Upload the files discovered under the paths,
|
|
30 |
and upload them under the FTP server.
|
|
31 |
"""
|
|
32 |
self.server = server
|
|
33 |
self.paths = paths
|
|
34 |
self.diamondsid = diamondsid
|
|
35 |
self.ftp = None
|
|
36 |
|
|
37 |
def upload(self):
|
628
|
38 |
""" Proceed to the upload. """
|
587
|
39 |
self._open()
|
|
40 |
monsyms = []
|
|
41 |
i = 1
|
|
42 |
for p_path in self.paths:
|
|
43 |
if os.path.exists(p_path) and os.path.isfile(p_path):
|
|
44 |
# ftp://1.2.3.4/ctc_helium/[diamonds_id]/mon_syms/2/mon.sym
|
628
|
45 |
outputdir = "%s/mon_syms/%d" % (self.diamondsid , i)
|
587
|
46 |
output = outputdir + "/MON.SYM"
|
|
47 |
self._ftpmkdirs(outputdir)
|
|
48 |
print "Copying %s under %s" % (p_path, output)
|
|
49 |
self._send(p_path, output)
|
|
50 |
monsyms.append(output)
|
|
51 |
i += 1
|
|
52 |
self._close()
|
|
53 |
return monsyms
|
|
54 |
|
|
55 |
def _open(self):
|
|
56 |
"""open"""
|
|
57 |
self.ftp = ftplib.FTP(self.server, 'anonymous', '')
|
|
58 |
|
|
59 |
def _close(self):
|
|
60 |
"""close"""
|
|
61 |
self.ftp.quit()
|
|
62 |
|
628
|
63 |
def _ftpmkdirs(self, dir_):
|
587
|
64 |
"""ftp make directory"""
|
|
65 |
pwd = self.ftp.pwd()
|
628
|
66 |
for d_dir in dir_.split('/'):
|
|
67 |
if len(d_dir) != 0:
|
587
|
68 |
try:
|
|
69 |
print "Creating %s under %s" % (d_dir, self.ftp.pwd())
|
|
70 |
self.ftp.mkd(d_dir)
|
|
71 |
except ftplib.error_perm: #capture the exception but not display it.
|
|
72 |
pass
|
|
73 |
self.ftp.cwd(d_dir)
|
|
74 |
self.ftp.cwd(pwd)
|
|
75 |
|
|
76 |
def _send(self, src, dst):
|
|
77 |
"""send """
|
628
|
78 |
try:
|
|
79 |
self.ftp.storbinary("STOR " + dst, open(src, "rb"), 1024)
|
|
80 |
except ftplib.error_perm, e:
|
|
81 |
print 'File already uploaded. ' + str(e)
|
|
82 |
|
|
83 |
|