buildframework/helium/sf/python/pythoncore/lib/nokia/gscm.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : gscm.py 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
""" Wrapper module that get CCM info using GSCM framework. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import os
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import subprocess
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import pkg_resources
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
import tempfile
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
# Uncomment this line to enable logging in this module, or configure logging elsewhere
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
#logging.basicConfig(level=logging.DEBUG)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
_logger = logging.getLogger("gscm")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
def _execute(command):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
    """ Runs a command and returns the result data. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    output = process.stdout.read()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    process.poll()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    status = process.returncode
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
    return (output, status)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
def __get_gscm_info(method, dbname):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    """ Generic method that call function 'method' on GSCM wrapper script. """
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    45
    (f_desc, filename) = tempfile.mkstemp()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    46
    f_file = os.fdopen(f_desc, 'w')
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    47
    f_file.write(pkg_resources.resource_string(__name__, "get_gscm_info.pl"))# pylint: disable=E1101
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    48
    f_file.close()
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    command = "perl " + filename
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    50
    command += " %s %s" % (method, dbname)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
    _logger.debug("Running command: %s" % command)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
    (output, status) = _execute(command)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    _logger.debug("Status: %s" % status)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    _logger.debug("Output: %s" % output)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    if status == 0 or status == None and not ("Can't locate" in output):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        return output.strip()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    if not 'HLM_SUBCON' in os.environ:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
        raise Exception("Error retrieving get_db_path info for '%s' database.\nOUTPUT:%s" % (dbname, output.strip()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    return None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
def get_db_path(dbname):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    """ Returns the database path for dbname database. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
    _logger.debug("get_db_path: %s" % dbname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    return __get_gscm_info('get_db_path', dbname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
def get_router_address(dbname):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
    """ Returns the database router address for dbname database. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
    _logger.debug("get_router_address: %s" % dbname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
    return __get_gscm_info('get_router_address', dbname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
def get_engine_host(dbname):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
    """ Returns the database engine host for dbname database. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
    _logger.debug("get_engine_host: %s" % dbname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    return __get_gscm_info('get_engine_host', dbname)