summaryrefslogtreecommitdiffstats
path: root/platform_utils.py
diff options
context:
space:
mode:
authorRenaud Paquay <rpaquay@google.com>2016-11-01 14:37:13 -0700
committerRenaud Paquay <rpaquay@google.com>2017-08-31 13:49:01 -0700
commit227ad2ef42f47798d24814dfc2cef8119c313ab7 (patch)
tree0702d4dac67f59072edc6587122c27f4753021a5 /platform_utils.py
parent2a4be948788dfe5ae9437b048fba229a96bbff2d (diff)
downloadgit-repo-227ad2ef42f47798d24814dfc2cef8119c313ab7.tar.gz
Implement islink, readlink and realpath using Win32 api
Change-Id: I18452cbb32d24db73601ad10485dbe6bb278731c
Diffstat (limited to 'platform_utils.py')
-rw-r--r--platform_utils.py54
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
247def 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
259def 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
274def 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)