Package x2go :: Package backends :: Package info :: Module _stdout
[frames] | no frames]

Source Code for Module x2go.backends.info._stdout

  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  X2goServerSessionList and X2goServerSessionInfo classes - data handling for  
 22  X2Go server sessions. 
 23   
 24  This backend handles X2Go server implementations that respond with session infos  
 25  via server-side STDOUT. 
 26   
 27  """ 
 28  __NAME__ = 'x2goserversessioninfo-pylib' 
 29   
 30   
 31  # modules 
 32  import types 
 33  import re 
 34   
 35  import x2go.defaults as defaults 
 36   
37 -class X2goServerSessionInfoSTDOUT(object):
38 """\ 39 L{X2goServerSessionInfo} is used to store all information 40 that is retrieved from the connected X2Go server on 41 C{X2goTerminalSessionBACKEND.start()} resp. C{X2goTerminalSessionBACKEND.resume()}. 42 43 """
44 - def __str__(self):
45 return self.name
46 - def __repr__(self):
47 result = 'X2goServerSessionInfoSTDOUT(' 48 for p in dir(self): 49 if '__' in p or not p in self.__dict__ or type(p) is types.InstanceType: continue 50 result += p + '=' + str(self.__dict__[p]) +',' 51 return result.strip(',') + ')'
52
53 - def _parse_x2golistsessions_line(self, x2go_output):
54 """\ 55 Parse a single line of X2go's listsessions output. 56 57 @param x2go_output: output from ,,x2golistsessions'' command (as list of strings/lines) 58 @type x2go_output: C{list} 59 60 """ 61 try: 62 l = x2go_output.split("|") 63 self.name = l[1] 64 self.cookie = l[6] 65 self.agent_pid = int(l[0]) 66 self.display = int(l[2]) 67 self.status = l[4] 68 self.graphics_port = int(l[8]) 69 self.snd_port = int(l[9]) 70 self.sshfs_port = int(l[13]) 71 self.username = l[11] 72 self.hostname = l[3] 73 # TODO: turn into datetime object 74 self.date_created = l[5] 75 # TODO: turn into datetime object 76 self.date_suspended = l[10] 77 self.local_container = '' 78 except IndexError, e: 79 # DEBUGGING CODE 80 raise e 81 except ValueError, e: 82 # DEBUGGING CODE 83 raise e
84
86 """\ 87 Detect from session info if this session is a published applications provider. 88 89 @return: returns C{True} if this session is a published applications provider 90 @rtype: C{bool} 91 92 """ 93 return re.match('.*_stRPUBLISHED_.*', self.name)
94
95 - def is_running(self):
96 """\ 97 Is this session running? 98 99 @return: C{True} if the session is running, C{False} otherwise 100 @rtype: C{bool} 101 102 """ 103 return self.status == 'R'
104
105 - def is_suspended(self):
106 """\ 107 Is this session suspended? 108 109 @return: C{True} if the session is suspended, C{False} otherwise 110 @rtype: C{bool} 111 112 """ 113 return self.status == 'S'
114
115 - def is_desktop_session(self):
116 """\ 117 Is this session a desktop session? 118 119 @return: C{True} if this session is a desktop session, C{False} otherwise 120 @rtype: C{bool} 121 122 """ 123 _desktop_sessions = defaults.X2GO_DESKTOPSESSIONS.keys() 124 _regexp_desktop_sessions = '(%s)' % "|".join(_desktop_sessions) 125 return re.match('.*_stD%s_.*' % _regexp_desktop_sessions, self.name)
126
127 - def _parse_x2gostartagent_output(self, x2go_output):
128 """\ 129 Parse x2gostartagent output. 130 131 @param x2go_output: output from ,,x2gostartagent'' command (as list of strings/lines) 132 @type x2go_output: C{list} 133 134 """ 135 try: 136 l = x2go_output.split("\n") 137 self.name = l[3] 138 self.cookie = l[1] 139 self.agent_pid = int(l[2]) 140 self.display = int(l[0]) 141 self.graphics_port = int(l[4]) 142 self.snd_port = int(l[5]) 143 self.sshfs_port = int(l[6]) 144 self.username = '' 145 self.hostname = '' 146 # TODO: we have to see how we fill these fields here... 147 self.date_created = '' 148 self.date_suspended = '' 149 # TODO: presume session is running after x2gostartagent, this could be better 150 self.status = 'R' 151 self.local_container = '' 152 self.remote_container = '' 153 except IndexError, e: 154 # DEBUGGING CODE 155 raise e 156 except ValueError, e: 157 # DEBUGGING CODE 158 raise e
159 160
161 - def initialize(self, x2go_output, username='', hostname='', local_container='', remote_container=''):
162 """\ 163 Setup a a session info data block, includes parsing of X2Go server's C{x2gostartagent} stdout values. 164 165 @param x2go_output: X2Go server's C{x2gostartagent} command output, each value 166 separated by a newline character. 167 @type x2go_output: str 168 @param username: session user name 169 @type username: str 170 @param hostname: hostname of X2Go server 171 @type hostname: str 172 @param local_container: X2Go client session directory for config files, cache and session logs 173 @type local_container: str 174 @param remote_container: X2Go server session directory for config files, cache and session logs 175 @type remote_container: str 176 177 """ 178 self.protect() 179 self._parse_x2gostartagent_output(x2go_output) 180 self.username = username 181 self.hostname = hostname 182 self.local_container = local_container 183 self.remote_container = remote_container
184
185 - def protect(self):
186 """\ 187 Write-protect this session info data structure. 188 189 """ 190 self.protected = True
191
192 - def unprotect(self):
193 """\ 194 Remove write-protection from this session info data structure. 195 196 """ 197 self.protected = False
198
199 - def is_protected(self):
200 """\ 201 202 """ 203 return self.protected
204
205 - def get_status(self):
206 """\ 207 Retrieve the session's status from this session info data structure. 208 209 @return: session status 210 @rtype: C{str} 211 212 """ 213 return self.status
214
215 - def clear(self):
216 """\ 217 Clear all properties of a L{X2goServerSessionInfo} object. 218 219 """ 220 self.name = '' 221 self.cookie = '' 222 self.agent_pid = '' 223 self.display = '' 224 self.graphics_port = '' 225 self.snd_port = '' 226 self.sshfs_port = '' 227 self.username = '' 228 self.hostname = '' 229 self.date_created = '' 230 self.date_suspended = '' 231 self.status = '' 232 self.local_container = '' 233 self.remote_container = '' 234 self.protected = False
235
236 - def update(self, session_info):
237 """\ 238 Update all properties of a L{X2goServerSessionInfo} object. 239 240 @param session_info: a provided session info data structure 241 @type session_info: C{X2goServerSessionInfo*} 242 243 """ 244 if type(session_info) == type(self): 245 for prop in ('graphics_port', 'snd_port', 'sshfs_port', 'date_suspended', 'status', ): 246 if hasattr(session_info, prop): 247 _new = getattr(session_info, prop) 248 _current = getattr(self, prop) 249 if _new != _current: 250 setattr(self, prop, _new)
251 252 __init__ = clear 253 """ Class constructor, identical to L{clear()} method. """
254 255
256 -class X2goServerSessionListSTDOUT(object):
257 """\ 258 L{X2goServerSessionListSTDOUT} is used to store all information 259 that is retrieved from a connected X2Go server on a 260 C{X2goControlSessionBACKEND.list_sessions()} call. 261 262 """
263 - def __init__(self, x2go_output, info_backend=X2goServerSessionInfoSTDOUT):
264 """\ 265 @param x2go_output: X2Go server's C{x2golistsessions} command output, each 266 session separated by a newline character. Session values are separated 267 by Unix Pipe Symbols ('|') 268 @type x2go_output: str 269 @param info_backend: the session info backend to use 270 @type info_backend: C{X2goServerSessionInfo*} 271 272 """ 273 self.sessions = {} 274 lines = x2go_output.split("\n") 275 for line in lines: 276 if not line: 277 continue 278 s_info = info_backend() 279 s_info._parse_x2golistsessions_line(line) 280 self.sessions[s_info.name] = s_info
281
282 - def __call__(self):
283 return self.sessions
284
285 - def get_session_info(self, session_name):
286 """\ 287 Retrieve the session information for C{<session_name>}. 288 289 @param session_name: the queried session name 290 @type session_name: C{str} 291 292 @return: the session info of C{<session_name>} 293 @rtype: C{X2goServerSessionInfo*} or C{None} 294 295 """ 296 try: 297 return self.sessions[session_name] 298 except KeyError: 299 return None
300