Skip to content

Commit

Permalink
Add debugging output to path conversion
Browse files Browse the repository at this point in the history
Add path conversion info into file.

Signed-off-by: amorphousWaste <20346603+amorphousWaste@users.noreply.github.com>
  • Loading branch information
amorphousWaste committed Nov 24, 2022
1 parent 8844f69 commit 6a4edb1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 22 deletions.
46 changes: 40 additions & 6 deletions src/rezplugins/shell/_utils/powershell_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from rez.system import system
from rez.utils.platform_ import platform_
from rez.utils.execution import Popen
from rez.utils.logging_ import print_debug
from rez.util import shlex_join
from .windows import convert_path

Expand Down Expand Up @@ -250,7 +251,16 @@ def normalize_path(self, path):
return path

if platform_.name == "windows":
return convert_path(path, 'windows')
converted_path = convert_path(path, 'windows')
if path != converted_path:
print_debug(
'Path converted: {} -> {}'.format(path, converted_path)
)
self._addline(
'# Path converted: {} -> {}'.format(path, converted_path)
)
return converted_path

else:
return path

Expand All @@ -261,21 +271,45 @@ def shebang(self):
pass

def setenv(self, key, value):
value = self.escape_string(value, is_path=self._is_pathed_key(key))
self._addline('Set-Item -Path "Env:{0}" -Value "{1}"'.format(key, value))
is_path = self._is_pathed_key(key)
new_value = self.escape_string(value, is_path=is_path)

if is_path and value != new_value:
print_debug(
'Path changed: {} -> {}'.format(value, new_value)
)
self._addline(
'# Path value changed: {} -> {}'.format(value, new_value)
)

self._addline(
'Set-Item -Path "Env:{0}" -Value "{1}"'.format(key, new_value)
)

def prependenv(self, key, value):
value = self.escape_string(value, is_path=self._is_pathed_key(key))
is_path = self._is_pathed_key(key)
new_value = self.escape_string(value, is_path=is_path)

if is_path and value != new_value:
self._addline(
'# Path value changed: {} -> {}'.format(value, new_value)
)

# Be careful about ambiguous case in pwsh on Linux where pathsep is :
# so that the ${ENV:VAR} form has to be used to not collide.
self._addline(
'Set-Item -Path "Env:{0}" -Value ("{1}{2}" + (Get-ChildItem "Env:{0}").Value)'.format(
key, value, self.pathsep)
key, new_value, self.pathsep)
)

def appendenv(self, key, value):
value = self.escape_string(value, is_path=self._is_pathed_key(key))
is_path = self._is_pathed_key(key)
new_value = self.escape_string(value, is_path=is_path)

if is_path and value != new_value:
self._addline(
'# Path value changed: {} -> {}'.format(value, new_value)
)

# Be careful about ambiguous case in pwsh on Linux where pathsep is :
# so that the ${ENV:VAR} form has to be used to not collide.
Expand Down
15 changes: 9 additions & 6 deletions src/rezplugins/shell/_utils/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import subprocess
from rez.utils.execution import Popen

from rez.utils.logging_ import print_debug

_drive_start_regex = re.compile(r"^([A-Za-z]):\\")
_drive_regex_mixed = re.compile(r"([a-z]):/")
Expand Down Expand Up @@ -38,20 +38,23 @@ def _repl(m):

# Convert the path based on mode.
if mode == 'mixed':
path = to_mixed_path(path)
new_path = to_mixed_path(path)
elif mode == 'windows':
path = to_windows_path(path)
new_path = to_windows_path(path)
else:
path = to_posix_path(path)
new_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('\\', '/')
new_path = new_path.replace('\\', '/')

return path
if path != new_path:
print_debug('Path converted: {} -> {}'.format(path, new_path))

return new_path


def to_posix_path(path):
Expand Down
18 changes: 14 additions & 4 deletions src/rezplugins/shell/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""
Windows Command Prompt (DOS) shell.
"""
from lib2to3.pytree import convert
from rez.config import config
from rez.rex import RexExecutor, expandable, OutputStyle, EscapedString
from rez.shells import Shell
Expand Down Expand Up @@ -296,24 +295,35 @@ def normalize_path(self, path):
Returns:
(str): Normalized file path.
"""
return convert_path(path, 'windows')
# Prevent path conversion if normalization is disabled in the config.
if config.disable_normalization:
return path

