Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #REFERENCES#
- __author__ = "Corey M. Farmer - 2011"
- __version__ = "1.1"
- '''DESCRIPTION: this program is designed to:
- 1) run as a system process daemon (background process)
- 2) read given directory for file feeds from ITC (managed by Robie Bennett)
- 3) parse and write the file contents to a mysql datatable
- 4) delete file after job completion
- Exception Handling:
- program errors will be handled via commonfunctionlibrary.buildEmailGen() method
- and sent to the system adminMailingList defined in given module.
- errors encountered from ITC files will prompt a file rename to 'invError_' + [filename]
- and excluded from future directory iterator instances.'''
- #END REFERENCES#
- import sys, os
- sys.path.append( '/home/coreyf/applications/common' )
- import commonfunctionlibrary as comfunclib
- def checkIfRunning():
- '''This method verifies that an instance of this program is not already running.
- If count of process id`s is greater than 1 close program.'''
- if len( os.popen( "ps -aef | grep -i 'itc_d.py' | grep -v 'grep' | awk '{ print $3 }'" ).read().strip().split( '\n' ) ) > 1:
- raise SystemExit(0)
- def createDaemon():
- '''This method sends the itc_d.py program to the command shell background process list;
- freeing up the shell CLI for user input again while the program continues to execute.'''
- try:
- pid = os.fork()
- except OSError, e:
- raise Exception, "%s [%d]" % (e.strerror, e.errno)
- if ( pid == 0 ):
- os.setsid()
- try:
- pid = os.fork()
- except OSError, e:
- raise Exception, "%s [%d]" % (e.strerror, e.errno)
- if ( pid != 0 ): os._exit(0)
- else:
- os._exit(0)
- def dirIterate():
- '''iterate over "path" variable location searching for files not including
- files with "invError_" in filename.'''
- for file in os.listdir( path ):
- if 'invError_' in file: continue
- parseContents( file )
- def parseContents( file ):
- '''method to parse the contents of individual files, write the data to a mysql table,
- and delete the file after successfully completing previous operations.'''
- global aErrorHandler
- errLevel = 0
- try:
- fopen = open( path + file )
- try:
- contents = fopen.read().split('|')
- if len( contents ) == 8:
- for data in contents:
- print data
- else:
- errLevel = 2.1
- aErrorHandler.append( {'error': 'empty or missing fields in exploded file content!-> expected 8 fields but found ' + str( len( contents ) ), 'file': file, 'caller': 'parseContents::if errLevel:' + str( errLevel ) } )
- finally:
- fopen.close()
- except IOError, e:
- errLevel = 1.1
- aErrorHandler.append( {'error': e.strerror, 'file': file, 'caller': 'parseContents::try errLevel:' + str( errLevel ) } )
- pfile = path + file
- if os.path.exists( pfile ):
- os.rename( pfile, path + 'invError_' + file )
- if errLevel > 0:
- try:
- pass
- except OSError, e:
- errLevel = 1.2
- aErrorHandler.append( {'error': e.strerror, 'file': file, 'caller': 'parseContents::try errLevel:' + str( errLevel ) } )
- finally: pass
- def sendErrorReport():
- '''method designed to handle errors/exceptions encountered within this program
- in a graceful way by notifying the adminMailingList and not raising the system level exception
- (keeping the program from exiting with errors).'''
- aAttach = []
- body = '<strong>Error(s) have occurred within the file (' + __file__ + ' )!</strong><br><br>'
- for index, error in enumerate( aErrorHandler ):
- if len( error['file'] ) > 0: aAttach.append( path + error['file'] )
- body += ( str( index+1 ) + ') <strong>Function Name:</strong> ' + error['caller'] +
- '<br><strong>File Causing Error:</strong> ' + path + error['file'] +
- '<br><strong>Error:</strong> ' + error['error'] + '<br><br>' )
- if len( body ) > 0:
- comfunclib.buildEmailGen( 'USLONSWEB003::pyERROR>>' + __file__, 'adminMailingList', body, aAttach )
- #begin execution code
- path = '/home/coreyf/uslonsweb003/www/systems/txt_test/'
- def main():
- aErrorHandler = []
- #SETUP ENVIRONMENT#
- checkIfRunning() #ensure program is not running as another process already
- #createDaemon() #set program as shell background process (daemonize)
- ###################
- dirIterate()
- if len( aErrorHandler ) > 0:
- sendErrorReport()
- raise SystemExit(0)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement