77# Version history
88# 2012xxxx SF first version
99# 20120628 AX removed testing for every line, added timing code,
10- # 20120629 AX added loop over all arguments, exception handling, restructured code
10+ # 20120629 AX added loop over all arguments, exception handling, restructured code, moved processed files to archive or error folder
1111#
1212# test:
1313# cd /DATA
1414# python scripts/mlab/mlab_mysql_import2.py mlab/clean/glasnost/20090128T000000Z-batch-batch-glasnost-0002.tgz.csv
1515#
16- # ToDO: loop over all arguments in sys.argv[0]
17- # deduplication toevoegen (put in hash, test on hash, clear hash for each file, but keep last entry
18- # move files naar done directory
19- # move error files naar error directory
20- #
16+ # ToDO: v loop over all arguments in sys.argv[0]
17+ # v deduplication toevoegen (put in hash, test on hash, clear hash for each file, but keep last entry
18+ # v move files naar archive directory
19+ # v move error files naar error directory
20+ # v log process and errors
2121
2222import sys
2323import re
6262#################################################################
6363
6464def usage ():
65- print "Usage: mlab_mysql_import.py mlab_file.csv"
66- print "Recursive import can be realised by running:"
67- print "find . -iname '*.tgz.csv' -exec ./mlab_mysql_import.py {} \;"
65+ print "Usage: mlab_mysql_import3.py mlab_file1.csv [mlab_files.csv ...]"
6866 sys .exit (1 )
6967
7068def extract_destination (filename ):
@@ -145,6 +143,14 @@ def get_file_id(cur, filename):
145143 return get_file_id (cur , filename )
146144 return id [0 ]
147145
146+ def dedup (file_id , table , test_datetime , destination , source_ip ):
147+ key = str (file_id ) + table + str (test_datetime ) + destination + source_ip
148+ if key in deduplookup :
149+ return False
150+ else :
151+ deduplookup [key ] = True
152+ return True
153+
148154# returns True on error, False on correct processing
149155def process_file (f , filename ):
150156 start_time = datetime .now ()
@@ -185,7 +191,9 @@ def process_file(f, filename):
185191 # this file has already been read: ABORT WITH ERROR
186192 raise Exception ('File entry already exist in db; the file has already been read: ' + filename )
187193 filetest = False
188- blunt_insert_dbentry (cur , file_id , db_tables [test ], test_datetime , destination , source_ip )
194+ # test if we have already done it in this or last filetest
195+ if (dedup (file_id , db_tables [test ], test_datetime , destination , source_ip )):
196+ blunt_insert_dbentry (cur , file_id , db_tables [test ], test_datetime , destination , source_ip )
189197 end_time = datetime .now ()
190198 print 'File done in ' + str (end_time - start_time )
191199 failure = False
@@ -231,8 +239,6 @@ def move_archive(pathname):
231239 f .write (pathname + '\n ' )
232240
233241
234-
235-
236242#################################################################
237243# #
238244# start of initialisation #
@@ -251,6 +257,11 @@ def move_archive(pathname):
251257f .write ("\n New batchjob on " + str (datetime .now ()))
252258f .close
253259
260+ # deduplookup is a hash we use for de-duplication of input lines
261+ # maybe it is necessary to purge parts of it during the duration of the import
262+ # but then we have to carefully monitor tests that appear in multiple files
263+ # OR store the last test in a separate global (dirty? yeah, I know)
264+ deduplookup = {}
254265
255266#################################################################
256267# #
0 commit comments