OSDN Git Service

Initial commit
[speare/speare.git] / python debugger / 2.x / server.py
1 #! /usr/bin/env python
2
3 # A generic Python debugger for Speare Pro.
4 # Copyright (c) 2019 sevenuc.com. All rights reserved.
5
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.
9
10 # More info: 
11 #    http://sevenuc.com/en/Speare.html
12 # Contact:
13 #    Sevenuc support <info@sevenuc.com>
14 # Issue report and requests pull:
15 #    https://github.com/chengdu/Speare
16
17 import os
18 import sys
19 import codecs
20 import signal
21 import socket
22 from bdb import BdbQuit
23 from debugstub import Debugstub
24
25 port = 4444
26 __version__ = '0.0.2'
27 if (sys.version_info.major != 2):
28     print("Wrong Python version!")
29     sys.exit(0)
30
31 reload(sys)
32 sys.setdefaultencoding('utf-8')
33
34 def printbanner():
35     print("\n")
36     print("   ____")
37     print("  / __/ __  ___ ___  ___ ___")
38     print("  _\\ \\/ _ \\/ -_) _ `/ __/ -_)")
39     print(" /___/ .__/\\__/\\_,_/_/  \\__/")
40     print("    /_/")
41     print("Speare Debug Server v1.0")
42     print("(c) http://sevenuc.com \n")
43
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)
53     dbs.set_trace()
54     dbs.basedirs = []
55     dbs.basedirs.append(base)
56     dbs._runscript(filename)
57
58 if len(sys.argv) == 2:
59     try: port = int(sys.argv[1])
60     except:
61         print('*** invalid port number: "%s".' % sys.argv[1])
62         sys.exit(0)
63
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
68 filename = None
69 printbanner()
70 print('Listen on port %d ...' % port)
71 connection, client_address = sock.accept()
72 #connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
73 while True:
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]
77     break
78 try:
79     if filename: startDebugger(sock, connection, port, filename)
80     else: print("*** can't get a script to start debugging session.")
81 finally:
82     if connection: connection.close()
83     if sock: sock.close()
84