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