You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2025/02/13 20:07:34 UTC
(allura) branch master updated (fd1c56ed8 -> 1c8853f92)
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git
from fd1c56ed8 [#8575] upgrade pymongo and pymongocrypt
new ce93579b3 [#8574] adding aiosmtpd to replace smtpd package
new c0c577631 [#8574] updated test to make use of the BetterDebuggingServer class
new 1c8853f92 [#8574] removed the async function start_server in favor a more simple approach to keep the server running
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
Allura/allura/command/smtp_server.py | 52 ++++++++++++++++++----------
Allura/allura/tests/test_mail_util.py | 39 ++++++++++++++++-----
Allura/docs/getting_started/installation.rst | 2 +-
Allura/test.ini | 3 ++
AlluraTest/alluratest/smtp_debug.py | 23 +++++++++---
docker-compose.yml | 2 +-
6 files changed, 86 insertions(+), 35 deletions(-)
(allura) 03/03: [#8574] removed the async function start_server in favor a more simple approach to keep the server running
Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git
commit 1c8853f9255dac6f83f901fa1960a56f734fc057
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Tue Feb 4 15:51:02 2025 -0700
[#8574] removed the async function start_server in favor a more simple approach to keep the server running
---
Allura/allura/command/smtp_server.py | 27 ++++++++++-----------------
Allura/allura/tests/test_mail_util.py | 33 +++++++++++++++++++--------------
Allura/test.ini | 3 +++
AlluraTest/alluratest/smtp_debug.py | 10 +---------
4 files changed, 33 insertions(+), 40 deletions(-)
diff --git a/Allura/allura/command/smtp_server.py b/Allura/allura/command/smtp_server.py
index 3a1b07e42..1ce8e2cf0 100644
--- a/Allura/allura/command/smtp_server.py
+++ b/Allura/allura/command/smtp_server.py
@@ -25,18 +25,10 @@
from allura.lib import helpers as h
from paste.deploy.converters import asint
from aiosmtpd.controller import Controller
-import asyncio
from tg.wsgiapp import RequestLocals
+import time
-async def start_server(loop):
- handler = MailServer()
- hostname = tg.config.get('forgemail.host', '0.0.0.0')
- port = asint(tg.config.get('forgemail.port', 8825))
- controller = Controller(handler, hostname=hostname, port=port)
- controller.start()
-
-
class SMTPServerCommand(base.Command):
min_args = 1
max_args = 1
@@ -50,14 +42,14 @@ class SMTPServerCommand(base.Command):
def command(self):
faulthandler.enable()
self.basic_setup()
- loop = asyncio.get_event_loop()
- task = loop.create_task(start_server(loop=loop))
- try:
- loop.run_forever()
- except KeyboardInterrupt:
- task.cancel()
- finally:
- loop.close()
+ handler = MailServer()
+ hostname = tg.config.get('forgemail.host', '0.0.0.0')
+ port = asint(tg.config.get('forgemail.port', 8825))
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
+ while True:
+ time.sleep(100000)
+
class MailServer:
async def handle_DATA(self, server, session, envelope):
@@ -68,6 +60,7 @@ async def handle_DATA(self, server, session, envelope):
data = envelope.content
base.log.info('Msg Received from %s for %s', mailfrom, rcpttos)
base.log.info(' (%d bytes)', len(data))
+ # pass the app_globals into the request context again because it's in a different thread
tgl = RequestLocals()
tgl.tmpl_context = EmptyClass()
tgl.app_globals = tg.app_globals
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index 19001b8d5..38418ddf3 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -24,7 +24,6 @@
from tg import config as tg_config
from smtplib import SMTP as SMTPClient
from alluratest.controller import setup_basic_test, setup_global_objects
-from alluratest.smtp_debug import BetterDebuggingServer
from allura.lib.utils import ConfigProxy
from allura.app import Application
from allura.lib.mail_util import (
@@ -39,7 +38,8 @@
from allura.lib.exceptions import AddressException
from allura.tests import decorators as td
from paste.deploy.converters import asint
-from aiosmtpd.handlers import Debugging
+from allura.command.smtp_server import MailServer
+from aiosmtpd.controller import Controller
config = ConfigProxy(
common_suffix='forgemail.domain',
@@ -335,24 +335,29 @@ def setup_method(self, method):
@mock.patch('allura.command.base.log', autospec=True)
def test(self, log):
- mailserver = BetterDebuggingServer()
- controller, handler = mailserver.start_server()
hostname = tg_config.get('forgemail.host', '0.0.0.0')
- port = asint(tg_config.get('forgemail.port', 8825))
+ port = asint(tg_config.get('forgemail.port', 8827))
+ handler = MailServer()
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
+
with SMTPClient(hostname, port, timeout=0.1) as client:
- resp = client.docmd("HELP", "HELO")
code, msg = client.ehlo("example.com")
assert code == 250
- client.sendmail('aperson@example.com', ['bperson@example.com'],"""
- From: Anne Person <an...@example.com>
- To: Bart Person <ba...@example.com>
+ mailfrom = client.docmd("MAIL FROM: <fr...@example.com>")
+ assert mailfrom == (250, b'OK')
+ rcpt = client.docmd("RCPT TO: <to...@example.com>")
+ assert rcpt == (250, b'OK')
+ data = client.docmd("DATA")
+ assert data == (354, b'End data with <CR><LF>.<CR><LF>')
+
+ with SMTPClient(hostname, port, timeout=0.1) as client:
+ client.sendmail('from@example.com', ['to@example.com'],"""
+ From: From Person <fr...@example.com>
+ To: To Person <to...@example.com>
Subject: A test
Hi Bart, this is Anne.
""")
- assert isinstance(handler, Debugging)
- text = handler.stream.getvalue()
- assert 'From: Anne Person <an...@example.com>' in text
- assert ' To: Bart Person <ba...@example.com>' in text
- assert 'Subject: A test' in text
+ controller.stop()
diff --git a/Allura/test.ini b/Allura/test.ini
index 8c3604c11..c46c0646a 100644
--- a/Allura/test.ini
+++ b/Allura/test.ini
@@ -84,6 +84,9 @@ markdown_render_max_length = 40000
; TODO: update tests and remove this setting override
auth.require_email_addr = false
+forgemail.host = 127.0.0.1
+forgemail.port = 8827
+
[app:task]
use = main
; TurboGears will use controllers/task.py as root controller
diff --git a/AlluraTest/alluratest/smtp_debug.py b/AlluraTest/alluratest/smtp_debug.py
index b6cec8324..f4e1517e8 100644
--- a/AlluraTest/alluratest/smtp_debug.py
+++ b/AlluraTest/alluratest/smtp_debug.py
@@ -21,17 +21,9 @@
from io import StringIO
from paste.deploy.converters import asint
+
class BetterDebuggingServer:
- def start_server(self):
- stream = StringIO()
- handler = Debugging(stream) # throws away the email
- hostname = tg.config.get('forgemail.host', '0.0.0.0')
- port = asint(tg.config.get('forgemail.port', 8825))
- controller = Controller(handler, hostname=hostname, port=port)
- controller.start()
- return controller, handler
-
async def handle_DATA(self, server, session, envelope):
try:
(allura) 01/03: [#8574] adding aiosmtpd to replace smtpd package
Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git
commit ce93579b37fb4f1419db5889ec5c4b4d037bc0e9
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Fri Jan 31 12:17:00 2025 -0700
[#8574] adding aiosmtpd to replace smtpd package
---
Allura/allura/command/smtp_server.py | 59 +++++++++++++++++++---------
Allura/docs/getting_started/installation.rst | 2 +-
AlluraTest/alluratest/smtp_debug.py | 27 ++++++++++---
docker-compose.yml | 2 +-
4 files changed, 63 insertions(+), 27 deletions(-)
diff --git a/Allura/allura/command/smtp_server.py b/Allura/allura/command/smtp_server.py
index a8fe44b65..3a1b07e42 100644
--- a/Allura/allura/command/smtp_server.py
+++ b/Allura/allura/command/smtp_server.py
@@ -15,19 +15,27 @@
# specific language governing permissions and limitations
# under the License.
-import smtpd
-import asyncore
-
import faulthandler
import tg
from paste.script import command
import allura.tasks
from allura.command import base
+from allura.command.base import EmptyClass, Command
from allura.lib import helpers as h
-
from paste.deploy.converters import asint
+from aiosmtpd.controller import Controller
+import asyncio
+from tg.wsgiapp import RequestLocals
+
+async def start_server(loop):
+ handler = MailServer()
+ hostname = tg.config.get('forgemail.host', '0.0.0.0')
+ port = asint(tg.config.get('forgemail.port', 8825))
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
+
class SMTPServerCommand(base.Command):
min_args = 1
@@ -38,25 +46,38 @@ class SMTPServerCommand(base.Command):
parser.add_option('-c', '--context', dest='context',
help=('The context of the message (path to the project'
' and/or tool'))
-
+
def command(self):
faulthandler.enable()
self.basic_setup()
- MailServer((tg.config.get('forgemail.host', '0.0.0.0'),
- asint(tg.config.get('forgemail.port', 8825))),
- None)
- asyncore.loop()
-
-
-class MailServer(smtpd.SMTPServer):
-
- def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
+ loop = asyncio.get_event_loop()
+ task = loop.create_task(start_server(loop=loop))
+ try:
+ loop.run_forever()
+ except KeyboardInterrupt:
+ task.cancel()
+ finally:
+ loop.close()
+
+class MailServer:
+ async def handle_DATA(self, server, session, envelope):
try:
+ peer = session.peer
+ mailfrom = envelope.mail_from
+ rcpttos = envelope.rcpt_tos
+ data = envelope.content
base.log.info('Msg Received from %s for %s', mailfrom, rcpttos)
base.log.info(' (%d bytes)', len(data))
- task = allura.tasks.mail_tasks.route_email.post(
- peer=peer, mailfrom=mailfrom, rcpttos=rcpttos,
- data=h.really_unicode(data))
+ tgl = RequestLocals()
+ tgl.tmpl_context = EmptyClass()
+ tgl.app_globals = tg.app_globals
+ tg.request_local.context._push_object(tgl)
+ task = allura.tasks.mail_tasks.route_email.post(peer=peer, mailfrom=mailfrom, rcpttos=rcpttos,
+ data=h.really_unicode(data))
base.log.info(f'Msg passed along as task {task._id}')
- except Exception:
- base.log.exception('Error handling msg')
+ except Exception as error:
+ base.log.exception(f'Error handling msg - {error}')
+ return '500 Could not process your message'
+
+ return '250 OK'
+
diff --git a/Allura/docs/getting_started/installation.rst b/Allura/docs/getting_started/installation.rst
index fddfa16f2..972b58055 100644
--- a/Allura/docs/getting_started/installation.rst
+++ b/Allura/docs/getting_started/installation.rst
@@ -341,7 +341,7 @@ your :file:`development.ini` (8826 by default).
.. code-block:: bash
- python -u -m smtpd -n -c DebuggingServer localhost:8826
+ python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d
This will create a new debugging server that discards messages and prints them to stdout.
diff --git a/AlluraTest/alluratest/smtp_debug.py b/AlluraTest/alluratest/smtp_debug.py
index 377754bed..20db4fc32 100644
--- a/AlluraTest/alluratest/smtp_debug.py
+++ b/AlluraTest/alluratest/smtp_debug.py
@@ -15,11 +15,26 @@
# specific language governing permissions and limitations
# under the License.
-from smtpd import DebuggingServer
+import tg
+from aiosmtpd.handlers import Debugging, Sink
+from aiosmtpd.controller import Controller
+from io import StringIO
+from paste.deploy.converters import asint
+class BetterDebuggingServer:
+
+ def process_message(self, server, session, envelope):
+ stream = StringIO()
+ handler = Debugging(stream) # throws away the email
+ hostname = tg.config.get('forgemail.host', '0.0.0.0')
+ port = asint(tg.config.get('forgemail.port', 8825))
+ controller = Controller(handler, hostname=hostname, port=port)
+ controller.start()
-class BetterDebuggingServer(DebuggingServer):
-
- def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
- print('TO: ' + ', '.join(rcpttos))
- super().process_message(peer, mailfrom, rcpttos, data)
+ async def handle_DATA(self, server, session, envelope):
+ try:
+ rcpttos = envelope.rcpt_tos
+ print('TO: ' + ', '.join(rcpttos))
+ except Exception as error:
+ return f'500 Could not process your message. Error {error}'
+ return '250 OK'
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index b4d2e3a4d..b0c0c28c5 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -64,7 +64,7 @@ services:
environment: *env
volumes_from:
- web
- command: python -u -m smtpd -n -c alluratest.smtp_debug.BetterDebuggingServer 0.0.0.0:8826
+ command: python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d
expose:
- "8826"
(allura) 02/03: [#8574] updated test to make use of the BetterDebuggingServer class
Posted by br...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git
commit c0c57763149aa904ddc80a8b322700c695403a58
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Tue Feb 4 14:46:27 2025 -0700
[#8574] updated test to make use of the BetterDebuggingServer class
---
Allura/allura/tests/test_mail_util.py | 34 ++++++++++++++++++++--------
Allura/docs/getting_started/installation.rst | 2 +-
AlluraTest/alluratest/smtp_debug.py | 8 ++++++-
docker-compose.yml | 2 +-
4 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index c549e5a01..19001b8d5 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -22,9 +22,9 @@
import pytest
from ming.odm import ThreadLocalODMSession
from tg import config as tg_config
-
+from smtplib import SMTP as SMTPClient
from alluratest.controller import setup_basic_test, setup_global_objects
-from allura.command.smtp_server import MailServer
+from alluratest.smtp_debug import BetterDebuggingServer
from allura.lib.utils import ConfigProxy
from allura.app import Application
from allura.lib.mail_util import (
@@ -38,7 +38,8 @@
)
from allura.lib.exceptions import AddressException
from allura.tests import decorators as td
-
+from paste.deploy.converters import asint
+from aiosmtpd.handlers import Debugging
config = ConfigProxy(
common_suffix='forgemail.domain',
@@ -334,9 +335,24 @@ def setup_method(self, method):
@mock.patch('allura.command.base.log', autospec=True)
def test(self, log):
- listen_port = ('0.0.0.0', 8825)
- mailserver = MailServer(listen_port, None)
- mailserver.process_message('127.0.0.1', 'foo@bar.com', ['1234@tickets.test.p.localhost'],
- 'this is the email body with headers and everything Ο'.encode())
- assert [] == log.exception.call_args_list
- assert log.info.call_args[0][0].startswith('Msg passed along as task '), log.info.call_args
+ mailserver = BetterDebuggingServer()
+ controller, handler = mailserver.start_server()
+ hostname = tg_config.get('forgemail.host', '0.0.0.0')
+ port = asint(tg_config.get('forgemail.port', 8825))
+ with SMTPClient(hostname, port, timeout=0.1) as client:
+ resp = client.docmd("HELP", "HELO")
+ code, msg = client.ehlo("example.com")
+ assert code == 250
+ client.sendmail('aperson@example.com', ['bperson@example.com'],"""
+ From: Anne Person <an...@example.com>
+ To: Bart Person <ba...@example.com>
+ Subject: A test
+ Hi Bart, this is Anne.
+ """)
+ assert isinstance(handler, Debugging)
+ text = handler.stream.getvalue()
+ assert 'From: Anne Person <an...@example.com>' in text
+ assert ' To: Bart Person <ba...@example.com>' in text
+ assert 'Subject: A test' in text
+
+
diff --git a/Allura/docs/getting_started/installation.rst b/Allura/docs/getting_started/installation.rst
index 972b58055..bb3771d56 100644
--- a/Allura/docs/getting_started/installation.rst
+++ b/Allura/docs/getting_started/installation.rst
@@ -341,7 +341,7 @@ your :file:`development.ini` (8826 by default).
.. code-block:: bash
- python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d
+ python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826
This will create a new debugging server that discards messages and prints them to stdout.
diff --git a/AlluraTest/alluratest/smtp_debug.py b/AlluraTest/alluratest/smtp_debug.py
index 20db4fc32..b6cec8324 100644
--- a/AlluraTest/alluratest/smtp_debug.py
+++ b/AlluraTest/alluratest/smtp_debug.py
@@ -23,18 +23,24 @@
class BetterDebuggingServer:
- def process_message(self, server, session, envelope):
+ def start_server(self):
stream = StringIO()
handler = Debugging(stream) # throws away the email
hostname = tg.config.get('forgemail.host', '0.0.0.0')
port = asint(tg.config.get('forgemail.port', 8825))
controller = Controller(handler, hostname=hostname, port=port)
controller.start()
+ return controller, handler
+
async def handle_DATA(self, server, session, envelope):
try:
rcpttos = envelope.rcpt_tos
+ message = envelope.content.decode("utf-8")
+ message = "\n".join(message.split("\r\n"))
print('TO: ' + ', '.join(rcpttos))
+ print('FROM: ' + envelope.mail_from)
+ print(message)
except Exception as error:
return f'500 Could not process your message. Error {error}'
return '250 OK'
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index b0c0c28c5..a6c663959 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -64,7 +64,7 @@ services:
environment: *env
volumes_from:
- web
- command: python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826 -d
+ command: python -u -m aiosmtpd -n -c alluratest.smtp_debug.BetterDebuggingServer -l 0.0.0.0:8826
expose:
- "8826"