Skip to content

Commit

Permalink
Do not raise an error if converting an unmapped UNC path
Browse files Browse the repository at this point in the history
Signed-off-by: javrin <jawabiscuit@users.noreply.github.com>
  • Loading branch information
Jawabiscuit committed Sep 15, 2023
1 parent 38ab2e5 commit 595c9e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/rez/rezconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,12 @@
}

# Some shells may require finer grained control over how path variables are
# handled. Similar to `env_pathed_vars`, this option provides a way to define
# handled. Similar to `pathed_env_vars`, this option provides a way to define
# variables the shell should handle, but on a per-shell basis. This setting can
# be used to override the pathing strategy provided by `pathed_env_vars` or to
# disable modification if that is desired.
#
# Note that, similar to `env_pathed_vars`, wildcards are supported.
# Note that, similar to `pathed_env_vars`, wildcards are supported.
shell_pathed_env_vars = {
"gitbash": ["PYTHONPATH"]
}
Expand Down
38 changes: 33 additions & 5 deletions src/rez/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,40 +429,68 @@ def test_dotted_paths(self):

@platform_dependent(["windows"])
def test_windows_unc_paths(self):
self.assertEqual(
cygpath.to_mixed_path("\\\\my_folder\\my_file.txt"),
"//my_folder/my_file.txt"
)
self.assertEqual(
cygpath.to_mixed_path("\\\\Server\\Share\\folder"),
"//Server/Share/folder"
)
self.assertEqual(
cygpath.to_mixed_path("\\\\server\\share\\folder\\file.txt"),
"//server/share/folder/file.txt"
)
self.assertEqual(
cygpath.to_mixed_path("\\\\server\\share/folder/file.txt"),
"//server/share/folder/file.txt"
)
self.assertEqual(
cygpath.to_mixed_path(r"\\server\share/folder\//file.txt"),
"//server/share/folder/file.txt"
)

@platform_dependent(["windows"])
def test_windows_unc_paths_strict(self):
self.assertRaisesRegex(
ValueError,
"Cannot convert path to mixed path: '.*' "
"Unmapped UNC paths are not supported",
cygpath.to_mixed_path,
'\\\\my_folder\\my_file.txt'
'\\\\my_folder\\my_file.txt',
strict=True,
)
self.assertRaisesRegex(
ValueError,
"Cannot convert path to mixed path: '.*' "
"Unmapped UNC paths are not supported",
cygpath.to_mixed_path,
"\\\\Server\\Share\\folder"
"\\\\Server\\Share\\folder",
strict=True,
)
self.assertRaisesRegex(
ValueError,
"Cannot convert path to mixed path: '.*' "
"Unmapped UNC paths are not supported",
cygpath.to_mixed_path,
"\\\\server\\share\\folder\\file.txt"
"\\\\server\\share\\folder\\file.txt",
strict=True,
)
self.assertRaisesRegex(
ValueError,
"Cannot convert path to mixed path: '.*' "
"Unmapped UNC paths are not supported",
cygpath.to_mixed_path,
"\\\\server\\share/folder/file.txt"
"\\\\server\\share/folder/file.txt",
strict=True,
)
self.assertRaisesRegex(
ValueError,
"Cannot convert path to mixed path: '.*' "
"Unmapped UNC paths are not supported",
cygpath.to_mixed_path,
r"\\server\share/folder\//file.txt"
r"\\server\share/folder\//file.txt",
strict=True,
)

@platform_dependent(["windows"])
Expand Down
22 changes: 14 additions & 8 deletions src/rez/utils/cygpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ def to_posix_path(path):
return drive + path


def to_mixed_path(path):
def to_mixed_path(path, strict=False):
r"""Convert (eg) 'C:\foo\bin' to 'C:/foo/bin'
Note: Especially in the case of UNC paths, this function will return a path
that is practically guaranteed to exist but it is not verified.
Args:
path (str): Path to convert.
strict (bool): Raise error if UNC path is not mapped to a drive. If False,
just return the path with slashes normalized.
Returns:
str: Converted path.
Expand All @@ -169,10 +171,12 @@ def to_mixed_path(path):
drive = to_mapped_drive(path)
if drive:
return drive + slashify(unc_path)
raise ValueError(
"Cannot convert path to mixed path: {!r} "
"Unmapped UNC paths are not supported".format(path)
)
if strict:
raise ValueError(
"Cannot convert path to mixed path: {!r} "
"Unmapped UNC paths are not supported".format(path)
)
return slashify(path, unc=True)

drive, path = os.path.splitdrive(path)

Expand All @@ -199,21 +203,23 @@ def to_mixed_path(path):
return drive + path


def slashify(path):
def slashify(path, unc=False):
"""Ensures path only contains forward slashes.
Args:
path (str): Path to convert.
unc (bool): Path is a unc path
Returns:
str: Converted path.
str: Path with slashes normalized.
"""
# Remove double backslashes and dots
path = os.path.normpath(path)
# Normalize slashes
path = path.replace("\\", "/")
# Remove double slashes
path = re.sub(r'/{2,}', '/', path)
if not unc:
path = re.sub(r'/{2,}', '/', path)
return path


Expand Down

0 comments on commit 595c9e8

Please sign in to comment.