diff options
Diffstat (limited to 'platform_utils.py')
-rw-r--r-- | platform_utils.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/platform_utils.py b/platform_utils.py index e0fa9dcc..2ad56490 100644 --- a/platform_utils.py +++ b/platform_utils.py | |||
@@ -242,3 +242,57 @@ def rename(src, dst): | |||
242 | raise | 242 | raise |
243 | else: | 243 | else: |
244 | os.rename(src, dst) | 244 | os.rename(src, dst) |
245 | |||
246 | |||
247 | def islink(path): | ||
248 | """Test whether a path is a symbolic link. | ||
249 | |||
250 | Availability: Windows, Unix. | ||
251 | """ | ||
252 | if isWindows(): | ||
253 | import platform_utils_win32 | ||
254 | return platform_utils_win32.islink(path) | ||
255 | else: | ||
256 | return os.path.islink(path) | ||
257 | |||
258 | |||
259 | def readlink(path): | ||
260 | """Return a string representing the path to which the symbolic link | ||
261 | points. The result may be either an absolute or relative pathname; | ||
262 | if it is relative, it may be converted to an absolute pathname using | ||
263 | os.path.join(os.path.dirname(path), result). | ||
264 | |||
265 | Availability: Windows, Unix. | ||
266 | """ | ||
267 | if isWindows(): | ||
268 | import platform_utils_win32 | ||
269 | return platform_utils_win32.readlink(path) | ||
270 | else: | ||
271 | return os.readlink(path) | ||
272 | |||
273 | |||
274 | def realpath(path): | ||
275 | """Return the canonical path of the specified filename, eliminating | ||
276 | any symbolic links encountered in the path. | ||
277 | |||
278 | Availability: Windows, Unix. | ||
279 | """ | ||
280 | if isWindows(): | ||
281 | current_path = os.path.abspath(path) | ||
282 | path_tail = [] | ||
283 | for c in range(0, 100): # Avoid cycles | ||
284 | if islink(current_path): | ||
285 | target = readlink(current_path) | ||
286 | current_path = os.path.join(os.path.dirname(current_path), target) | ||
287 | else: | ||
288 | basename = os.path.basename(current_path) | ||
289 | if basename == '': | ||
290 | path_tail.append(current_path) | ||
291 | break | ||
292 | path_tail.append(basename) | ||
293 | current_path = os.path.dirname(current_path) | ||
294 | path_tail.reverse() | ||
295 | result = os.path.normpath(os.path.join(*path_tail)) | ||
296 | return result | ||
297 | else: | ||
298 | return os.path.realpath(path) | ||