• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

python problems

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    python problems

    arrrgghhhh I'm writing a python script that effectively logs onto a Helpdesk System, retrieves a file, logs onto the ftp server, creates the directories and copies it across or uploads the file from the FTP server to the helpdesk

    i'm getting an error in teardown() of
    "global name 'ftp' is not defined "
    but I can't for the life of me work out why it isn't picking up the global ftp variable..

    any help much appreciated

    Code:
    from ConfigParser import ConfigParser
    from optparse import OptionParser
    import logging
    import sys
    import ctypes
    import pickle
    import os
    import errno
    
    from pyars import cars, erars
    from ftplib import FTP
     
    global ars
    global ftp
    
    def setup(server, user, password, verbose = False):
        global ars
        ars = erars.erARS()
        if verbose:
            ars.logger.setLevel(logging.DEBUG)
        else:
            ars.logger.setLevel(logging.INFO) 
        c=ars.Login(server, user, password,
    #                charSet='Windows-1252'
    #                charSet='UTF-8'
                    charSet = 'iso8859-1'
                    )
        if ars.errnr > 1:
            ars.logger.error('Could not login to server!')
            sys.exit(2)
    
    def ftpSetup(server, user, password, path):
        global ftp
        
        ### open FTP connection
        ftp = FTP(server)
        try:
            ftp.login(user, password)
            ftp.cwd(path)
        except ftplib.error_perm:
            print ('Error logging in to ftp server %s with user %s' % (server,
                                                                      user))
    
    def teardown():
        global ars 
        global ftp
        
        ftp.quit()
        ars.Logoff()
    
    def changeToOrCreateFtpDir(directory):
        '''try to change to a directory; if it fails, create the directory and
    change to it.'''
        global ftp
        
        try:
            ftp.cwd(directory)
        except ftplib.error_perm:
            ftp.mkd(directory)
            ftp.cwd(directory)
    
    def transferAttachToResolve(worklogEntry,
                         location):
        '''transferAttachToResolve will receive the path information on the ftp server,
    retrieve the file from there and add it as an attachment to the worklog.
    Input:
    '''
        path = '/var/tmp/resolve'
        (location, filename) = os.path.split(location)
        try:
            ftp.cwd(location)
        except ftplib.error_perm:
            print ('Error changing to ftp directory %s' % location)
            return
        
        filename = '%s/%s' % (path, filename)
        fileHandle = open(filename, 'wb')
        ftp.retrbinary("RETR " + filename, fileHandle.write)
        fileHandle.close()
        
        result = ars.SetEntry('RESOLVE:CASE:WRKLG:Worklog', entryId, 
                              {800600012l: (filename, None, None, filename)})
        if ars.errnr > 1:
            print ('Error adding attachment %s to worklog %s' % (filename,
                                                                 entryId))
    
    def transferAttachToMotto(worklogEntry,
                           fileName,
                           product,
                           caseNumber,
                           prontoID = ''):
        '''transferAttachToMotto takes the attachment from fieldId 800600012
    from the worklog, stores it under the filename in /var/tmp and transfers
    it to the Motto ftp server.
    If we receive a prontoID, it is not an INFOADD, but an INFORESPONSE message. The
    only difference is the ftp path where we store the attachment.
    Input: worklogEntry: entryId of worklog
           fileName: name for temporary file
           product: RESOLVE product code
           caseNumber: caseNumber of the worklog
           prontoID (optional) if handed over is used to generate the ftp path
    Output: /'''
    
        global ars
        global ftp
        
        fileName = "/var/tmp/resolve/%s" % fileName
        ### prepare a structure for the attachment
        loc = cars.ARLocStruct()
        loc.locType = cars.AR_LOC_FILENAME
        loc.u.filename = fileName
          
        att = ars.GetEntryBLOB ('RESOLVE:CASE:WRKLG:Worklog', worklogEntry, 
                                800600012, 
                                loc)
        if ars.errnr > 1:
            print ars.statusText()
            return
    
        ### open the file to read it as a file like object
        fileHandle = open(fileName, "r")
        
        ### create the required folder structure
        changeToOrCreateFtpDir(product)
        changeToOrCreateFtpDir(caseNumber)
    
        if prontoId != '':
            changeToOrCreateFtpDir(prontoId)
            changeToOrCreateFtpDir('answer')    
                    
        ### save file like object to the ftp path
        ftp.storbinary("STOR " + fileName, fileHandle)
        fileHandle.close()
    
    def main():
        
        usage = '''This tool handles the ftp transfer between RESOLVE and Motto.
    
    It supports the upload (from Motto to RESOLVE, please use -l and -w) as well as
    the download (from RESOLVE to Motto, indicated by not using -l!).
     
    usage: %prog [options]''' 
    
        parser = OptionParser()
        parser.add_option("-w", "--worklog",  default='',
                              action="store", type="string", dest="worklog", help="worklog ID")
        parser.add_option("-c", "--case", default='',
                              action="store", type="string", dest="case", help="case number")
        parser.add_option("-m", "--productCode", default='',
                              action="store", type="string", dest="productCode", help="PDM Product Code")
        parser.add_option("-p", "--path", default='',
                              action="store", type="string", dest="path", help="complete FTP filepath for output")
        parser.add_option("-o", "--outputFile", default='attachment.txt',
                              action="store", type="string", dest="outputFile", help="filename for output")
        parser.add_option("-i", "--prontoID", default='',
                          action="store", type="string", dest="prontoID", help="Pronto Action ID") 
        parser.add_option("-l", "--location", default='',
                          action="store", type="string", dest="location", help="ftppath of attachment to be retrieved")
        parser.add_option("-v", "--verbose", action="store_true", default=False,
                              dest="verbose", help="verbose debugging output")
        
        (options, args) = parser.parse_args()
        
        config = ConfigParser ()
        config.read ("/global/resolve/programs/conf/api.conf")
        resolveServer = config.get ('ARS', 'ServerName')
        resolvePort = config.get ('ARS', 'ARServerPort')
        resolveUser = config.get ('ARS', 'UserName')
        resolvePwd = config.get ('ARS', 'Password')
        
        ftpServer = config.get ('Motto', 'ftpServer')
        ftpUser = config.get ('Motto', 'ftpUser')
        ftpPwd = config.get ('Motto', 'ftpPwd')
        ftpRootDir = config.get ('Motto', 'ftpRootDir')
        
        try:
            setup('%s:%s' % (resolveServer, resolvePort),
                  resolveUser, resolvePwd, options.verbose)
    
            ftpSetup(ftpServer, ftpUser, ftpPwd, ftpRootDir)
            if options.location != '':
                transferAttachToResolve(options.worklog,
                                 options.location)
            else:
                transferAttachToMotto(options.worklog,
                                   options.outputFile,
                                   options.productCode,
                                   options.case,
                                   options.prontoID)
        finally:
            teardown()
    
    if __name__ == "__main__":
        main()
    Last edited by chef; 2 October 2009, 12:03.
    The proud owner of 125 Xeno Geek Points

    #2
    Python

    Looks like a cross between VB and Pascal.

    Apart from that, I know nothing

    Comment


      #3
      more info here on python
      The proud owner of 125 Xeno Geek Points

      Comment


        #4
        Is your ars populated?

        Comment


          #5
          You should switch to using Capistrano

          Comment


            #6
            I am only just getting my head round Python, but here are my thoughts:

            a) what is Python error reporting like? Does it suffer from the same problem as other languages where the error often refers to a previous / subsequent line, or get a daft error because an error earlier was not handled by an exception handler?

            b) When I copy 'n' paste that from your posting into Notepad, some of the indentation looks suspect. Double check your spaces & tabs.

            c) Blank lines. When I run the code through the debugger step by step, it is stopping at lines 91 and 142 when I would not have expected it to. Do the blank lines within the procedurs/functions matter?

            d) Can you rename the 'ftp' variable, even if it is to myftp or theftp. It is such a common term that it might be in use for something else somewhere else.

            e) Change teardown to:
            Code:
            def teardown():
                global ars 
                global ftp
                
                print ('teardown 1')
                ftp.quit()
                print ('teardown 2')
                ars.Logoff()
                print ('teardown end')
            just to be absolutely sure of where it is failing.

            f) Don't bother closing the FTP connection! I.e., comment out the quit() and see what happens. (It'll time out after a few minutes anyway (probably!!)).

            g) Can you trap an error that may occur to the ftp quit()?

            h) Can you put something in immediately before the ftp.quit() that would test whether the ftp connection is still open? (A gut feeling...)

            i) Instead of having teardown() called under the 'finally', put the following as inline code:
            Code:
                ftp.quit()
                ars.Logoff()
            j) Out of my depth here, but , is that 'try:' & 'finally:' necessary?

            Is that of any use?
            If you read the best 3 books in any subject, you'll be in the top 5% of experts in the world.

            Comment


              #7
              Originally posted by DimPrawn View Post
              Looks like a cross between VB and Pascal.

              Comment


                #8
                Originally posted by chef View Post
                i'm getting an error in teardown() of
                "global name 'ftp' is not defined "
                but I can't for the life of me work out why it isn't picking up the global ftp variable..
                Adding import ftplib seemed to fix things for me - although I'd commented out much of your code, I was still using the FTP bits to open and then close a connection to localhost. Give it a try

                Comment


                  #9
                  Originally posted by NickFitz View Post
                  Adding import ftplib seemed to fix things for me - although I'd commented out much of your code, I was still using the FTP bits to open and then close a connection to localhost. Give it a try


                  Nick your a genius, I knew it was some idiotic/newbie/rookie mistake..

                  thank you
                  The proud owner of 125 Xeno Geek Points

                  Comment


                    #10
                    Chef - I'm glad your problem got resolved (that NickFitz chappie is a bit good) but I'm slightly surprised by the lack of 'oo er missus' responses
                    +50 Xeno Geek Points
                    Come back Toolpusher, scotspine, Voodooflux. Pogle
                    As for the rest of you - DILLIGAF

                    Purveyor of fine quality smut since 2005

                    CUK Olympic University Challenge Champions 2010/2012

                    Comment

                    Working...
                    X