3 # A generic Python debugger for Speare Pro.
4 # Copyright (c) 2019 sevenuc.com. All rights reserved.
6 # THIS FILE IS PART OF THE ADVANCED VERSION OF SPEARE CODE EDITOR.
7 # WITHOUT THE WRITTEN PERMISSION OF THE AUTHOR THIS FILE MAY NOT
8 # BE USED FOR ANY COMMERCIAL PRODUCT.
11 # http://sevenuc.com/en/Speare.html
13 # Sevenuc support <info@sevenuc.com>
14 # Issue report and requests pull:
15 # https://github.com/chengdu/Speare
22 from bdb import BdbQuit
23 from debugstub import Debugstub
27 if (sys.version_info.major != 2):
28 print("Wrong Python version!")
32 sys.setdefaultencoding('utf-8')
37 print(" / __/ __ ___ ___ ___ ___")
38 print(" _\\ \\/ _ \\/ -_) _ `/ __/ -_)")
39 print(" /___/ .__/\\__/\\_,_/_/ \\__/")
41 print("Speare Debug Server v1.0")
42 print("(c) http://sevenuc.com \n")
44 def startDebugger(sock, connection, port, filename):
45 print('Start debugging session on: %s' % filename)
46 base = os.path.dirname(filename)
47 sys.path.insert(0, base)
48 signal.signal(signal.SIGTTOU, signal.SIG_IGN)
49 dbs = Debugstub(sock, connection)
50 dir_path = os.path.dirname(os.path.realpath(__file__))
51 files = ["debugger.py", "debugstub.py", "server.py"]
52 dbs.excluded_files = map(lambda x: os.path.join(dir_path, x), files)
55 dbs.basedirs.append(base)
56 dbs._runscript(filename)
58 if len(sys.argv) == 2:
59 try: port = int(sys.argv[1])
61 print('*** invalid port number: "%s".' % sys.argv[1])
64 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65 server_address = ('localhost', port)
66 sock.bind(server_address) # Address already in use
67 sock.listen(1) # Listen for incoming connection
70 print('Listen on port %d ...' % port)
71 connection, client_address = sock.accept()
72 #connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
74 data = connection.recv(1024) # Receive the startup script
75 filename = data.decode('utf-8').strip('\r\n')
76 if filename.startswith("b'"): filename = filename[2:-2]
79 if filename: startDebugger(sock, connection, port, filename)
80 else: print("*** can't get a script to start debugging session.")
82 if connection: connection.close()