Skip to content
Closed
Changes from 1 commit
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
Next Next commit
fix several bugs
* Discard the DEFAULT "%PATH%" value.
* Delete wrong `os.path.isdir()`.
* Add absolute path instead of `%APPDATA%...` if the type of PATH is `REG_SZ`.
* Broadcast a `WM_SETTINGCHANG` message after the change of env vars.
* Add codes to deal with possible errors.
  • Loading branch information
XDcsy authored May 18, 2017
commit ddaf7aab2baf4827bb92659181a1911bebf734cd
54 changes: 39 additions & 15 deletions Tools/scripts/win_add2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,67 @@
import site
import os
import winreg
import ctypes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in alphabetical order


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
pass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary pass

except:
raise OSError("Failed to load PATH value")

if hasattr(site, "USER_SITE") and dtype == winreg.REG_EXPAND_SZ:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hasattr check doesn't apply to the subsequent elif block, or rather, if the first check here fails then we may end up crashing in the elif block.

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")
else:
userscripts = None

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