mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
rundjangoserver: Filter stdout instead of forking the code.
We failed to update this fork for the Django 3.2 upgrade. Unfork it so that’s not something we need to remember to do. Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
276d95b58b
commit
9d0340c7a8
@@ -6,7 +6,6 @@ EXCLUDED_FILES = [
|
||||
"puppet/zulip/files/nagios_plugins/zulip_nagios_server/check_website_response.sh",
|
||||
"scripts/lib/third",
|
||||
"static/third",
|
||||
"zilencer/management/commands/rundjangoserver.py",
|
||||
# Transifex syncs translation.json files without trailing
|
||||
# newlines; there's nothing other than trailing newlines we'd be
|
||||
# checking for in these anyway.
|
||||
|
@@ -1,69 +1,46 @@
|
||||
import errno
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
from typing import Any, NoReturn
|
||||
from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.commands.runserver import Command as DjangoCommand
|
||||
from django.core.servers.basehttp import run
|
||||
from django.utils import autoreload
|
||||
from typing_extensions import Protocol
|
||||
|
||||
|
||||
class Writable(Protocol):
|
||||
def write(self, s: str) -> None:
|
||||
...
|
||||
|
||||
|
||||
class FakeStdout:
|
||||
"""
|
||||
Filter stdout from Django's runserver, to declutter run-dev.py
|
||||
startup output.
|
||||
"""
|
||||
|
||||
def __init__(self, stdout: Writable) -> None:
|
||||
self.stdout = stdout
|
||||
# Might fail to suppress the date line around midnight, but, whatever.
|
||||
self.date_prefix = datetime.now().strftime("%B %d, %Y - ")
|
||||
|
||||
def write(self, s: str) -> None:
|
||||
if (
|
||||
s == "Performing system checks...\n\n"
|
||||
or s.startswith("System check identified no issues")
|
||||
or s.startswith(self.date_prefix)
|
||||
):
|
||||
pass
|
||||
elif "Quit the server with " in s:
|
||||
self.stdout.write(
|
||||
"Django process (re)started. " + s[s.index("Quit the server with ") :]
|
||||
)
|
||||
else:
|
||||
self.stdout.write(s)
|
||||
|
||||
|
||||
class Command(DjangoCommand):
|
||||
# Copied from Django's runserver and modified to remove print statements.
|
||||
# This lets us declutter run-dev.py startup output.
|
||||
def inner_run(self, *args: Any, **options: Any) -> None:
|
||||
# If an exception was silenced in ManagementUtility.execute in order
|
||||
# to be raised in the child process, raise it now.
|
||||
autoreload.raise_last_exception()
|
||||
|
||||
threading = options["use_threading"]
|
||||
# 'shutdown_message' is a stealth option.
|
||||
shutdown_message = options.get("shutdown_message", "")
|
||||
quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C"
|
||||
|
||||
self.check(display_num_errors=False)
|
||||
# Need to check migrations here, so can't use the
|
||||
# requires_migrations_check attribute.
|
||||
self.check_migrations()
|
||||
self.stdout.write(
|
||||
("Django process (re)started. Quit the server with %(quit_command)s.\n")
|
||||
% {
|
||||
"version": self.get_version(),
|
||||
"settings": settings.SETTINGS_MODULE,
|
||||
"protocol": self.protocol,
|
||||
"addr": "[%s]" % self.addr if self._raw_ipv6 else self.addr,
|
||||
"port": self.port,
|
||||
"quit_command": quit_command,
|
||||
}
|
||||
)
|
||||
stdout: Writable
|
||||
|
||||
def inner_run(self, *args: object, **options: object) -> None:
|
||||
self.stdout = FakeStdout(self.stdout)
|
||||
try:
|
||||
handler = self.get_handler(*args, **options)
|
||||
run(
|
||||
self.addr,
|
||||
int(self.port),
|
||||
handler,
|
||||
ipv6=self.use_ipv6,
|
||||
threading=threading,
|
||||
server_cls=self.server_cls,
|
||||
)
|
||||
except socket.error as e:
|
||||
# Use helpful error messages instead of ugly tracebacks.
|
||||
ERRORS = {
|
||||
errno.EACCES: "You don't have permission to access that port.",
|
||||
errno.EADDRINUSE: "That port is already in use.",
|
||||
errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
|
||||
}
|
||||
try:
|
||||
error_text = ERRORS[e.errno]
|
||||
except KeyError:
|
||||
error_text = str(e)
|
||||
self.stderr.write("Error: %s" % error_text)
|
||||
# Need to use an OS exit because sys.exit doesn't work in a thread
|
||||
os._exit(1)
|
||||
except KeyboardInterrupt:
|
||||
if shutdown_message:
|
||||
self.stdout.write(shutdown_message)
|
||||
sys.exit(0)
|
||||
super().inner_run(*args, **options)
|
||||
finally:
|
||||
self.stdout = self.stdout.stdout
|
||||
|
Reference in New Issue
Block a user