FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-30374: Fixed several bugs in win_add2path.py by XDcsy · Pull Request #1645 · python/cpython · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
57 changes: 40 additions & 17 deletions Tools/scripts/win_add2path.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL