| 1 | # The software in this package is distributed under the GNU General |
| 2 | # Public License version 2 (with a special exception described below). |
| 3 | # |
| 4 | # A copy of GNU General Public License (GPL) is included in this distribution, |
| 5 | # in the file COPYING.GPL. |
| 6 | # |
| 7 | # As a special exception, if other files instantiate templates or use macros |
| 8 | # or inline functions from this file, or you compile this file and link it |
| 9 | # with other works to produce a work based on this file, this file |
| 10 | # does not by itself cause the resulting work to be covered |
| 11 | # by the GNU General Public License. |
| 12 | # |
| 13 | # However the source code for this file must still be made available |
| 14 | # in accordance with section (3) of the GNU General Public License. |
| 15 | # |
| 16 | # This exception does not invalidate any other reasons why a work based |
| 17 | # on this file might be covered by the GNU General Public License. |
| 18 | # |
| 19 | # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com> |
| 20 | |
| 21 | """ |
| 22 | Utility to customize email and other test data. |
| 23 | |
| 24 | Copyright: Intra2net AG |
| 25 | """ |
| 26 | |
| 27 | import os |
| 28 | import re |
| 29 | import logging |
| 30 | log = logging.getLogger('pyi2ncommon.test_data_sync') |
| 31 | |
| 32 | |
| 33 | def append_email_id_header(data_dir): |
| 34 | """ |
| 35 | Use to append unique autotest id header to emails. |
| 36 | |
| 37 | :param str data_dir: directory containing the emails |
| 38 | """ |
| 39 | email_id = 10000 |
| 40 | files = [] |
| 41 | for main_dir, _, files in os.walk(data_dir): |
| 42 | for i in range(len(files)): |
| 43 | file_path = os.path.join(main_dir, files[i]) |
| 44 | if i % 100 == 0: |
| 45 | log.info("%i done\n", i) |
| 46 | |
| 47 | if not re.match(r"^\d+\.$", files[i]): |
| 48 | continue |
| 49 | if os.path.getsize(file_path) == 0: |
| 50 | log.warning("Skipping empty file %s", file_path) |
| 51 | continue |
| 52 | |
| 53 | log.info("Adding header to email %s", file_path) |
| 54 | id_line = "" |
| 55 | message_file = open(file_path, "r") |
| 56 | for line in message_file: |
| 57 | if re.match("^Message-Id:", line, re.IGNORECASE): |
| 58 | id_line = line |
| 59 | message_file.close() |
| 60 | break |
| 61 | |
| 62 | readed = open(file_path, "r").read() |
| 63 | autotest_id_line = "Autotest-Message-ID: <" + str(email_id + i) + ".63D49232CC@gwt-intranator.m.i2n>\r\n" |
| 64 | if id_line == "": |
| 65 | final = autotest_id_line + readed |
| 66 | else: |
| 67 | final = readed.replace(id_line, autotest_id_line) |
| 68 | log.info("%s >> %s", id_line, autotest_id_line) |
| 69 | open(file_path, "w").write(final) |
| 70 | log.info("%i mails replaced.", len(files)) |