Skip to content

Commit

Permalink
Simplify syspaths
Browse files Browse the repository at this point in the history
Here's an attempt at minimising what goes into PATH on startup. Previously, whatever the PATH was set to on the users machine got forwarded into the Rez context, but that's bad as we can never be sure what that PATH actually is. Now we know it includes these three paths, which is what you'll find in a newly installed Windows install. They are added dynamically, based on where `cmd.exe` and `powershell.exe` are first found, which is debatable whether that's desirable. The alternative is simply adding `c:\windows\system32` etc directly, which may be the most secure route.
  • Loading branch information
mottosso committed Jun 11, 2019
1 parent ec781fe commit c8d1690
Showing 1 changed file with 11 additions and 76 deletions.
87 changes: 11 additions & 76 deletions src/rezplugins/shell/powershell.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""
Windows Command Prompt (DOS) shell.
"""
"""Windows PowerShell 5+"""

from rez.config import config
from rez.rex import RexExecutor, OutputStyle, EscapedString
from rez.shells import Shell
Expand Down Expand Up @@ -77,83 +76,19 @@ def get_syspaths(cls):
cls.syspaths = config.standard_system_paths
return cls.syspaths

# detect system paths using registry
def gen_expected_regex(parts):
whitespace = r"[\s]+"
return whitespace.join(parts)

paths = []

cmd = [
"REG",
"QUERY",
(
"HKLM\\SYSTEM\\CurrentControlSet\\"
"Control\\Session Manager\\Environment"
),
"/v",
"PATH"
# Vanilla PATH, before user interference
cls.syspaths = [
os.path.dirname(which("cmd")), # e.g. System32/
os.path.dirname(which("powershell")),
os.path.dirname(which("scrcons")),
]

expected = gen_expected_regex([
(
"HKEY_LOCAL_MACHINE\\\\SYSTEM\\\\CurrentControlSet\\\\"
"Control\\\\Session Manager\\\\Environment"
),
"PATH",
"REG_(EXPAND_)?SZ",
"(.*)"
])

p = popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=True)
out_, _ = p.communicate()
out_ = out_.strip()

if p.returncode == 0:
match = re.match(expected, out_)
if match:
paths.extend(match.group(2).split(os.pathsep))

cmd = [
"REG",
"QUERY",
"HKCU\\Environment",
"/v",
"PATH"
# And Rez itself, for making rez calls
# within the resulting context
cls.syspaths += [
os.path.dirname(which("rez")),
]

expected = gen_expected_regex([
"HKEY_CURRENT_USER\\\\Environment",
"PATH",
"REG_(EXPAND_)?SZ",
"(.*)"
])

p = popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=True)
out_, _ = p.communicate()
out_ = out_.strip()

if p.returncode == 0:
match = re.match(expected, out_)
if match:
paths.extend(match.group(2).split(os.pathsep))

cls.syspaths = list(set([x for x in paths if x]))

# add Rez binaries
exe = which("rez-env")
assert exe, "Could not find rez binary, this is a bug"
rez_bin_dir = os.path.dirname(exe)
cls.syspaths.insert(0, rez_bin_dir)

return cls.syspaths

def _bind_interactive_rez(self):
Expand Down

0 comments on commit c8d1690

Please sign in to comment.