~verterok/charms/xenial/conn-check/focal

« back to all changes in this revision

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

  • Committer: Guillermo Gonzalez
  • Date: 2023-06-29 16:33:24 UTC
  • Revision ID: guillermo.gonzalez@canonical.com-20230629163324-03vq3m9qtvu6f6or
update charmhelpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
"""Remote Python Debugger (pdb wrapper)."""
 
16
 
 
17
import pdb
 
18
import socket
 
19
import sys
 
20
 
 
21
__author__ = "Bertrand Janin <b@janin.com>"
 
22
__version__ = "0.1.3"
 
23
 
 
24
 
 
25
class Rpdb(pdb.Pdb):
 
26
 
 
27
    def __init__(self, addr="127.0.0.1", port=4444):
 
28
        """Initialize the socket and initialize pdb."""
 
29
 
 
30
        # Backup stdin and stdout before replacing them by the socket handle
 
31
        self.old_stdout = sys.stdout
 
32
        self.old_stdin = sys.stdin
 
33
 
 
34
        # Open a 'reusable' socket to let the webapp reload on the same port
 
35
        self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
36
        self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
 
37
        self.skt.bind((addr, port))
 
38
        self.skt.listen(1)
 
39
        (clientsocket, address) = self.skt.accept()
 
40
        handle = clientsocket.makefile('rw')
 
41
        pdb.Pdb.__init__(self, completekey='tab', stdin=handle, stdout=handle)
 
42
        sys.stdout = sys.stdin = handle
 
43
 
 
44
    def shutdown(self):
 
45
        """Revert stdin and stdout, close the socket."""
 
46
        sys.stdout = self.old_stdout
 
47
        sys.stdin = self.old_stdin
 
48
        self.skt.close()
 
49
        self.set_continue()
 
50
 
 
51
    def do_continue(self, arg):
 
52
        """Stop all operation on ``continue``."""
 
53
        self.shutdown()
 
54
        return 1
 
55
 
 
56
    do_EOF = do_quit = do_exit = do_c = do_cont = do_continue