Skip to content

Commit

Permalink
Merge pull request #524 from Charcoal-SE/pydevd_debugging_options
Browse files Browse the repository at this point in the history
Enable ability to go into 'Attach Debugger' mode w/ PyCharm debuggers

We will very likely revert this later.
  • Loading branch information
teward committed Feb 13, 2017
2 parents 19f03a9 + 93c315f commit f0707ff
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
11 changes: 8 additions & 3 deletions .gitignore
Expand Up @@ -66,8 +66,13 @@ test/.cache/
# Config
config

# Ignore .DS_Store file (usually seen in Mac environments)
.DS_Store

# PyDev Debugger Module - should not be included in Repository
# if this is present, because this can differ from env. to env.
pydevd.py

# PyCharm Debugger Package (includes pydevd and related plugins).
#
# Necessary for the Debugging handler's enablement, and to allow
# for the debugger to communicate with PyCharm debugger servers
# running on a specified system.
!/pycharm-debug.egg
44 changes: 44 additions & 0 deletions debugging.py
@@ -0,0 +1,44 @@
import os


# noinspection PyClassHasNoInit,PyDeprecation
class Debugging:
enabled = None
pydev_host = None
pydev_port = None
environ_str = None
environ_args = None

_envpydev = str(os.environ.get('PYDEVD')).lower()

if not _envpydev:
enabled = False
elif _envpydev.__contains__('true') or _envpydev.__contains__('enable') or _envpydev.__contains__('yes'):
enabled = True

environ_str = "PYDEVD='%s'" % 'True'

host = os.environ.get('PYDEVD_HOST')
if not host:
pydev_host = 'localhost'
else:
pydev_host = str(host)

if pydev_host != 'localhost':
environ_str += " PYDEVD_HOST='%s'" % pydev_host

port = os.environ.get('PYDEVD_PORT')
if not port:
pydev_port = 20500
else:
pydev_port = int(port)

if pydev_port != 20500:
environ_str += " PYDEVD_PORT='%s'" % pydev_port

environ_args = environ_str.split()
environ_str += " "
environ_dict = {'PYDEVD': enabled, 'PYDEVD_HOST': pydev_host, 'PYDEVD_PORT': pydev_port}

else:
enabled = False
14 changes: 11 additions & 3 deletions nocrash.py
Expand Up @@ -5,6 +5,7 @@
import subprocess as sp
from time import sleep
import sys
from debugging import Debugging

# Get environment variables
ChatExchangeU = os.environ.get('ChatExchangeU')
Expand All @@ -26,8 +27,17 @@
switch_to_standby = False
ecode = None # Define this to prevent errors

# Make a clean copy of existing environment variables, to pass down to subprocess.
environ = os.environ.copy()

# Add debug environment variables to environment copy in variable 'environ', if Debugging is enabled
if Debugging.enabled:
for (key, value) in Debugging.environ_dict.iteritems():
environ[key] = str(value)

while stoprunning is False:
# print "[NoCrash] Switch to Standby? %s" % switch_to_standby

if count == 0:
if switch_to_standby or ("standby" in sys.argv):
switch_to_standby = False # Necessary for the while loop
Expand All @@ -42,12 +52,10 @@
command = 'python ws.py standby'.split()

try:
ecode = sp.call(command + persistent_arguments)
ecode = sp.call(command + persistent_arguments, env=environ)
except KeyboardInterrupt:
# print "[NoCrash] KeyBoard Interrupt received.."
ecode = 6
except Exception as e:
raise e

if ecode == 3:
# print "[NoCrash] Pull in new updates."
Expand Down
Binary file added pycharm-debug.egg
Binary file not shown.
29 changes: 28 additions & 1 deletion ws.py
Expand Up @@ -11,6 +11,33 @@
# Hence, please avoid adding code before this comment; if it's necessary,
# test it thoroughly.

import os
from debugging import Debugging
if Debugging.enabled:
# noinspection PyBroadException
try:
sys.path.append('pycharm-debug.egg')
# noinspection PyUnresolvedReferences, PyPackageRequirements
import pydevd
except:
print "Debugging enabled, but 'pydevd' is not available or not working."
# noinspection PyProtectedMember
os._exit(6)

# noinspection PyBroadException
try:
print "Attaching Debugger..."
pydevd.settrace(host=Debugging.pydev_host, port=Debugging.pydev_port, stdoutToServer=True, stderrToServer=True,
suspend=True)
except Exception:
print "Unable to attach to debugger."
# noinspection PyProtectedMember
os._exit(6)

else:
pass # We do nothing here if Debugger is not set, so go on.


# noinspection PyPackageRequirements
import websocket
import getpass
Expand All @@ -27,10 +54,10 @@
from metasmoke import Metasmoke
from deletionwatcher import DeletionWatcher
import json
import os
import time
import requests


if "ChatExchangeU" in os.environ:
username = os.environ["ChatExchangeU"]
else:
Expand Down

0 comments on commit f0707ff

Please sign in to comment.