Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# test that we can create junctions, even if symlinks are disabled
djunc = _win32_junction(dpath, join(tempdir, 'djunc'))
fjunc = _win32_junction(fpath, join(tempdir, 'fjunc.txt'))
if testing:
_win32_junction(broken_dpath, join(tempdir, 'broken_djunc'))
_win32_junction(broken_fpath, join(tempdir, 'broken_fjunc.txt'))
assert _win32_is_junction(djunc)
assert _win32_is_hardlinked(fpath, fjunc)
except Exception:
warnings.warn('We cannot create junctions either!')
raise
if testing:
# break the links
util_io.delete(broken_dpath)
util_io.delete(broken_fpath)
if verbose:
from ubelt import util_links
util_links._dirstats(tempdir)
try:
# Cleanup the test directory
util_io.delete(tempdir)
except Exception:
print('ERROR IN DELETE')
from ubelt import util_links
util_links._dirstats(tempdir)
raise
can_symlink = can_symlink_directories and can_symlink_files
__win32_can_symlink__ = can_symlink
print('Symlink: {path} -> {link}'.format(path=path, link=link))
if islink(link):
if verbose:
print('... already exists')
pointed = _readlink(link)
if pointed == path:
if verbose > 1:
print('... and points to the right place')
return link
if verbose > 1:
if not exists(link):
print('... but it is broken and points somewhere else: {}'.format(pointed))
else:
print('... but it points somewhere else: {}'.format(pointed))
if overwrite:
util_io.delete(link, verbose=verbose > 1)
elif exists(link):
if _win32_links is None:
if verbose:
print('... already exists, but its a file. This will error.')
raise FileExistsError(
'cannot overwrite a physical path: "{}"'.format(path))
else: # nocover
if verbose:
print('... already exists, and is either a file or hard link. '
'Assuming it is a hard link. '
'On non-win32 systems this would error.')
if _win32_links is None:
os.symlink(path, link)
else: # nocover
_win32_links._symlink(path, link, overwrite=overwrite, verbose=verbose)
except Exception:
warnings.warn('We cannot create junctions either!')
raise
if testing:
# break the links
util_io.delete(broken_dpath)
util_io.delete(broken_fpath)
if verbose:
from ubelt import util_links
util_links._dirstats(tempdir)
try:
# Cleanup the test directory
util_io.delete(tempdir)
except Exception:
print('ERROR IN DELETE')
from ubelt import util_links
util_links._dirstats(tempdir)
raise
can_symlink = can_symlink_directories and can_symlink_files
__win32_can_symlink__ = can_symlink
if not can_symlink:
warnings.warn('Cannot make real symlink. Falling back to junction')
if verbose:
print('can_symlink = {!r}'.format(can_symlink))
print('__win32_can_symlink__ = {!r}'.format(__win32_can_symlink__))
return can_symlink
try:
# test that we can create junctions, even if symlinks are disabled
djunc = _win32_junction(dpath, join(tempdir, 'djunc'))
fjunc = _win32_junction(fpath, join(tempdir, 'fjunc.txt'))
if testing:
_win32_junction(broken_dpath, join(tempdir, 'broken_djunc'))
_win32_junction(broken_fpath, join(tempdir, 'broken_fjunc.txt'))
assert _win32_is_junction(djunc)
assert _win32_is_hardlinked(fpath, fjunc)
except Exception:
warnings.warn('We cannot create junctions either!')
raise
if testing:
# break the links
util_io.delete(broken_dpath)
util_io.delete(broken_fpath)
if verbose:
from ubelt import util_links
util_links._dirstats(tempdir)
try:
# Cleanup the test directory
util_io.delete(tempdir)
except Exception:
print('ERROR IN DELETE')
from ubelt import util_links
util_links._dirstats(tempdir)
raise
can_symlink = can_symlink_directories and can_symlink_files
raise IOError('Cannot overwrite a real directory')
elif os.path.isfile(link):
if _win32_is_hardlinked(link, path):
if verbose:
print('...and is a hard link that points to the same place')
return link
else:
if verbose:
print('...and is a hard link that points somewhere else')
if _win32_can_symlink():
raise IOError('Cannot overwrite potentially real file if we can symlink')
if overwrite:
if verbose:
print('...overwriting')
util_io.delete(link, verbose > 1)
else:
if exists(link):
raise IOError('Link already exists')
_win32_symlink2(path, link, verbose=verbose)