1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """ Nokia specific interface to Synergy sessions. """
21
22 import logging
23 import netrc
24 import os
25 import os.path
26 import sys
27 import fileutils
28
29 import ccm
30 import nokia.gscm
31
32
33
34
35 _logger = logging.getLogger("nokia.nokiaccm")
36
37
38 -def open_session(username=None, password=None, engine=None, dbpath=None, database=None, reuse=True):
39 """Provides a Session object.
40
41 Attempts to return a Session, based either on existing Synergy
42 sessions or by creating a new one.
43
44 - If a .netrc file can be found on the user's personal drive,
45 that will be read to obtain Synergy login information if it
46 is defined there. This will be used to fill in any missing
47 parameters not passed in the call to open_session().
48
49 The format of the .netrc file entries should be:
50
51 machine synergy login USERNAME password foobar account DATABASE_PATH@SERVER
52
53 If the details refer to a specific database, the machine can be the database name,
54 instead of "synergy".
55 - If an existing session is running that matches the supplied
56 parameters, it will reuse that.
57
58 """
59
60 if password == None or username == None or engine == None or dbpath == None:
61 if sys.platform == "win32":
62 os.environ['HOME'] = "H:" + os.sep
63 _logger.debug('Opening .netrc file')
64 try:
65 netrc_file = netrc.netrc()
66 netrc_info = None
67
68 if database != None:
69 netrc_info = netrc_file.authenticators(database)
70
71
72 if netrc_info == None:
73 netrc_info = netrc_file.authenticators('synergy')
74
75 if netrc_info != None:
76 (n_username, n_account, n_password) = netrc_info
77 if username == None:
78 username = n_username
79 if password == None:
80 password = n_password
81 if n_account != None:
82 (n_dbpath, n_engine) = n_account.split('@')
83 if dbpath == None and n_dbpath is not None:
84 _logger.info('Database path set using .netrc (%s)' % n_dbpath)
85 dbpath = n_dbpath
86 if engine == None and n_engine is not None:
87 _logger.info('Database engine set using .netrc (%s)' % n_engine)
88 engine = n_engine
89 except IOError:
90 _logger.debug('Error accessing .netrc file')
91
92
93 if username == None:
94 username = os.environ['USERNAME']
95
96
97 if dbpath == None and database != None:
98 _logger.info('Database path set using the GSCM database.')
99 dbpath = nokia.gscm.get_db_path(database)
100
101
102 if engine == None and database != None:
103 _logger.info('Database engine set using the GSCM database.')
104 engine = nokia.gscm.get_engine_host(database)
105
106
107 _sessions = []
108
109 if password == None and reuse:
110 current_sessions = ccm.running_sessions()
111 for current_session in current_sessions:
112 if current_session.dbpath == dbpath:
113 return current_session
114 else:
115 if ccm.CCM_BIN == None:
116 raise ccm.CCMException("Could not find CM/Synergy executable in the path.")
117
118 router_address = None
119 if database == None and dbpath != None:
120 database = os.path.basename(dbpath)
121
122 lock = fileutils.Lock(ccm.CCM_SESSION_LOCK)
123 try:
124 lock.lock(wait=True)
125
126 if database != None:
127 router_address = nokia.gscm.get_router_address(database)
128 if sys.platform == "win32" and router_address != None:
129 routerfile = open(os.path.join(os.path.dirname(ccm.CCM_BIN), "../etc/_router.adr"), 'r')
130 current_router = routerfile.read().strip()
131 routerfile.close()
132 if current_router != router_address.strip():
133 _logger.info('Updating %s' % (os.path.normpath(os.path.join(os.path.dirname(ccm.CCM_BIN), "../etc/_router.adr"))))
134 routerfile = open(os.path.join(os.path.dirname(ccm.CCM_BIN), "../etc/_router.adr"), "w+")
135 routerfile.write("%s\n" % router_address)
136 routerfile.close()
137
138
139 new_session = ccm.Session.start(username, password, engine, dbpath)
140 lock.unlock()
141 return new_session
142 finally:
143 lock.unlock()
144 raise ccm.CCMException("Cannot open session for user '%s'" % username)
145