Package nokia :: Module nokiaccm |
[hide private]
[frames] | no frames]

# Source Code for Module nokia.nokiaccm

      1  #============================================================================
      2  #Name        : nokiaccm.py
      3  #Part of     : Helium
      4
      5  #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
      6  #All rights reserved.
      7  #This component and the accompanying materials are made available
      8  #under the terms of the License "Eclipse Public License v1.0"
      9  #which accompanies this distribution, and is available
     10  #at the URL "http://www.eclipse.org/legal/epl-v10.html".
     11  #
     12  #Initial Contributors:
     13  #Nokia Corporation - initial contribution.
     14  #
     15  #Contributors:
     16  #
     17  #Description:
     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  # Uncomment this line to enable logging in this module, or configure logging elsewhere
     34  #logging.basicConfig(level=logging.DEBUG)
     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      # See if a .netrc file can be used
     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              # If settings for a specific database
     68              if database != None:
     69                  netrc_info = netrc_file.authenticators(database)
     70
     71              # if not found just try generic one
     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      # using environment username in case username is not defined.
     93      if username == None:
     94          username = os.environ['USERNAME']
     95
     96      # looking for dbpath using GSCM database
     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      # looking for engine host using GSCM database
    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      # See if any currently running sessions can be used, only if no password submitted, else use a brand new session!
    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          # Looking for router address using GSCM database
    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              # if we have the database name we can switch to the correct Synergy router
    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              # If no existing sessions were available, start a new one
    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