2017-11-16 00:55:49 +01:00
|
|
|
import cProfile
|
|
|
|
import logging
|
2019-01-15 02:58:03 +01:00
|
|
|
import tempfile
|
2016-04-07 23:11:21 +02:00
|
|
|
from typing import Any, Dict
|
2013-06-19 20:24:16 +02:00
|
|
|
|
2017-07-18 02:02:59 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-07-18 02:02:59 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.middleware import LogRequests
|
2017-07-18 02:02:59 +02:00
|
|
|
from zerver.models import UserMessage, UserProfile
|
2020-06-22 23:32:53 +02:00
|
|
|
from zerver.views.message_fetch import get_messages_backend
|
2013-06-19 20:24:16 +02:00
|
|
|
|
|
|
|
request_logger = LogRequests()
|
|
|
|
|
2017-11-05 11:33:09 +01:00
|
|
|
class MockSession:
|
2017-10-27 12:57:54 +02:00
|
|
|
def __init__(self) -> None:
|
2013-06-19 20:24:16 +02:00
|
|
|
self.modified = False
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
class MockRequest(HttpRequest):
|
2017-10-27 12:57:54 +02:00
|
|
|
def __init__(self, user: UserProfile) -> None:
|
2017-07-18 02:02:59 +02:00
|
|
|
self.user = user
|
2013-06-19 20:24:16 +02:00
|
|
|
self.path = '/'
|
|
|
|
self.method = "POST"
|
|
|
|
self.META = {"REMOTE_ADDR": "127.0.0.1"}
|
2017-11-04 14:16:50 +01:00
|
|
|
anchor = UserMessage.objects.filter(user_profile=self.user).order_by("-message")[200].message_id
|
2016-08-13 16:56:15 +02:00
|
|
|
self.REQUEST = {
|
2017-11-04 14:16:50 +01:00
|
|
|
"anchor": anchor,
|
2016-08-13 16:56:15 +02:00
|
|
|
"num_before": 1200,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"num_after": 200,
|
2016-08-13 16:56:15 +02:00
|
|
|
}
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.GET: Dict[Any, Any] = {}
|
2013-06-19 20:24:16 +02:00
|
|
|
self.session = MockSession()
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def get_full_path(self) -> str:
|
2013-06-19 20:24:16 +02:00
|
|
|
return self.path
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def profile_request(request: HttpRequest) -> HttpResponse:
|
2013-06-19 20:24:16 +02:00
|
|
|
request_logger.process_request(request)
|
|
|
|
prof = cProfile.Profile()
|
|
|
|
prof.enable()
|
2017-03-24 07:51:46 +01:00
|
|
|
ret = get_messages_backend(request, request.user,
|
|
|
|
apply_markdown=True)
|
2013-06-19 20:24:16 +02:00
|
|
|
prof.disable()
|
2019-01-15 02:58:03 +01:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='profile.data.', delete=False) as stats_file:
|
|
|
|
prof.dump_stats(stats_file.name)
|
|
|
|
request_logger.process_response(request, ret)
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Profiling data written to %s", stats_file.name)
|
2013-06-19 20:24:16 +02:00
|
|
|
return ret
|
|
|
|
|
2017-07-18 02:02:59 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2017-10-27 12:57:54 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2017-07-18 02:02:59 +02:00
|
|
|
parser.add_argument("email", metavar="<email>", type=str, help="Email address of the user")
|
|
|
|
self.add_realm_args(parser)
|
2013-06-19 20:24:16 +02:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-07-18 02:02:59 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
user = self.get_user(options["email"], realm)
|
|
|
|
profile_request(MockRequest(user))
|