Package x2go :: Module cleanup
[frames] | no frames]

Source Code for Module x2go.cleanup

  1  # -*- coding: utf-8 -*- 
  2   
  3  # Copyright (C) 2010-2012 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> 
  4  # 
  5  # Python X2Go is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU Affero General Public License as published by 
  7  # the Free Software Foundation; either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Python X2Go is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU Affero General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU Affero General Public License 
 16  # along with this program; if not, write to the 
 17  # Free Software Foundation, Inc., 
 18  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 
 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  # Python X2Go modules 
 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   
38 -def x2go_cleanup(e=None, threads=None):
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 # stop X2Go reverse forwarding tunnels 72 for t in threads: 73 if type(t) == rforward.X2goRevFwTunnel: 74 t.stop_thread() 75 del t 76 77 # stop X2Go paramiko transports used by X2goTerminalSession objects 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 # on Windows: stop the XServer that we evoked 85 if _X2GOCLIENT_OS == 'Windows': 86 for t in threads: 87 if type(t) == xserver.X2goXServer: 88 t.stop_thread() 89 del t 90 91 # on Windows: stop the PulseAudio daemon that we evoked 92 if _X2GOCLIENT_OS == 'Windows': 93 for t in threads: 94 if type(t) == pulseaudio.X2goPulseAudio: 95 t.stop_thread() 96 del t 97 98 for t in threads: 99 if type(t) == guardian.X2goSessionGuardian: 100 t.stop_thread() 101 del t 102 103 gevent.sleep(1) 104 105 if e is not None: 106 raise e 107 108 except KeyboardInterrupt: 109 # do not allow keyboard interrupts during Python X2Go cleanup 110 pass 111 except SystemExit: 112 # neither do we allow SIGTERM signals during cleanup... 113 pass
114