Skip to content

Commit

Permalink
Add convert_path wrapper
Browse files Browse the repository at this point in the history
This function has options for how paths should be converted based on cygpath modes. This function will call to_posix_path or to_windows_path as needed.

Signed-off-by: amorphousWaste <20346603+amorphousWaste@users.noreply.github.com>
  • Loading branch information
amorphousWaste authored and Jawabiscuit committed Sep 15, 2023
1 parent 51d9af0 commit e997c9e
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/rezplugins/shell/_utils/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,41 @@
_env_var_regex = re.compile(r"%([^%]*)%")


def to_posix_path(path):
"""Convert (eg) "C:\foo" to "/c/foo"
TODO: doesn't take into account escaped bask slashes, which would be
weird to have in a path, but is possible.
def convert_path(path, mode='unix', force_fwdslash=False):
r"""Convert a path to unix style or windows style as per cygpath rules.
Args:
path (str): Path to convert.
mode (str|Optional): Cygpath-style mode to use:
unix (default): Unix style path (c:\ and C:\ -> /c/)
windows: Windows style path (c:\ and C:\ -> C:/)
force_fwdslash (bool|Optional): Return a path containing only
forward slashes regardless of mode. Default is False.
Returns:
path(str): Converted path.
"""

# expand refs like %SYSTEMROOT%, leave as-is if not in environ
def _repl(m):
varname = m.groups()[0]
return os.getenv(varname, m.group())

path = _env_var_regex.sub(_repl, path)

# C:\ ==> /C/
# Convert the path based on mode.
if mode == 'windows':
path = to_windows_path(path)
else:
path = to_posix_path(path)

# NOTE: This would be normal cygpath behavior, but the broader
# implications of enabling it need extensive testing.
# Leaving it up to the user for now.
if force_fwdslash:
# Backslash -> fwdslash
path = path.replace('\\', '/')

return path
drive_letter_match = _drive_start_regex.match(path)
# If converting the drive letter to posix, capitalize the drive
# letter as per cygpath behavior.
Expand Down

0 comments on commit e997c9e

Please sign in to comment.