releasing/blocks/framework/src/SymbianUtils/Readelf.py
author kelvzhu
Thu, 02 Sep 2010 15:02:14 +0800
changeset 632 934f9131337b
permissions -rw-r--r--
Delivery Blocks to SF

#
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
# This component and the accompanying materials are made available
# under the terms of "Eclipse Public License v1.0"
# which accompanies this distribution, and is available
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
#
# Initial Contributors:
# Nokia Corporation - initial contribution.
#
# Contributors:
#
# Description:
# Extract library API from ELF-files
#

import os
import sys
import re

from SymbianUtils import SymbianUtilsError

class Readelf(object):
    '''
    Methods for extracting a library API. Blocks.Packaging.ComponentBuilder.Rule
    and Blocks.Packaging.PackageModel.API make use of Readelf.
    '''
    class ConfigurationError(SymbianUtilsError):
        ''' Missing required binaries '''

    class Failed(SymbianUtilsError):
        ''' The external binary returned an error '''

    if sys.platform == "win32":
        readelf = os.path.normpath(os.path.join(os.path.dirname(__file__), "bin", "readelf.exe"))
    elif "linux" in sys.platform:
        readelf = "/usr/bin/readelf"

    if not os.path.isfile(readelf):
        raise ConfigurationError, "Cannot find readelf command (%s)" % readelf

    @classmethod
    def getInterface(cls, path):
        '''

        @param path: The file to inspect
        @return: The interface - relevant parts from the symbol table as read from file by I{readelf}
        @rtype: Dictionary
        '''
        path = os.path.normpath(path)
        api = {}
        binary = cls.readelf + " -Ws " + path + " 2>&1"
        rex = re.compile(r'^\s*(\d+):\s+\d+\s+\d+\s+FUNC\s+GLOBAL\s+DEFAULT\s+\d+\s+(.*)@@.*')
        fo = os.popen(binary, "r", -1)
        for line in fo:
            match = rex.search(line)
            if match:
                api[match.group(1)] = match.group(2)
        if fo.close():
            # "Not an ELF file", "No such file", etc.
            raise cls.Failed("readelf unable to get interface for '%s': %s" % (path, line.strip()))
        return api