buildframework/helium/sf/python/pythoncore/lib/ctc.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
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):
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    38
        """ Proceed to the upload. """
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
        self._open()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
        monsyms = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
        i = 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
        for p_path in self.paths:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
            if os.path.exists(p_path) and os.path.isfile(p_path):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
                # ftp://1.2.3.4/ctc_helium/[diamonds_id]/mon_syms/2/mon.sym
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    45
                outputdir = "%s/mon_syms/%d" % (self.diamondsid , i)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
                output = outputdir + "/MON.SYM"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
                self._ftpmkdirs(outputdir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
                print "Copying %s under %s" % (p_path, output)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
                self._send(p_path, output)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
                monsyms.append(output)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
                i += 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
        self._close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
        return monsyms
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    def _open(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        """open"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
        self.ftp = ftplib.FTP(self.server, 'anonymous', '')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    def _close(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        """close"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        self.ftp.quit()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    63
    def _ftpmkdirs(self, dir_):
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
        """ftp make directory"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
        pwd = self.ftp.pwd()
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    66
        for d_dir in dir_.split('/'):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    67
            if len(d_dir) != 0:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
                try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
                    print "Creating %s under %s" % (d_dir, self.ftp.pwd())
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
                    self.ftp.mkd(d_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
                except ftplib.error_perm:      #capture the exception but not display it.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
                    pass
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
                self.ftp.cwd(d_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        self.ftp.cwd(pwd)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    def _send(self, src, dst):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        """send """
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    78
        try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    79
            self.ftp.storbinary("STOR " + dst, open(src, "rb"), 1024)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    80
        except ftplib.error_perm, e:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    81
            print 'File already uploaded. ' + str(e)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    82
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    83