1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 A recommended X2go session clean up helper function.
22
23 """
24
25 import gevent
26 import paramiko
27 import threading
28
29
30 import guardian
31 import rforward
32 from defaults import X2GOCLIENT_OS as _X2GOCLIENT_OS
33
34 if _X2GOCLIENT_OS == 'Windows':
35 import xserver
36 import pulseaudio
37
39 """\
40 For every Python X2go application you write, please make sure to
41 capture the C{KeyboardInterrupt} and the C{SystemExit} exceptions and
42 call this function if either of the exceptions occurs.
43
44 Example::
45
46 import x2go
47
48 try:
49 my_x2goclient = x2go.X2goClient(...)
50
51 [... your code ...]
52
53 sys.exit(0)
54 except (KeyboardInterrupt, SystemExit):
55 x2go.x2go_cleanup()
56
57 @param e: if L{x2go_cleanup} got called as you caught an exception in your code this can be the
58 C{Exception} that we will process at the end of the clean-up (or if clean-up failed or was not
59 appropriate)
60 @type e: C{exception}
61 @param threads: a list of threads to clean up
62 @type threads: C{list}
63
64 """
65 try:
66 if threads is None:
67 threads = threading.enumerate()
68 else:
69 threads = threads
70
71
72 for t in threads:
73 if type(t) == rforward.X2goRevFwTunnel:
74 t.stop_thread()
75 del t
76
77
78 for t in threads:
79 if type(t) == paramiko.Transport:
80 if hasattr(t, '_x2go_session_marker'):
81 t.stop_thread()
82 del t
83
84
85 if _X2GOCLIENT_OS == 'Windows':
86 for t in threads:
87 if type(t) == xserver.X2goXServer:
88 t.stop_thread()
89 gevent.sleep(1)
90 del t
91
92
93 if _X2GOCLIENT_OS == 'Windows':
94 for t in threads:
95 if type(t) == pulseaudio.X2goPulseAudio:
96 t.stop_thread()
97 gevent.sleep(1)
98 del t
99
100 if e is not None:
101 raise e
102
103 except KeyboardInterrupt:
104
105 pass
106 except SystemExit:
107
108 pass
109