author | timothy.murphy@nokia.com |
Tue, 11 May 2010 20:22:35 +0100 | |
branch | fix |
changeset 549 | d633be326c9f |
parent 400 | 554cc189839f |
permissions | -rw-r--r-- |
3 | 1 |
# |
400
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
2 |
# Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). |
3 | 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 |
# generic_path module |
|
16 |
# |
|
17 |
||
18 |
import os |
|
19 |
import sys |
|
20 |
import re |
|
21 |
import types |
|
400
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
22 |
import ctypes |
3 | 23 |
|
24 |
# are we on windows, and if so what is the current drive letter |
|
25 |
isWin = sys.platform.lower().startswith("win") |
|
26 |
if isWin: |
|
27 |
drive = re.match('^([A-Za-z]:)',os.getcwd()).group(0) |
|
28 |
||
29 |
# regex for "bare" drive letters |
|
30 |
driveRE = re.compile('^[A-Za-z]:$') |
|
31 |
||
32 |
# Base class |
|
33 |
||
34 |
class Path: |
|
35 |
"""This class represents a file path. |
|
36 |
||
37 |
A generic path object supports operations without needing to know |
|
38 |
about Windows and Linux differences. The standard str() function can |
|
39 |
obtain a string version of the path in Local format for use by |
|
40 |
platform-specific functions (file opening for example). |
|
41 |
||
42 |
We use forward slashes as path separators (even on Windows). |
|
43 |
||
44 |
For example, |
|
45 |
||
46 |
path1 = generic_path.Path("/foo") |
|
47 |
path2 = generic_path.Path("bar", "bing.bang") |
|
48 |
||
49 |
print str(path1.Append(path2)) |
|
50 |
||
51 |
Prints /foo/bar/bing.bang on Linux |
|
52 |
Prints c:/foo/bar/bing.bang on Windows (if c is the current drive) |
|
53 |
""" |
|
54 |
||
55 |
def __init__(self, *arguments): |
|
56 |
"""construct a path from a list of path elements""" |
|
57 |
||
58 |
if len(arguments) == 0: |
|
59 |
self.path = "" |
|
60 |
return |
|
61 |
||
62 |
list = [] |
|
63 |
for i,arg in enumerate(arguments): |
|
64 |
if isWin: |
|
65 |
if i == 0: |
|
66 |
# If the first element starts with \ or / then we will |
|
67 |
# add the current drive letter to make a fully absolute path |
|
68 |
if arg.startswith("\\\\"): |
|
69 |
list.append(arg) # A UNC path - don't mess with it |
|
70 |
elif arg.startswith("\\") or arg.startswith("/"): |
|
71 |
list.append(drive + arg) |
|
72 |
# If the first element is a bare drive then dress it with a \ |
|
73 |
# temporarily otherwise "join" will not work properly. |
|
74 |
elif driveRE.match(arg): |
|
75 |
list.append(arg + "\\") |
|
76 |
# nothing special about the first element |
|
77 |
else: |
|
78 |
list.append(arg) |
|
79 |
else: |
|
80 |
if arg.startswith("\\\\"): |
|
81 |
raise ValueError("non-initial path components must not start with \\\\ : %s" % arg) |
|
82 |
else: |
|
83 |
list.append(arg) |
|
84 |
if ";" in arg: |
|
85 |
raise ValueError("An individual windows Path may not contain ';' : %s" % arg) |
|
86 |
else: |
|
87 |
list.append(arg) |
|
88 |
||
89 |
self.path = os.path.join(*list) |
|
90 |
||
91 |
# normalise to avoid nastiness with dots and multiple separators |
|
92 |
# but do not normalise "" as it will become "." |
|
93 |
if self.path != "": |
|
94 |
self.path = os.path.normpath(self.path) |
|
95 |
||
96 |
# always use forward slashes as separators |
|
97 |
self.path = self.path.replace("\\", "/") |
|
98 |
||
99 |
# remove trailing slashes unless we are just / |
|
100 |
if self.path != "/": |
|
101 |
self.path = self.path.rstrip("/") |
|
102 |
||
103 |
def __str__(self): |
|
104 |
return self.path |
|
105 |
||
106 |
def GetNeutralStr(self): |
|
107 |
"""return the path as a string that could be included in other paths.""" |
|
108 |
return self.path.replace(":","").replace("/","") |
|
109 |
||
110 |
def GetLocalString(self): |
|
111 |
"""return a string in the local file-system format. |
|
112 |
||
113 |
e.g. C:/tmp on Windows or /C/tmp on Linux""" |
|
114 |
return self.path |
|
115 |
||
116 |
def isAbsolute(self): |
|
117 |
"test whether this path is absolute or relative" |
|
118 |
# C: is an absolute directory |
|
119 |
return (os.path.isabs(self.path) or driveRE.match(self.path)) |
|
120 |
||
121 |
def Absolute(self): |
|
122 |
"""return an object for the absolute version of this path. |
|
123 |
||
124 |
Prepends the current working directory to relative paths and |
|
125 |
the current drive (on Windows) to /something type paths.""" |
|
126 |
# leave C: alone as abspath will stick the cwd on |
|
127 |
if driveRE.match(self.path): |
|
128 |
return Path(self.path) |
|
129 |
else: |
|
130 |
return Path(os.path.abspath(self.path)) |
|
131 |
||
132 |
def Append(self, *arguments): |
|
133 |
"return an object with path elements added at the end of this path" |
|
134 |
return Join(*((self,) + arguments)) |
|
135 |
||
136 |
def Prepend(self, *arguments): |
|
137 |
"return an object with path elements added at the start of this path" |
|
138 |
return Join(*(arguments + (self,))) |
|
139 |
||
140 |
def isDir(self): |
|
141 |
"test whether this path points to an existing directory" |
|
142 |
# C: is a directory |
|
143 |
return (os.path.isdir(self.path) or driveRE.match(self.path)) |
|
144 |
||
145 |
def isFile(self): |
|
146 |
"test whether this path points to an existing file" |
|
147 |
return os.path.isfile(self.path) |
|
148 |
||
149 |
def Exists(self): |
|
150 |
"test whether this path exists in the filesystem" |
|
151 |
if driveRE.match(self.path): |
|
152 |
return os.path.exists(self.path + "/") |
|
153 |
else: |
|
154 |
return os.path.exists(self.path) |
|
155 |
||
156 |
def Dir(self): |
|
157 |
"return an object for the directory part of this path" |
|
158 |
if driveRE.match(self.path): |
|
159 |
return Path(self.path) |
|
160 |
else: |
|
161 |
return Path(os.path.dirname(self.path)) |
|
162 |
||
163 |
def File(self): |
|
164 |
"return a string for the file part of this path" |
|
165 |
return os.path.basename(self.path) |
|
166 |
||
167 |
def Components(self): |
|
168 |
"""return a list of the components of this path.""" |
|
169 |
return self.path.split('/') |
|
170 |
||
171 |
def FindCaseless(self): |
|
172 |
"""Given a path which may not be not correct in terms of case, |
|
173 |
search the filesystem to find the corresponding, correct path. |
|
174 |
paths are assumed to be absolute and normalised (which they |
|
175 |
should be in this class). |
|
176 |
||
177 |
Assumes that the path is more right than wrong, i.e. starts |
|
178 |
with the full path and tests for existence - then takes the |
|
179 |
last component off and check for that. |
|
180 |
||
181 |
This will be inefficient if used in cases where the file |
|
182 |
has a high probability of not existing. |
|
183 |
""" |
|
184 |
||
185 |
if os.path.exists(self.path): |
|
186 |
return Path(self.path) |
|
187 |
||
188 |
unknown_elements = [] |
|
189 |
tail = self.path |
|
190 |
head = None |
|
191 |
while tail != '': |
|
192 |
if os.path.exists(tail): |
|
193 |
break |
|
194 |
else: |
|
195 |
(tail,head) = os.path.split(tail) |
|
196 |
#print "(head,tail) = (%s,%s)\n" % (head,tail) |
|
197 |
unknown_elements.append(head) |
|
198 |
||
199 |
if tail == None: |
|
200 |
result = "" |
|
201 |
else: |
|
202 |
result = tail |
|
203 |
||
204 |
# Now we know the bits that may be wrong so we can search for them |
|
205 |
unknown_elements.reverse() |
|
206 |
for item in unknown_elements: |
|
207 |
possible = os.path.join(result, item) |
|
208 |
if os.path.exists(possible): |
|
209 |
result = possible |
|
210 |
continue # not finished yet - only this element is ok |
|
211 |
||
212 |
# Nope, we really do have to search for this component of the path |
|
213 |
possible = None |
|
214 |
if result: |
|
215 |
for file in os.listdir(result): |
|
216 |
if file.lower() == item.lower(): |
|
217 |
possible = os.path.join(result,file) |
|
218 |
break # find first matching name (might not be right) |
|
219 |
if possible is None: |
|
220 |
result = "" |
|
221 |
break # really couldn't find the file |
|
222 |
result = possible |
|
223 |
||
224 |
if result == "": |
|
225 |
return None |
|
226 |
||
227 |
return Path(result) |
|
228 |
||
229 |
def From(self,source): |
|
230 |
"""Returns the relative path from 'source' to here.""" |
|
231 |
list1 = source.Absolute().Components() |
|
232 |
list2 = self.Absolute().Components() |
|
233 |
||
234 |
# on windows if the drives are different |
|
235 |
# then the relative path is the absolute one. |
|
236 |
if isWin and list1[0] != list2[0]: |
|
237 |
return self.Absolute() |
|
238 |
||
239 |
final_list = [] |
|
240 |
for item in list1: |
|
241 |
if list2 != []: |
|
242 |
for widget in list2: |
|
243 |
if item == widget: |
|
244 |
list2.pop(0) |
|
245 |
break |
|
246 |
else: |
|
247 |
final_list.insert(0, "..") |
|
248 |
final_list.append(widget) |
|
249 |
list2.pop(0) |
|
250 |
break |
|
251 |
else: |
|
252 |
final_list.insert(0, "..") |
|
253 |
||
254 |
final_list.extend(list2) |
|
255 |
||
256 |
return Join(*final_list) |
|
257 |
||
258 |
def GetShellPath(self): |
|
259 |
"""Returns correct slashes according to os type as a string |
|
260 |
""" |
|
261 |
if isWin: |
|
262 |
if "OSTYPE" in os.environ and os.environ['OSTYPE'] == "cygwin" : |
|
263 |
return self.path |
|
264 |
||
265 |
return self.path.replace("/", "\\") |
|
266 |
||
267 |
return self.path |
|
268 |
||
400
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
269 |
def GetSpaceSafePath(self): |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
270 |
"""Returns a version of the path where spaces don't interfere with shell interpretation. |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
271 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
272 |
This functionality only applies to Windows - paths containing spaces are assumed to be problematic |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
273 |
on non-Windows platforms. |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
274 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
275 |
On Windows, the path is returned in Windows-specific 8.3 short path form - tilde are used to replace |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
276 |
spaces and fit path elements within 8.3 requirements. As 8.3 format paths are not guaranteed to be |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
277 |
supported on all Windows installs, and can only be calculated if they exist, a newly formated path is |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
278 |
only returned if it is returned by the Windows API and tested to exist. |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
279 |
""" |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
280 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
281 |
if not isWin: |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
282 |
return None |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
283 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
284 |
from ctypes.wintypes import DWORD, LPSTR, MAX_PATH |
3 | 285 |
|
400
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
286 |
GetShortPathNameA = ctypes.windll.kernel32.GetShortPathNameA |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
287 |
GetShortPathNameA.restype = DWORD |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
288 |
GetShortPathNameA.argtypes = LPSTR, LPSTR, DWORD |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
289 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
290 |
buffer = ctypes.create_string_buffer(MAX_PATH) |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
291 |
GetShortPathNameA(self.path, buffer, MAX_PATH) |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
292 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
293 |
spacesafe = buffer.value |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
294 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
295 |
if not spacesafe or not os.path.exists(spacesafe): |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
296 |
return None |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
297 |
|
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
298 |
return spacesafe |
554cc189839f
Support for resolving, on Windows only, <env/> 'tool' and 'toolchain' type paths with spaces.
Jon Chatten
parents:
3
diff
changeset
|
299 |
|
3 | 300 |
# Module functions |
301 |
||
302 |
def Join(*arguments): |
|
303 |
"""Concatenate the given list to make a generic path object. |
|
304 |
||
305 |
This can accept both strings and Path objects, and join |
|
306 |
them "intelligently" to make a complete path.""" |
|
307 |
list = [] |
|
308 |
for arg in arguments: |
|
309 |
if isinstance(arg, Path): |
|
310 |
list.append(arg.path) |
|
311 |
else: |
|
312 |
list.append(arg) |
|
313 |
||
314 |
return Path(*list) |
|
315 |
||
316 |
def CurrentDir(): |
|
317 |
"return a Path object for the current working directory" |
|
318 |
return Path(os.getcwd()) |
|
319 |
||
320 |
def NormalisePathList(aList): |
|
321 |
"""Convert a list of strings into a list of Path objects""" |
|
322 |
return map(lambda x: Path(x), aList) |
|
323 |
||
324 |
def Where(afile): |
|
325 |
"""Return the location of a file 'afile' in the system path. |
|
326 |
||
327 |
On windows, adds .exe onto the filename if it's not there. Returns the first location it found or None if it wasn't found. |
|
328 |
||
329 |
>>> Where("python") |
|
330 |
"/usr/bin/python" |
|
331 |
>>> Where("nonexistentfile") |
|
332 |
None |
|
333 |
""" |
|
334 |
location = None |
|
335 |
if sys.platform.startswith("win"): |
|
336 |
if not afile.lower().endswith(".exe"): |
|
337 |
afile += ".exe" |
|
338 |
||
339 |
for current_file in [os.path.join(loop_number,afile) for loop_number in |
|
340 |
os.environ["PATH"].split(os.path.pathsep)]: |
|
341 |
if os.path.isfile(current_file): |
|
342 |
location = current_file |
|
343 |
break |
|
344 |
return location |
|
345 |
||
346 |
# end of generic_path module |