stratumserver: Truncate overlong extranonce2 in share submissions to workaround BitFo...
[bitcoin:eloipool.git] / interactivemode.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 def exit():
18         import os, signal
19         os.kill(os.getpid(), signal.SIGTERM)
20
21 def _RunCLI():
22         import code, sys, threading
23         namespace = {}
24         try:
25                 raise None
26         except:
27                 # The goal here is to find the context that imported us
28                 try:
29                         frame = sys.exc_info()[2].tb_frame
30                         while frame.f_code.co_filename[0] != '<':
31                                 frame = frame.f_back
32                         while frame.f_code.co_filename[0] == '<':
33                                 frame = frame.f_back
34                         namespace.update(frame.f_globals)
35                         namespace.update(frame.f_locals)
36                 except:
37                         print("InteractiveMode: Error setting up initial namespace")
38                 finally:
39                         frame = None
40         
41         namespace.setdefault('exit', exit)
42         
43         def CLI():
44                 while True:
45                         code.interact(local=namespace, banner='')
46                         print("Not exiting implicitly. Use exit() if you really want to.")
47                         dt = ndt = 0
48                         for thread in threading.enumerate():
49                                 if thread.daemon:
50                                         dt += 1
51                                 else:
52                                         ndt += 1
53                         print("(%d threads: %d primary, %d daemon)" % (dt + ndt, ndt, dt))
54         threading.Timer(0, CLI).start()
55
56 _RunCLI()