remote debugger: adds pycharm remote debugger

This commit is contained in:
ievgenii vdovenko 2025-06-11 10:09:19 +02:00
parent 8d60a35c68
commit d00249de71
4 changed files with 55 additions and 0 deletions

View file

@ -25,3 +25,6 @@ types-sqlalchemy
types-psutil
types-pycurl
types-ujson
#remote debugger
pydevd-pycharm~=251.25410.159

View file

@ -0,0 +1,2 @@
PYCHARM_DEBUG = "PYCHARM_DEBUG"
PYCHARM_DEBUG_PAUSE_AT_STARTUP = "PYCHARM_DEBUG_PAUSE_AT_STARTUP"

View file

@ -31,6 +31,8 @@ from pyramid.settings import asbool, aslist
from pyramid.httpexceptions import HTTPException, HTTPError, HTTPInternalServerError, HTTPFound, HTTPNotFound
from pyramid.renderers import render_to_response
from rhodecode.config.constants import PYCHARM_DEBUG
from rhodecode.lib.middleware.pycharm_remote_debugger import PyCharmDebugMiddleware
from rhodecode.model import meta
from rhodecode.config import patches
@ -420,6 +422,16 @@ def includeme(config, auth_resources=None):
config.add_view(error_handler, context=HTTPError)
def wrap_app_in_pycharm_remote_debugger_if_enabled(pyramid_app):
pycharm_debugger_enabled = os.getenv(PYCHARM_DEBUG, "0") == "1"
log.debug(f"Pycharm remote debugger enabled: {pycharm_debugger_enabled}")
if pycharm_debugger_enabled:
return PyCharmDebugMiddleware(pyramid_app)
return pyramid_app
def wrap_app_in_wsgi_middlewares(pyramid_app, config):
"""
Apply outer WSGI middlewares around the application.
@ -429,6 +441,7 @@ def wrap_app_in_wsgi_middlewares(pyramid_app, config):
# enable https redirects based on HTTP_X_URL_SCHEME set by proxy
pyramid_app = HttpsFixup(pyramid_app, settings)
pyramid_app = wrap_app_in_pycharm_remote_debugger_if_enabled(pyramid_app)
pyramid_app, _ae_client = wrap_in_appenlight_if_enabled(pyramid_app, settings)
registry.ae_client = _ae_client

View file

@ -0,0 +1,37 @@
import os
import logging
import socket
from rhodecode.config.constants import PYCHARM_DEBUG_PAUSE_AT_STARTUP
log = logging.getLogger(__name__)
class PyCharmDebugMiddleware:
def __init__(self, handler):
self.handler = handler
self._start_debugger()
def _start_debugger(self):
try:
import pydevd_pycharm
suspend = os.getenv(PYCHARM_DEBUG_PAUSE_AT_STARTUP, "0") == "1"
host = "host.docker.internal" # assuming that app is running inside docker, and the debug server is on the same machine
pydevd_pycharm.settrace(
host,
suspend=suspend,
stdoutToServer=True,
stderrToServer=True,
)
log.debug("PyCharm debugger attached successfully!")
except ImportError:
log.warning("pydevd_pycharm not installed. Debugging disabled.")
except ConnectionRefusedError:
ip = socket.gethostbyname(host)
log.warning(f"debug server is not running on host[ip]: {host}[{ip}], shutdown remote debugger.")
pydevd_pycharm.stoptrace()
def __call__(self, environ, start_response):
return self.handler(environ, start_response)