Package flumotion :: Package admin :: Module connections
[hide private]

Source Code for Module flumotion.admin.connections

  1  # -*- Mode: Python; fill-column: 80 -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with th 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22   
 23  import os 
 24  from xml.dom import minidom, Node 
 25   
 26  from flumotion.configure import configure 
 27  from flumotion.common import connection, errors, log 
 28  from flumotion.twisted import pb as fpb 
 29   
30 -def get_recent_connections():
31 def parse_connection(f): 32 tree = minidom.parse(f) 33 state = {} 34 for n in [x for x in tree.documentElement.childNodes 35 if x.nodeType != Node.TEXT_NODE 36 and x.nodeType != Node.COMMENT_NODE]: 37 state[n.nodeName] = n.childNodes[0].wholeText 38 state['port'] = int(state['port']) 39 state['use_insecure'] = (state['use_insecure'] != '0') 40 authenticator = fpb.Authenticator(username=state['user'], 41 password=state['passwd']) 42 return connection.PBConnectionInfo(state['host'], state['port'], 43 not state['use_insecure'], 44 authenticator)
45 46 try: 47 # DSU, or as perl folks call it, a Schwartz Transform 48 files = os.listdir(configure.registrydir) 49 files = [os.path.join(configure.registrydir, f) for f in files] 50 files = [(os.stat(f).st_mtime, f) for f in files 51 if f.endswith('.connection')] 52 files.sort() 53 files.reverse() 54 55 ret = [] 56 for f in [x[1] for x in files]: 57 try: 58 state = parse_connection(f) 59 ret.append({'name': str(state), 60 'file': f, 61 'info': state}) 62 except Exception, e: 63 log.warning('connections', 'Error parsing %s: %r', f, e) 64 return ret 65 except OSError, e: 66 log.warning('connections', 'Error: %s: %s', e.strerror, e.filename) 67 return [] 68
69 -def parsePBConnectionInfo(managerString, use_ssl=True, 70 defaultPort=configure.defaultSSLManagerPort):
71 """The same as L{flumotion.common.connection.parsePBConnectionInfo}, 72 but fills in missing information from the recent connections cache 73 if possible. 74 """ 75 recent = get_recent_connections() 76 if not managerString: 77 if recent: 78 return recent[0]['info'] 79 else: 80 raise errors.OptionError('No string given and no recent ' 81 'connections to use') 82 83 info = connection.parsePBConnectionInfo(managerString, username=None, 84 password=None, 85 port=defaultPort, 86 use_ssl=use_ssl) 87 88 def compatible(i1, i2): 89 if i1.port and i1.port != i2.port: 90 return False 91 if i1.use_ssl != i2.use_ssl: 92 return False 93 a1, a2 = i1.authenticator, i2.authenticator 94 if a1.username and a1.username != a2.username: 95 return False 96 if a1.password and a1.password != a2.password: 97 return False 98 return True
99 100 if not info.authenticator.username: 101 for c in recent: 102 recent = c['info'] 103 if compatible(info, recent): 104 info.authenticator.username = recent.authenticator.username 105 info.authenticator.password = recent.authenticator.password 106 break 107 elif not info.authenticator.password: 108 for c in recent: 109 recent = c['info'] 110 if compatible(info, recent): 111 info.authenticator.password = recent.authenticator.password 112 break 113 if not (info.authenticator.username and info.authenticator.password): 114 raise errors.OptionError('You are connecting to %s for the ' 115 'first time; please specify a user and ' 116 'password (e.g. user:test@%s).' 117 % (managerString, managerString)) 118 else: 119 return info 120