# The software in this package is distributed under the GNU General # Public License version 2 (with a special exception described below). # # A copy of GNU General Public License (GPL) is included in this distribution, # in the file COPYING.GPL. # # As a special exception, if other files instantiate templates or use macros # or inline functions from this file, or you compile this file and link it # with other works to produce a work based on this file, this file # does not by itself cause the resulting work to be covered # by the GNU General Public License. # # However the source code for this file must still be made available # in accordance with section (3) of the GNU General Public License. # # This exception does not invalidate any other reasons why a work based # on this file might be covered by the GNU General Public License. # # Copyright (c) 2016-2018 Intra2net AG """ Utility to customize email and other test data. Copyright: Intra2net AG """ import os import re import logging log = logging.getLogger('pyi2ncommon.test_data_sync') def append_email_id_header(data_dir): """ Use to append unique autotest id header to emails. :param str data_dir: directory containing the emails """ email_id = 10000 files = [] for main_dir, _, files in os.walk(data_dir): for i in range(len(files)): file_path = os.path.join(main_dir, files[i]) if i % 100 == 0: log.info("%i done\n", i) if not re.match(r"^\d+\.$", files[i]): continue if os.path.getsize(file_path) == 0: log.warning("Skipping empty file %s", file_path) continue log.info("Adding header to email %s", file_path) id_line = "" message_file = open(file_path, "r") for line in message_file: if re.match("^Message-Id:", line, re.IGNORECASE): id_line = line message_file.close() break readed = open(file_path, "r").read() autotest_id_line = "Autotest-Message-ID: <" + str(email_id + i) + ".63D49232CC@gwt-intranator.m.i2n>\r\n" if id_line == "": final = autotest_id_line + readed else: final = readed.replace(id_line, autotest_id_line) log.info("%s >> %s", id_line, autotest_id_line) open(file_path, "w").write(final) log.info("%i mails replaced.", len(files))