Move WindowsSymlinkOverride logic to Runtime

This commit is contained in:
Dmitry Petrov
2017-04-07 18:45:58 -07:00
parent 14c47b0379
commit e9112658d5
2 changed files with 29 additions and 27 deletions
+2 -27
View File
@@ -1,30 +1,5 @@
import os
import ctypes
class WindowsSymlinkOverride(object):
FUNC = None
def __init__(self):
func = ctypes.windll.kernel32.CreateSymbolicLinkW
func.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
func.restype = ctypes.c_ubyte
WindowsSymlinkOverride.FUNC = func
@staticmethod
def symlink(source, link_name):
'''symlink(source, link_name) - DVC override for Windows
Creates a symbolic link pointing to source named link_name'''
flags = 0
if source is not None and os.path.isdir(source):
flags = 1
if WindowsSymlinkOverride.FUNC(link_name, source, flags) == 0:
raise ctypes.WinError()
from dvc.runtime import Runtime
def setup():
if os.name == 'nt':
os.symlink = WindowsSymlinkOverride().symlink
pass
Runtime.symlink_setup()
+27
View File
@@ -1,5 +1,6 @@
import os
import sys
import ctypes
from dvc.config import Config, ConfigI
from dvc.exceptions import DvcException
@@ -10,6 +11,31 @@ from dvc.settings import Settings
class Runtime(object):
CONFIG = 'dvc.conf'
SYMLINC_OVERRIDE = None
@staticmethod
def symlink_setup():
if os.name != 'nt':
return
func = ctypes.windll.kernel32.CreateSymbolicLinkW
func.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
func.restype = ctypes.c_ubyte
Runtime.SYMLINC_OVERRIDE = func
os.symlink = Runtime.symlink
@staticmethod
def symlink(source, link_name):
'''symlink(source, link_name) - DVC override for Windows
Creates a symbolic link pointing to source named link_name'''
flags = 0
if source is not None and os.path.isdir(source):
flags = 1
if Runtime.SYMLINC_OVERRIDE(link_name, source, flags) == 0:
raise ctypes.WinError()
@staticmethod
def conf_file_path(git_dir):
@@ -18,6 +44,7 @@ class Runtime(object):
@staticmethod
def run(cmd_class, parse_config=True):
try:
Runtime.symlink_setup()
runtime_git = GitWrapper()
if parse_config: