Windows symlink: os.path.realpath resolves symlink for windows

This commit is contained in:
Dmitry Petrov
2017-04-10 16:44:48 -07:00
parent 92553358e6
commit 0d0d4853ac
+22
View File
@@ -1,4 +1,5 @@
import os
import re
import sys
import ctypes
@@ -29,6 +30,7 @@ class Runtime(object):
os.symlink = Runtime.symlink
os.path.islink = Runtime.is_link
os.path.realpath = Runtime.realpath
@staticmethod
def _setup_windows_symlink():
@@ -52,6 +54,26 @@ class Runtime(object):
return '<SYMLINK>' in output
@staticmethod
def realpath(path):
if os.name != 'nt':
return os.path.realpath(path)
if not os.path.islink(path):
return os.path.realpath(path)
# It is definitely not the best way to check a symlink.
code, output, _ = Executor.exec_cmd(["dir", path], shell=True)
if code != 0 or not '<SYMLINK>' in output:
return os.path.realpath(path)
groups = re.compile(r'\[\S+\]$').findall(output.strip())
if len(groups) < 1:
return os.path.realpath(path)
resolved_link = groups[0][1:-1]
return resolved_link
@staticmethod
def symlink(source, link_name):
'''symlink(source, link_name) - DVC override for Windows