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# See if a .netrc file can be used
+ 60ifpassword==Noneorusername==Noneorengine==Noneordbpath==None:
+ 61ifsys.platform=="win32":
+ 62os.environ['HOME']="H:"+os.sep
+ 63_logger.debug('Opening .netrc file')
+ 64try:
+ 65netrc_file=netrc.netrc()
+ 66netrc_info=None
+ 67# If settings for a specific database
+ 68ifdatabase!=None:
+ 69netrc_info=netrc_file.authenticators(database)
+ 70
+ 71# if not found just try generic one
+ 72ifnetrc_info==None:
+ 73netrc_info=netrc_file.authenticators('synergy')
+ 74
+ 75ifnetrc_info!=None:
+ 76(n_username,n_account,n_password)=netrc_info
+ 77ifusername==None:
+ 78username=n_username
+ 79ifpassword==None:
+ 80password=n_password
+ 81ifn_account!=None:
+ 82(n_dbpath,n_engine)=n_account.split('@')
+ 83ifdbpath==Noneandn_dbpathisnotNone:
+ 84_logger.info('Database path set using .netrc (%s)'%n_dbpath)
+ 85dbpath=n_dbpath
+ 86ifengine==Noneandn_engineisnotNone:
+ 87_logger.info('Database engine set using .netrc (%s)'%n_engine)
+ 88engine=n_engine
+ 89exceptIOError:
+ 90_logger.debug('Error accessing .netrc file')
+ 91
+ 92# using environment username in case username is not defined.
+ 93ifusername==None:
+ 94username=os.environ['USERNAME']
+ 95
+ 96# looking for dbpath using GSCM database
+ 97ifdbpath==Noneanddatabase!=None:
+ 98_logger.info('Database path set using the GSCM database.')
+ 99dbpath=nokia.gscm.get_db_path(database)
+100
+101# looking for engine host using GSCM database
+102ifengine==Noneanddatabase!=None:
+103_logger.info('Database engine set using the GSCM database.')
+104engine=nokia.gscm.get_engine_host(database)
+105
+106
+107_sessions=[]
+108# See if any currently running sessions can be used, only if no password submitted, else use a brand new session!
+109ifpassword==Noneandreuse:
+110current_sessions=ccm.running_sessions()
+111forcurrent_sessionincurrent_sessions:
+112ifcurrent_session.dbpath==dbpath:
+113returncurrent_session
+114else:
+115ifccm.CCM_BIN==None:
+116raiseccm.CCMException("Could not find CM/Synergy executable in the path.")
+117# Looking for router address using GSCM database
+118router_address=None
+119ifdatabase==Noneanddbpath!=None:
+120database=os.path.basename(dbpath)
+121
+122lock=fileutils.Lock(ccm.CCM_SESSION_LOCK)
+123try:
+124lock.lock(wait=True)
+125# if we have the database name we can switch to the correct Synergy router
+126ifdatabase!=None:
+127router_address=nokia.gscm.get_router_address(database)
+128ifsys.platform=="win32"androuter_address!=None:
+129routerfile=open(os.path.join(os.path.dirname(ccm.CCM_BIN),"../etc/_router.adr"),'r')
+130current_router=routerfile.read().strip()
+131routerfile.close()
+132ifcurrent_router!=router_address.strip():
+133_logger.info('Updating %s'%(os.path.normpath(os.path.join(os.path.dirname(ccm.CCM_BIN),"../etc/_router.adr"))))
+134routerfile=open(os.path.join(os.path.dirname(ccm.CCM_BIN),"../etc/_router.adr"),"w+")
+135routerfile.write("%s\n"%router_address)
+136routerfile.close()
+137
+138# If no existing sessions were available, start a new one
+139new_session=ccm.Session.start(username,password,engine,dbpath)
+140lock.unlock()
+141returnnew_session
+142finally:
+143lock.unlock()
+144raiseccm.CCMException("Cannot open session for user '%s'"%username)
+