def is_content_same(imap_server, email_id): """ Check if the content is the same on the disk and the mail. """ config = ConfigParser() config.read(["../project_config"]) attachments = imaputils.get_attachment(imap_server, email_id) for filename, content in attachments.items(): if re.search("run-[0-9]+-benchmark.log", filename) == None: continue filelocation_archive = os.path.join( os.path.dirname("__FILE__"), "../", config.get("locations", "archives"), filename ) filelocation_queue = os.path.join( os.path.dirname("__FILE__"), "../", config.get("locations", "queue"), filename ) md5 = hashlib.md5() md5.update(content) md5_hash = md5.hexdigest() filelocation_archive = filelocation_archive + "." + md5_hash filelocation_queue = filelocation_queue + "." + md5_hash # if util.check_if_file_exists_on_disk(filelocation_archive): if util.md5_for_file(open(filelocation_archive, "r")) == md5_hash: return True else: os.remove(filelocation_archive) return False elif util.check_if_file_exists_on_disk(filelocation_queue): if util.md5_for_file(open(filelocation_queue, "r")) == md5_hash: return True else: os.remove(filelocation_queue) return False else: return False
def is_content_same(imap_server, email_id): """ Check if the content is the same on the disk and the mail. """ config = ConfigParser() config.read(['../config']) attachments = imaputils.get_attachment(imap_server, email_id) for filename, content in attachments.items(): if re.search("run-[0-9]+-benchmark.log", filename) == None: continue filelocation_archive = os.path.join( os.path.dirname('__FILE__'), '../', config.get("locations", "archives"), filename) filelocation_queue = os.path.join(os.path.dirname('__FILE__'), '../', config.get("locations", "queue"), filename) md5 = hashlib.md5() md5.update(content) md5_hash = md5.hexdigest() filelocation_archive = filelocation_archive + '.' + md5_hash filelocation_queue = filelocation_queue + '.' + md5_hash # if util.check_if_file_exists_on_disk(filelocation_archive): if util.md5_for_file(open(filelocation_archive, 'r')) == md5_hash: return True else: os.remove(filelocation_archive) return False elif util.check_if_file_exists_on_disk(filelocation_queue): if util.md5_for_file(open(filelocation_queue, 'r')) == md5_hash: return True else: os.remove(filelocation_queue) return False else: return False
def main(): """ Main """ logger = bp_logger("GET_EMAIL_ATTACHMENTS") config = ConfigParser() config.read(["../project_config"]) imap_server = imaputils.get_imapserv_conn() connection = dbutils.get_connection() # This lists out the list of all the msg, emails = imap_server.search(None, "(UNSEEN)") # print emails # Go through each of the ids given out by IMAP for email_id in emails[0].split(): attachments = imaputils.get_attachment(imap_server, email_id) for filename, content in attachments.items(): # Filename has to match the required pattern. if re.search("run-[0-9]+-benchmark.log", filename) == None: logger.debug( "The filename {:s} did not match the needed pattern. IMAP id : {:d}".format(filename, int(email_id)) ) continue filelocation = os.path.join(config.get("locations", "queue"), filename) md5 = hashlib.md5() md5.update(content) md5_hash = md5.hexdigest() filelocation = filelocation + "." + md5_hash # if util.check_if_file_exists_on_disk(filelocation): logger.debug("The file {:s} exists on the disk.".format(filelocation)) continue else: file_queue = open(filelocation, "w") file_queue.write(content) logger.info("The file {:s} has been written to the disk.".format(filelocation)) file_queue.close() # Add code to write it to the db query = """ INSERT INTO email_logs (`imap_id`, `md5`, `time`) VALUES ({:d}, \"{:s}\", NOW()) """.format( int(email_id), md5_hash ) hw_id = dbutils.db_insert(connection, query) if hw_id == None: logger.error("The query \n {:s} has not been inserted.".format(query))
def main(): """ Main """ logger = bp_logger('GET_EMAIL_ATTACHMENTS') config = ConfigParser() config.read(['../project_config']) imap_server = imaputils.get_imapserv_conn() connection = dbutils.get_connection() # This lists out the list of all the msg, emails = imap_server.search(None, "(UNSEEN)") # print emails # Go through each of the ids given out by IMAP for email_id in emails[0].split(): attachments = imaputils.get_attachment(imap_server, email_id) for filename, content in attachments.items(): # Filename has to match the required pattern. if re.search("run-[0-9]+-benchmark.log", filename) == None: logger.debug( "The filename {:s} did not match the needed pattern. IMAP id : {:d}" .format(filename, int(email_id))) continue filelocation = os.path.join(config.get("locations", "queue"), filename) md5 = hashlib.md5() md5.update(content) md5_hash = md5.hexdigest() filelocation = filelocation + '.' + md5_hash # if util.check_if_file_exists_on_disk(filelocation): logger.debug( "The file {:s} exists on the disk.".format(filelocation)) continue else: file_queue = open(filelocation, 'w') file_queue.write(content) logger.info( "The file {:s} has been written to the disk.".format( filelocation)) file_queue.close() # Add code to write it to the db query = """ INSERT INTO email_logs (`imap_id`, `md5`, `time`) VALUES ({:d}, \"{:s}\", NOW()) """.format(int(email_id), md5_hash) hw_id = dbutils.db_insert(connection, query) if hw_id == None: logger.error( "The query \n {:s} has not been inserted.".format(query))