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-2013 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.agent_pid = int(l[0]) 64 self.name = l[1] 65 self.display = int(l[2]) 66 self.hostname = l[3] 67 self.status = l[4] 68 # TODO: turn into datetime object 69 self.date_created = l[5] 70 self.cookie = l[6] 71 self.graphics_port = int(l[8]) 72 self.snd_port = int(l[9]) 73 # TODO: turn into datetime object 74 self.date_suspended = l[10] 75 self.username = l[11] 76 self.sshfs_port = int(l[13]) 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 bool(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 get_session_type(self):
106 """\ 107 Get the session type (i.e. 'D', 'R', 'S' or 'P'). 108 109 @return: session type 110 @rtype: C{str} 111 """ 112 cmd = self.name.split('_')[1] 113 session_type = cmd[2] 114 if session_type == 'R' and self.is_published_applications_provider(): 115 session_type = 'P' 116 return session_type
117
118 - def get_share_mode(self):
119 """\ 120 Get the share mode of a shadow session. 121 122 @return: share mode (0: view-only, 1: full access), C{None} when used for non-desktop-sharing sessions 123 @rtype: C{str} 124 125 """ 126 share_mode = None 127 cmd = self.name.split('_')[1] 128 session_type = cmd[2] 129 if session_type == 'S': 130 share_mode = cmd[3] 131 return share_mode
132
133 - def is_suspended(self):
134 """\ 135 Is this session suspended? 136 137 @return: C{True} if the session is suspended, C{False} otherwise 138 @rtype: C{bool} 139 140 """ 141 return self.status == 'S'
142
143 - def is_desktop_session(self):
144 """\ 145 Is this session a desktop session? 146 147 @return: C{True} if this session is a desktop session, C{False} otherwise 148 @rtype: C{bool} 149 150 """ 151 return self.get_session_type() == 'D'
152
153 - def _parse_x2gostartagent_output(self, x2go_output):
154 """\ 155 Parse x2gostartagent output. 156 157 @param x2go_output: output from ,,x2gostartagent'' command (as list of strings/lines) 158 @type x2go_output: C{list} 159 160 """ 161 try: 162 l = x2go_output.split("\n") 163 self.name = l[3] 164 self.cookie = l[1] 165 self.agent_pid = int(l[2]) 166 self.display = int(l[0]) 167 self.graphics_port = int(l[4]) 168 self.snd_port = int(l[5]) 169 self.sshfs_port = int(l[6]) 170 self.username = '' 171 self.hostname = '' 172 # TODO: we have to see how we fill these fields here... 173 self.date_created = '' 174 self.date_suspended = '' 175 # TODO: presume session is running after x2gostartagent, this could be better 176 self.status = 'R' 177 self.local_container = '' 178 self.remote_container = '' 179 except IndexError, e: 180 # DEBUGGING CODE 181 raise e 182 except ValueError, e: 183 # DEBUGGING CODE 184 raise e
185 186
187 - def initialize(self, x2go_output, username='', hostname='', local_container='', remote_container=''):
188 """\ 189 Setup a a session info data block, includes parsing of X2Go server's C{x2gostartagent} stdout values. 190 191 @param x2go_output: X2Go server's C{x2gostartagent} command output, each value 192 separated by a newline character. 193 @type x2go_output: str 194 @param username: session user name 195 @type username: str 196 @param hostname: hostname of X2Go server 197 @type hostname: str 198 @param local_container: X2Go client session directory for config files, cache and session logs 199 @type local_container: str 200 @param remote_container: X2Go server session directory for config files, cache and session logs 201 @type remote_container: str 202 203 """ 204 self.protect() 205 self._parse_x2gostartagent_output(x2go_output) 206 self.username = username 207 self.hostname = hostname 208 self.local_container = local_container 209 self.remote_container = remote_container
210
211 - def protect(self):
212 """\ 213 Write-protect this session info data structure. 214 215 """ 216 self.protected = True
217
218 - def unprotect(self):
219 """\ 220 Remove write-protection from this session info data structure. 221 222 """ 223 self.protected = False
224
225 - def is_protected(self):
226 """\ 227 228 """ 229 return self.protected
230
231 - def get_status(self):
232 """\ 233 Retrieve the session's status from this session info data structure. 234 235 @return: session status 236 @rtype: C{str} 237 238 """ 239 return self.status
240
241 - def clear(self):
242 """\ 243 Clear all properties of a L{X2GoServerSessionInfo} object. 244 245 """ 246 self.name = '' 247 self.cookie = '' 248 self.agent_pid = '' 249 self.display = '' 250 self.graphics_port = '' 251 self.snd_port = '' 252 self.sshfs_port = '' 253 self.username = '' 254 self.hostname = '' 255 self.date_created = '' 256 self.date_suspended = '' 257 self.status = '' 258 self.local_container = '' 259 self.remote_container = '' 260 self.protected = False
261
262 - def update(self, session_info):
263 """\ 264 Update all properties of a L{X2GoServerSessionInfo} object. 265 266 @param session_info: a provided session info data structure 267 @type session_info: C{X2GoServerSessionInfo*} 268 269 """ 270 if type(session_info) == type(self): 271 for prop in ('graphics_port', 'snd_port', 'sshfs_port', 'date_suspended', 'status', ): 272 if hasattr(session_info, prop): 273 _new = getattr(session_info, prop) 274 _current = getattr(self, prop) 275 if _new != _current: 276 setattr(self, prop, _new)
277 278 __init__ = clear 279 """ Class constructor, identical to L{clear()} method. """
280 281
282 -class X2GoServerSessionListSTDOUT(object):
283 """\ 284 L{X2GoServerSessionListSTDOUT} is used to store all information 285 that is retrieved from a connected X2Go server on a 286 C{X2GoControlSessionBACKEND.list_sessions()} call. 287 288 """
289 - def __init__(self, x2go_output=None, info_backend=X2GoServerSessionInfoSTDOUT):
290 """\ 291 @param x2go_output: X2Go server's C{x2golistsessions} command output, each 292 session separated by a newline character. Session values are separated 293 by Unix Pipe Symbols ('|') 294 @type x2go_output: str 295 @param info_backend: the session info backend to use 296 @type info_backend: C{X2GoServerSessionInfo*} 297 298 """ 299 self.sessions = {} 300 if x2go_output is not None: 301 lines = x2go_output.split("\n") 302 for line in lines: 303 if not line: 304 continue 305 s_info = info_backend() 306 s_info._parse_x2golistsessions_line(line) 307 self.sessions[s_info.name] = s_info
308
309 - def __call__(self):
310 return self.sessions
311
312 - def set_sessions(self, sessions):
313 """\ 314 Set the sessions property directly by parsing a complete data structure. 315 316 """ 317 self.sessions = sessions
318
319 - def get_session_info(self, session_name):
320 """\ 321 Retrieve the session information for C{<session_name>}. 322 323 @param session_name: the queried session name 324 @type session_name: C{str} 325 326 @return: the session info of C{<session_name>} 327 @rtype: C{X2GoServerSessionInfo*} or C{None} 328 329 """ 330 try: 331 return self.sessions[session_name] 332 except KeyError: 333 return None
334
335 - def get_session_with(self, property_name, value, hostname=None):
336 """\ 337 Find session with a given display number on a given host. 338 339 @param property_name: match a session based on this property name 340 @type property_name: C{str} 341 @param value: the resulting session has to match this value for C{<property_name>} 342 @type value: C{str} 343 @param hostname: the result has to match this hostname 344 @type hostname: C{str} 345 346 """ 347 if property_name == 'display': 348 value = value.lstrip(':') 349 if '.' in value: value = value.split('.')[0] 350 351 for session in self.sessions.values(): 352 try: 353 if str(getattr(session, property_name)) == str(value): 354 if hostname is None or session.hostname == hostname: 355 return session 356 except AttributeError: 357 pass
358