From e5bee243a377c4d2aa32b6ac9ff1ede33c12227d Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Wed, 8 Jan 2020 14:09:22 -0600 Subject: [PATCH 1/4] py3: use links from os module --- dvc/system.py | 57 ++++++++++----------------------------------------- 1 file changed, 11 insertions(+), 46 deletions(-) diff --git a/dvc/system.py b/dvc/system.py index 26e8497cd1..4640b1ab69 100644 --- a/dvc/system.py +++ b/dvc/system.py @@ -4,6 +4,7 @@ import shutil from dvc.compat import fspath +from dvc.exceptions import DvcException logger = logging.getLogger(__name__) @@ -21,55 +22,19 @@ def copy(src, dest): @staticmethod def hardlink(source, link_name): - import ctypes - from dvc.exceptions import DvcException - - source, link_name = fspath(source), fspath(link_name) - - if System.is_unix(): - try: - os.link(source, link_name) - return - except Exception as exc: - raise DvcException("link") from exc - - CreateHardLink = ctypes.windll.kernel32.CreateHardLinkW - CreateHardLink.argtypes = [ - ctypes.c_wchar_p, - ctypes.c_wchar_p, - ctypes.c_void_p, - ] - CreateHardLink.restype = ctypes.wintypes.BOOL - - res = CreateHardLink(link_name, source, None) - if res == 0: - raise DvcException("CreateHardLinkW") from ctypes.WinError() + try: + os.link(source, link_name) + return + except Exception as exc: + raise DvcException("failed to link") from exc @staticmethod def symlink(source, link_name): - import ctypes - from dvc.exceptions import DvcException - - source, link_name = fspath(source), fspath(link_name) - - if System.is_unix(): - try: - os.symlink(source, link_name) - return - except Exception as exc: - msg = "failed to symlink '{}' -> '{}': {}" - raise DvcException(msg.format(source, link_name, str(exc))) - - flags = 0 - if source is not None and os.path.isdir(source): - flags = 1 - - func = ctypes.windll.kernel32.CreateSymbolicLinkW - func.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32) - func.restype = ctypes.c_ubyte - - if func(link_name, source, flags) == 0: - raise DvcException("CreateSymbolicLinkW") from ctypes.WinError() + try: + os.symlink(source, link_name) + return + except Exception as exc: + raise DvcException("failed to symlink") from exc @staticmethod def _reflink_darwin(src, dst): From 6eac40d2260c2073a9089c03572374b890a7c67a Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Fri, 10 Jan 2020 17:35:54 -0600 Subject: [PATCH 2/4] system: catch OSError instead of Exception --- dvc/system.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dvc/system.py b/dvc/system.py index 4640b1ab69..ce523d0014 100644 --- a/dvc/system.py +++ b/dvc/system.py @@ -24,16 +24,14 @@ def copy(src, dest): def hardlink(source, link_name): try: os.link(source, link_name) - return - except Exception as exc: + except OSError as exc: raise DvcException("failed to link") from exc @staticmethod def symlink(source, link_name): try: os.symlink(source, link_name) - return - except Exception as exc: + except OSError as exc: raise DvcException("failed to symlink") from exc @staticmethod From b8dd4225d217f9627602d462d27b1c7c8f27bd29 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Fri, 10 Jan 2020 17:40:59 -0600 Subject: [PATCH 3/4] py3: fspath source and link_name --- dvc/system.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dvc/system.py b/dvc/system.py index ce523d0014..acc90e4de6 100644 --- a/dvc/system.py +++ b/dvc/system.py @@ -22,6 +22,7 @@ def copy(src, dest): @staticmethod def hardlink(source, link_name): + source, link_name = fspath(source), fspath(link_name) try: os.link(source, link_name) except OSError as exc: @@ -29,6 +30,7 @@ def hardlink(source, link_name): @staticmethod def symlink(source, link_name): + source, link_name = fspath(source), fspath(link_name) try: os.symlink(source, link_name) except OSError as exc: From df97a2a7deb92d0de10840174ff3e00ea0d7e08c Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Fri, 10 Jan 2020 18:32:31 -0600 Subject: [PATCH 4/4] don't reimport DvcException --- dvc/system.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dvc/system.py b/dvc/system.py index acc90e4de6..d32663b4af 100644 --- a/dvc/system.py +++ b/dvc/system.py @@ -91,7 +91,6 @@ def _reflink_linux(src, dst): @staticmethod def reflink(source, link_name): import platform - from dvc.exceptions import DvcException source, link_name = fspath(source), fspath(link_name)