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
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()




Comment