Files
dvc/dvc/system.py
T
2017-10-30 05:05:28 +02:00

67 lines
1.7 KiB
Python

import ctypes
import os
import re
from builtins import str
if os.name == 'nt':
from ctypes import create_unicode_buffer, windll
from dvc.executor import Executor
class System(object):
LONG_PATH_BUFFER_SIZE = 1024
@staticmethod
def is_unix():
return os.name != 'nt'
@staticmethod
def hardlink(source, link_name):
if System.is_unix():
return os.link(source, link_name)
raise Exception('Not Implemented Yet')
@staticmethod
def samefile(path1, path2):
if System.is_unix():
return os.path.samefile(path1, path2)
raise Exception('Not Implemented Yet')
@staticmethod
def realpath(path):
# It is definitely not the best way to check a symlink.
if System.is_unix():
return os.path.realpath(path)
output = System._get_symlink_string(path)
if output is None:
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 get_long_path(path):
"""Convert short path to a full path. It is needed for Windows."""
if System.is_unix():
return path
buffer = create_unicode_buffer(System.LONG_PATH_BUFFER_SIZE)
get_long_path_name = windll.kernel32.GetLongPathNameW
result = get_long_path_name(u'%s' % str(path), buffer, System.LONG_PATH_BUFFER_SIZE)
if result == 0 or result > System.LONG_PATH_BUFFER_SIZE:
return path
return buffer.value
@staticmethod
def get_cwd():
return System.get_long_path(os.getcwd())