converted_path = convert_path(path, 'windows')

if path != converted_path:
self._addline(
'REM Path converted: {} -> {}'.format(
path, converted_path
)
)

return converted_path

def _saferefenv(self, key):
pass

def shebang(self):
pass

def setenv(self, key, value):
value = self.escape_string(
new_value = self.escape_string(
value,
is_path=self._is_pathed_key(key),
is_shell_path=self._is_shell_pathed_key(key),
)
self._addline('set %s=%s' % (key, value))

self._addline('set %s=%s' % (key, new_value))

def unsetenv(self, key):
self._addline("set %s=" % key)
Expand Down
15 changes: 13 additions & 2 deletions src/rezplugins/shell/csh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rez.util import shlex_join
from rez.utils.execution import Popen
from rez.utils.platform_ import platform_
from rez.utils.logging_ import print_debug
from rez.shells import UnixShell
from rez.rex import EscapedString

Expand Down Expand Up @@ -159,8 +160,18 @@ def _saferefenv(self, key):
self._addline("if (!($?%s)) setenv %s" % (key, key))

def setenv(self, key, value):
value = self.escape_string(value, is_path=self._is_pathed_key(key))
self._addline('setenv %s %s' % (key, value))
is_path = self._is_pathed_key(key) or self._is_shell_pathed_key(key)
new_value = self.escape_string(value, is_path=self._is_pathed_key(key))

if is_path and value != new_value:
print_debug(
'Path changed: {} -> {}'.format(value, new_value)
)
self._addline(
'# Path value changed: {} -> {}'.format(value, new_value)
)

self._addline('setenv %s %s' % (key, new_value))

def unsetenv(self, key):
self._addline("unsetenv %s" % key)
Expand Down
16 changes: 14 additions & 2 deletions src/rezplugins/shell/gitbash.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ def as_shell_path(self, path):
Returns:
(str): Transformed file path.
"""
return convert_path(path, mode='mixed', force_fwdslash=True)
# Prevent path conversion if normalization is disabled in the config.
if config.disable_normalization:
return path

converted_path = convert_path(path, mode='mixed', force_fwdslash=True)
if path != converted_path:
self._addline(
'# Path converted: {} -> {}'.format(path, converted_path)
)
return converted_path

def normalize_path(self, path):
"""Normalize the path to fit the environment.
For example, POSIX paths, Windows path, etc. If no transformation is
Expand All @@ -128,11 +134,17 @@ def normalize_path(self, path):
Returns:
(str): Normalized file path.
"""
return convert_path(path, mode='unix', force_fwdslash=True)
# Prevent path conversion if normalization is disabled in the config.
if config.disable_normalization:
return path

converted_path = convert_path(path, mode='unix', force_fwdslash=True)
if path != converted_path:
self._addline(
'# Path converted: {} -> {}'.format(path, converted_path)
)
return converted_path

def normalize_paths(self, value):
"""
This is a bit tricky in the case of gitbash. The problem we hit is that
Expand Down
14 changes: 12 additions & 2 deletions src/rezplugins/shell/sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rez.config import config
from rez.utils.execution import Popen
from rez.utils.platform_ import platform_
from rez.utils.logging_ import print_debug
from rez.shells import UnixShell
from rez.rex import EscapedString

Expand Down Expand Up @@ -105,15 +106,24 @@ def _bind_interactive_rez(self):

def setenv(self, key, value):
is_implicit = key == 'REZ_USED_IMPLICIT_PACKAGES'
is_path = self._is_pathed_key(key) or self._is_shell_pathed_key(key)

value = self.escape_string(
new_value = self.escape_string(
value,
is_path=self._is_pathed_key(key),
is_shell_path=self._is_shell_pathed_key(key),
is_implicit=is_implicit,
)

self._addline('export %s=%s' % (key, value))
if is_path and value != new_value:
print_debug(
'Path value changed: {} -> {}'.format(value, new_value)
)
self._addline(
'# Path value changed: {} -> {}'.format(value, new_value)
)

self._addline('export %s=%s' % (key, new_value))

def unsetenv(self, key):
self._addline("unset %s" % key)
Expand Down

0 comments on commit 6a4edb1

Please sign in to comment.