mirror of
https://github.com/AdaCore/git-hooks.git
synced 2026-02-12 12:43:11 -08:00
This script, which is used as a replacement for sendmail when running the testsuite, has two modes. In the default mode, it currently does nothing more than dumping to standard output the email being asked to send. This commit changes the script to parse the email instead, and dump its contents once parsed, rather than dumping the email as is. The dumping is written so to keep the output the same. The goal is to help print the email body in a human-readable way regardless of its content transfer encoding. This will become particularly useful when we start using base64 content transfer encodings. This is tied to the transition to Python 3.x, where the email support classes automatically chooses base64 transfer encoding in some cases. More generally speaking, we have plans to use base64 more often, as this format ensures that we do not exceed some limitations in email transport (maximum line length, for instance). TN: U530-006 Change-Id: I645126470e8a3b944ff668aba6d30381d418c6b1
56 lines
1.9 KiB
Python
Executable File
56 lines
1.9 KiB
Python
Executable File
#! /usr/bin/env python
|
|
"""A script to be used as a sendmail replacement during testing.
|
|
|
|
Instead of actually sending the email, we dump information about
|
|
the email on standard output.
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
|
|
from email.parser import Parser
|
|
import os
|
|
import sys
|
|
|
|
|
|
def stdout_sendmail():
|
|
email_str = sys.stdin.read()
|
|
e_msg = Parser().parsestr(email_str)
|
|
|
|
if "GIT_HOOKS_MINIMAL_EMAIL_DEBUG_TRACE" in os.environ:
|
|
# This environment variable is set when the testcase
|
|
# doesn't really care about the full contents of
|
|
# the email. Just print a simple trace showing that
|
|
# the email _is_ being sent, and that's it.
|
|
|
|
subject = e_msg["Subject"]
|
|
if "\n" in subject:
|
|
# The email subject got split across multiple lines during
|
|
# email formatting. Restore it to a single-line subject.
|
|
subject = "".join(subject.splitlines())
|
|
sys.stdout.write("DEBUG: Sending email: {subject}...".format(subject=subject))
|
|
|
|
else:
|
|
sys.stdout.write("DEBUG: ")
|
|
for field, value in e_msg.items():
|
|
sys.stdout.write("{field}: {value}\n".format(field=field, value=value))
|
|
payload = e_msg.get_payload(decode=True)
|
|
# The payload is in bytes; decode it before we send it to stdout
|
|
# (which wants str objects).
|
|
#
|
|
# Only do this decoding when Python >= 3.x, as we are getting
|
|
# some errors with Python 2.x which are not worth investigating
|
|
# at this stage (we are moving away from 2.x support).
|
|
if sys.version_info[0] >= 3:
|
|
# The payload is in bytes, and needs to be decoded
|
|
# in order to be
|
|
charset = e_msg.get_content_charset()
|
|
payload = payload.decode(charset)
|
|
sys.stdout.write("\n")
|
|
sys.stdout.write(payload)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
stdout_sendmail()
|