[PROTOTYPE] display all created/called view/form during a request

Closes #17219729

authorLaurent Peuch <cortex@worlddomination.be>
changesetbce3a2f4bc69
branchdefault
phasedraft
hiddenno
parent revision#a4d465a3e77d fix(ci): manually remove the .tox/doc directory
child revision<not specified>
files modified by this revision
cubicweb/pyramid/pyramidctl.py
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1563977460 -7200
# Wed Jul 24 16:11:00 2019 +0200
# Node ID bce3a2f4bc692ed66df1d965a25eb6a4bb3c9cf7
# Parent a4d465a3e77d07cf6a79c121c10b2d6484cd7468
[PROTOTYPE] display all created/called view/form during a request

Closes #17219729

diff --git a/cubicweb/pyramid/pyramidctl.py b/cubicweb/pyramid/pyramidctl.py
@@ -40,14 +40,78 @@
1  from cubicweb.view import inject_html_generating_call_on_w
2  from cubicweb.server import serverctl
3  from cubicweb.web.webctl import WebCreateHandler
4  from cubicweb.toolsutils import fill_templated_file
5 
6 +from cubicweb import view
7 +from cubicweb.web import form
8 +
9 +
10  import waitress
11 
12  MAXFD = 1024
13 
14 +def debug_call_stack(frame, event, arg):
15 +    if event != 'call':
16 +        return
17 +
18 +    func_name = frame.f_code.co_name
19 +    if func_name not in ("__init__", "__call__", "render", "w"):
20 +        return
21 +
22 +    if "self" not in frame.f_locals:
23 +        if (not func_name.startswith("__") and func_name not in ("<module>", "_add_rdef", "set_perms")) or func_name in ("__call__", "__init__"):
24 +            try:
25 +                # print("\033[1;35moh no '%s'\033[0m (%s)" % (func_name, frame.f_locals))
26 +                pass
27 +            except Exception:
28 +                print("ouin")
29 +        return
30 +
31 +    self = frame.f_locals["self"]
32 +
33 +    if self.__module__.startswith(("waitress", "threading", "socket", "logging", "_weakrefset", "pyramid", "six", "pkg_resources", "urllib3", "rql", "yapps")) or str(type(self)) == "<module>":
34 +        return
35 +
36 +    # print("\033[1;36m%s\033[0m %s" % (type(self), func_name))
37 +    if isinstance(self, view.View):
38 +        color = "1;36"
39 +        kind = "[view]"
40 +    elif isinstance(self, form.Form):
41 +        color = "1;35"
42 +        kind = "[form]"
43 +    elif func_name == "w":
44 +        color = "1.34"
45 +        kind = "[w]"
46 +    else:
47 +        return
48 +
49 +    args = []
50 +    for i in range(frame.f_code.co_argcount):
51 +        name = frame.f_code.co_varnames[i]
52 +        args.append("%s=%s" % (name, frame.f_locals[name]))
53 +
54 +    call = "%s.%s.%s" % (self.__module__, self.__class__.__name__, func_name)
55 +    base_len = len(kind) + 1 + len(call) + 1
56 +
57 +    args = (",\n%s" % (" " * base_len)).join(args)
58 +
59 +    co = frame.f_code
60 +    func_line_no = frame.f_lineno
61 +    func_filename = co.co_filename
62 +    caller = frame.f_back
63 +    caller_line_no = caller.f_lineno
64 +    caller_filename = caller.f_code.co_filename
65 +    print("\033[%sm%s %s(%s)\033[0m\nat %s:%s" % (color, kind, call, args, caller_filename, caller_line_no))
66 +    # print('Call to %s on line %s of %s from line %s of %s' % \
67 +            # (func_name, func_line_no, func_filename,
68 +             # caller_line_no, caller_filename))
69 +    return
70 +
71 +
72 +
73 +
74 
75  def _generate_pyramid_ini_file(pyramid_ini_path):
76      """Write a 'pyramid.ini' file into apphome."""
77      template_fpath = os.path.join(os.path.dirname(__file__), 'pyramid.ini.tmpl')
78      context = {
@@ -286,10 +350,11 @@
79          port = cwconfig['port'] or 8080
80          url_scheme = ('https' if cwconfig['base-url'].startswith('https')
81                        else 'http')
82          repo = app.application.registry['cubicweb.repository']
83          try:
84 +            # threading.settrace(debug_call_stack)
85              waitress.serve(app, host=host, port=port, url_scheme=url_scheme,
86                             clear_untrusted_proxy_headers=True, threads=self['nb-threads'])
87          finally:
88              repo.shutdown()
89          if self._needreload: