~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/python/rpdb.py

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers 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 Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Remote Python Debugger (pdb wrapper)."""
 
18
 
 
19
import pdb
 
20
import socket
 
21
import sys
 
22
 
 
23
__author__ = "Bertrand Janin <b@janin.com>"
 
24
__version__ = "0.1.3"
 
25
 
 
26
 
 
27
class Rpdb(pdb.Pdb):
 
28
 
 
29
    def __init__(self, addr="127.0.0.1", port=4444):
 
30
        """Initialize the socket and initialize pdb."""
 
31
 
 
32
        # Backup stdin and stdout before replacing them by the socket handle
 
33
        self.old_stdout = sys.stdout
 
34
        self.old_stdin = sys.stdin
 
35
 
 
36
        # Open a 'reusable' socket to let the webapp reload on the same port
 
37
        self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
38
        self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
 
39
        self.skt.bind((addr, port))
 
40
        self.skt.listen(1)
 
41
        (clientsocket, address) = self.skt.accept()
 
42
        handle = clientsocket.makefile('rw')
 
43
        pdb.Pdb.__init__(self, completekey='tab', stdin=handle, stdout=handle)
 
44
        sys.stdout = sys.stdin = handle
 
45
 
 
46
    def shutdown(self):
 
47
        """Revert stdin and stdout, close the socket."""
 
48
        sys.stdout = self.old_stdout
 
49
        sys.stdin = self.old_stdin
 
50
        self.skt.close()
 
51
        self.set_continue()
 
52
 
 
53
    def do_continue(self, arg):
 
54
        """Stop all operation on ``continue``."""
 
55
        self.shutdown()
 
56
        return 1
 
57
 
 
58
    do_EOF = do_quit = do_exit = do_c = do_cont = do_continue