Skip to content

bpo-30374: Fixed several bugs in win_add2path.py #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions Tools/scripts/win_add2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,70 @@
Licensed to PSF under a Contributor Agreement.
"""

import sys
import site
import ctypes
import os
import site
import sys
import winreg

HKCU = winreg.HKEY_CURRENT_USER
ENV = "Environment"
PATH = "PATH"
DEFAULT = "%PATH%"

def modify():
pythonpath = os.path.dirname(os.path.normpath(sys.executable))
scripts = os.path.join(pythonpath, "Scripts")
appdata = os.environ["APPDATA"]
if hasattr(site, "USER_SITE"):
usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
userpath = os.path.dirname(usersite)
userscripts = os.path.join(userpath, "Scripts")
else:
userscripts = None

with winreg.CreateKey(HKCU, ENV) as key:
try:
envpath = winreg.QueryValueEx(key, PATH)[0]
except OSError:
envpath = DEFAULT
envpath, dtype = winreg.QueryValueEx(key, PATH)
except FileNotFoundError:
envpath, dtype = "", winreg.REG_EXPAND_SZ
except:
raise OSError("Failed to load PATH value")

userscripts = None
if hasattr(site, "USER_SITE"):
if dtype == winreg.REG_EXPAND_SZ:
usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
userpath = os.path.dirname(usersite)
userscripts = os.path.join(userpath, "Scripts")
elif dtype == winreg.REG_SZ:
userpath = site.USER_SITE
userscripts = os.path.join(userpath, "Scripts")

paths = [envpath]
paths = []
for path in (pythonpath, scripts, userscripts):
if path and path not in envpath and os.path.isdir(path):
if path and path not in envpath:
paths.append(path)

envpath = os.pathsep.join(paths)
winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
if envpath == "":
envpath = os.pathsep.join(paths)
else:
envpath = os.pathsep.join([envpath] + paths)
winreg.SetValueEx(key, PATH, 0, dtype, envpath)
return paths, envpath

def refresh_environment_variables():
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
if not ctypes.windll.user32.SendMessageTimeoutW(
HWND_BROADCAST,
WM_SETTINGCHANGE,
0,
ENV,
SMTO_ABORTIFHUNG,
1000):
raise ctypes.WinError()

def main():
paths, envpath = modify()
if len(paths) > 1:
refresh_environment_variables()
print("Path(s) added:")
print('\n'.join(paths[1:]))
print('\n'.join(paths))
else:
print("No path was added")
print("\nPATH is now:\n%s\n" % envpath)
Expand Down