sbsv2/raptor/python/raptor_utilities.py
changeset 2 39c28ec933dd
child 13 c327db0664bb
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 #
       
     2 # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # raptor_utilities module
       
    16 # Useful wrapper functions and classes used in Raptor processing
       
    17 #
       
    18 
       
    19 import generic_path
       
    20 import os.path
       
    21 import re
       
    22 import sys
       
    23 
       
    24 dosSlashRegEx = re.compile(r'\\')
       
    25 unixSlashRegEx = re.compile(r'/')
       
    26 dosDriveRegEx = re.compile("^([A-Za-z]{1}):")
       
    27 
       
    28 def getOSPlatform():
       
    29 	return sys.platform.lower()
       
    30 
       
    31 def getOSFileSystem():
       
    32 	if getOSPlatform().startswith("win"):
       
    33 		return "cygwin"
       
    34 	else:
       
    35 		return "unix"
       
    36 
       
    37 def convertToUnixSlash(aReference):
       
    38 	return dosSlashRegEx.sub(r'/', aReference)
       
    39 
       
    40 def convertToDOSSlash(aReference):
       
    41 	return unixSlashRegEx.sub(r'\\', aReference)
       
    42 
       
    43 def absPathFromPath(aPathRoot, aReference):
       
    44 	pathRoot = convertToUnixSlash(aPathRoot)
       
    45 	reference = convertToUnixSlash(aReference)
       
    46 	
       
    47 	if os.path.isabs(reference):
       
    48 		reference = reference.lstrip(r'/')
       
    49 	
       
    50 	joined = os.path.join(pathRoot, reference)
       
    51 	
       
    52 	return os.path.abspath(joined)
       
    53    
       
    54 
       
    55 def absPathFromFile(aFileRoot, aReference):
       
    56 	pathRoot = os.path.dirname(aFileRoot)
       
    57 	return absPathFromPath(pathRoot, aReference)
       
    58 
       
    59 def sanitise(aPotentialFilename):
       
    60 	"Take a string and return a version suitable for use as a filename."
       
    61 	return re.sub("(\\\\|\/|:|;| )", "_", aPotentialFilename)
       
    62 
       
    63 def resolveSymbianPath(aFileRoot, aReference, aMainType="", aSubType="", aEPOCROOT="$(EPOCROOT)"):
       
    64 	""" Convert raw Symbian metadata path/file references into absolute makefile references, or list of references
       
    65 
       
    66 	<drive>-prefix	: maps to an emulated drive depending on the following cases:
       
    67 							(a) If the drive is C:, it maps to the *two* locations
       
    68 								$(EPOCROOT)/epoc32/data/<drive>/<path> and
       
    69 								$(EPOCROOT)/epoc32/winscw/<drive>/<path>
       
    70 							(b) If the drive is A:, B:, or D: to Z:, it maps to the *three* locations
       
    71 								$(EPOCROOT)/epoc32/data/<drive>/<path> and
       
    72 								$(EPOCROOT)/epoc32/release/winscw/udeb/<drive>/<path> and
       
    73 								$(EPOCROOT)/epoc32/release/winscw/urel/<drive>/<path>
       
    74 	Absolute 		: true absolute if:
       
    75 							(a) PRJ_*EXPORTS destination or DEFFILE location and
       
    76 							(b) not starting with an 'epoc32'
       
    77 						otherwise relative to $(EPOCROOT)
       
    78 	Relative 		: relative to $(EPOCROOT)/epoc32/include if:
       
    79 							(a) PRJ_EXPORTS destination and
       
    80 							(b) not a :zip statement,
       
    81 					  relative to $(EPOCROOT) if:
       
    82 							(a) PRJ_(TEST)EXPORTS destination and
       
    83 							(b) a :zip statement,
       
    84 						otherwise relative to aFileRoot
       
    85 	|-prefix 		: relative to aFileRoot
       
    86 	+-prefix 		: relative to $(EPOCROOT)/epoc32"""
       
    87 	
       
    88 	# Both reference and fileroot can have backslashes - so convert them.
       
    89 	reference = convertToUnixSlash(aReference)
       
    90 	fileroot = convertToUnixSlash(aFileRoot)
       
    91 	
       
    92 	# Remove Trailing backslashes so that the expansions doesnt mess up the shell
       
    93 	if reference.endswith('/') and len(reference) > 1:
       
    94 		reference = reference.rstrip('/')
       
    95 
       
    96 	emulatedDrive = dosDriveRegEx.match(reference)	
       
    97 	if emulatedDrive:
       
    98 		# Emulated drive C:/ Z:/ and the like
       
    99 		# C: drive 
       
   100 		if reference.lower().startswith("c"):
       
   101 			resolvedPath = []
       
   102 			resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/data/'+emulatedDrive.group(1), reference))
       
   103 			resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/winscw/'+emulatedDrive.group(1), reference))
       
   104 		else: # Other letters: A, B and D to Z
       
   105 			resolvedPath = []
       
   106 			resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/data/'+emulatedDrive.group(1), reference))
       
   107 			resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/release/winscw/udeb/'+emulatedDrive.group(1), reference))
       
   108 			resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/release/winscw/urel/'+emulatedDrive.group(1), reference))
       
   109 	elif os.path.isabs(reference):
       
   110 		# Absolute
       
   111 		if re.search("(DEFFILE|PRJ_(TEST)?EXPORTS)", aMainType, re.I) and not re.search("^\/epoc32\/", reference, re.I):
       
   112 			# Ensures prepending of drive if on Windows
       
   113 			resolvedPath = os.path.abspath(reference)
       
   114 		else:
       
   115 			resolvedPath = aEPOCROOT + reference
       
   116 		
       
   117 	elif reference.startswith("+"):
       
   118 		# '+' prefix
       
   119 		reference = reference.lstrip(r'+')
       
   120 		resolvedPath = aEPOCROOT + '/epoc32'+reference
       
   121 	elif reference.startswith("|"):
       
   122 		# '|' prefix
       
   123 		reference = reference.lstrip(r'|')
       
   124 		resolvedPath = absPathFromFile(fileroot, reference)
       
   125 	else:
       
   126 		# Relative
       
   127 		if aMainType == "PRJ_EXPORTS" and aSubType != ":zip":
       
   128 			resolvedPath = aEPOCROOT + '/epoc32/include/'+reference
       
   129 		elif aSubType == ":zip":
       
   130 			resolvedPath = aEPOCROOT + '/' + reference
       
   131 		else:
       
   132 			resolvedPath = absPathFromFile(fileroot, aReference)
       
   133 	
       
   134 	if isinstance(resolvedPath, list):
       
   135 		# In this case, this is a list of export destinations, 
       
   136 		makefilePath = map(lambda x: str(generic_path.Path(x)), resolvedPath)
       
   137 	else:
       
   138 		makefilePath = str(generic_path.Path(resolvedPath))
       
   139 	
       
   140 	return makefilePath # Note this is either a list of strings, or a single string
       
   141 
       
   142 
       
   143 class ExternalTool(object):
       
   144 	""" Generic wrapper for an external tool
       
   145 	
       
   146 	Provides the basic means to wrap up a tool that is external to Raptor with a
       
   147 	consistent interface for both invocation and the capture of output."""
       
   148 	
       
   149 	def __init__(self, aTool):
       
   150 		self.__Tool = aTool
       
   151 		self.__Output = []
       
   152 
       
   153 	def call(self, aArgs):		
       
   154 		print "RUNNNING: %s %s" %(self.__Tool, aArgs)
       
   155 		(input, output) = os.popen2(self.__Tool + " " + aArgs)
       
   156 		self.__Output = output.read()
       
   157 		return output.close() 
       
   158 	
       
   159 	def getTool(self):
       
   160 		return self.__Tool
       
   161 
       
   162 	def getOutput(self):
       
   163 		return self.__Output
       
   164 		
       
   165 	def getOutputLines(self):
       
   166 		return self.__Output.split("\n")
       
   167 
       
   168 
       
   169 class NullLog(object):
       
   170 	""" If your class has these methods then it can act as a log """
       
   171 	def Info(self, format, *extras):
       
   172 		"Send an information message to the configured channel"
       
   173 		return
       
   174 
       
   175 	def ClockInfo(self):
       
   176 		"Print a timestamp in seconds"
       
   177 		return
       
   178 
       
   179 	def Debug(self, format, *extras):
       
   180 		"Send a debugging message to the configured channel"
       
   181 		return
       
   182 
       
   183 	def Warn(self, format, *extras):
       
   184 		"Send a warning message to the configured channel"
       
   185 		return
       
   186 
       
   187 	def Error(self, format, *extras):
       
   188 		"Send an error message to the configured channel"
       
   189 		return
       
   190 
       
   191 nulllog = NullLog()