sbsv2/raptor/python/generic_path.py
branchfix
changeset 400 554cc189839f
parent 3 e1eecf4d390d
equal deleted inserted replaced
390:421e376bfce4 400:554cc189839f
     1 #
     1 #
     2 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 # Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 # All rights reserved.
     3 # All rights reserved.
     4 # This component and the accompanying materials are made available
     4 # This component and the accompanying materials are made available
     5 # under the terms of the License "Eclipse Public License v1.0"
     5 # under the terms of the License "Eclipse Public License v1.0"
     6 # which accompanies this distribution, and is available
     6 # which accompanies this distribution, and is available
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
    17 
    17 
    18 import os
    18 import os
    19 import sys
    19 import sys
    20 import re
    20 import re
    21 import types
    21 import types
       
    22 import ctypes
    22 
    23 
    23 # are we on windows, and if so what is the current drive letter
    24 # are we on windows, and if so what is the current drive letter
    24 isWin = sys.platform.lower().startswith("win")
    25 isWin = sys.platform.lower().startswith("win")
    25 if isWin:
    26 if isWin:
    26 	drive = re.match('^([A-Za-z]:)',os.getcwd()).group(0)
    27 	drive = re.match('^([A-Za-z]:)',os.getcwd()).group(0)
   263 
   264 
   264 			return self.path.replace("/", "\\")
   265 			return self.path.replace("/", "\\")
   265 
   266 
   266 		return self.path
   267 		return self.path
   267 
   268 
   268 
   269 	def GetSpaceSafePath(self):
       
   270 		"""Returns a version of the path where spaces don't interfere with shell interpretation.
       
   271 		
       
   272 		This functionality only applies to Windows - paths containing spaces are assumed to be problematic
       
   273 		on non-Windows platforms.
       
   274 		
       
   275 		On Windows, the path is returned in Windows-specific 8.3 short path form - tilde are used to replace
       
   276 		spaces and fit path elements within 8.3 requirements.  As 8.3 format paths are not guaranteed to be
       
   277 		supported on all Windows installs, and can only be calculated if they exist, a newly formated path is
       
   278 		only returned if it is returned by the Windows API and tested to exist.
       
   279 		"""
       
   280 		
       
   281 		if not isWin:
       
   282 			return None
       
   283 		
       
   284 		from ctypes.wintypes import DWORD, LPSTR, MAX_PATH
       
   285 
       
   286 		GetShortPathNameA = ctypes.windll.kernel32.GetShortPathNameA
       
   287 		GetShortPathNameA.restype = DWORD
       
   288 		GetShortPathNameA.argtypes = LPSTR, LPSTR, DWORD
       
   289 		
       
   290 		buffer = ctypes.create_string_buffer(MAX_PATH)
       
   291 		GetShortPathNameA(self.path, buffer, MAX_PATH)
       
   292 		
       
   293 		spacesafe = buffer.value
       
   294 		
       
   295 		if not spacesafe or not os.path.exists(spacesafe):
       
   296 			return None
       
   297 		
       
   298 		return spacesafe
       
   299 		
   269 # Module functions
   300 # Module functions
   270 
   301 
   271 def Join(*arguments):
   302 def Join(*arguments):
   272 	"""Concatenate the given list to make a generic path object. 
   303 	"""Concatenate the given list to make a generic path object. 
   273 	
   304