1717from .pathutils import Path
1818
1919
20+ def _package_name ():
21+ from _native import get_current_package
22+ return get_current_package ()
23+
24+
2025def check_app_alias (cmd ):
2126 LOGGER .debug ("Checking app execution aliases" )
22- from _native import get_current_package , read_alias_package
2327 # Expected identities:
2428 # Side-loaded MSIX
2529 # * "PythonSoftwareFoundation.PythonManager_3847v3x7pw1km",
@@ -30,13 +34,15 @@ def check_app_alias(cmd):
3034 # MSI/dev install
3135 # * None
3236 try :
33- pkg = get_current_package ()
37+ pkg = _package_name ()
3438 except OSError :
3539 LOGGER .debug ("Failed to get current package name." , exc_info = True )
3640 pkg = None
3741 if not pkg :
3842 LOGGER .debug ("Check skipped: MSI install can't do this check" )
3943 return "skip"
44+
45+ from _native import read_alias_package
4046 LOGGER .debug ("Checking for %s" , pkg )
4147 root = Path (os .environ ["LocalAppData" ]) / "Microsoft/WindowsApps"
4248 for name in ["py.exe" , "pyw.exe" , "python.exe" , "pythonw.exe" , "python3.exe" , "pymanager.exe" ]:
@@ -59,7 +65,8 @@ def check_long_paths(cmd):
5965 LOGGER .debug ("Checking long paths setting" )
6066 import winreg
6167 try :
62- with winreg .OpenKeyEx (winreg .HKEY_LOCAL_MACHINE , r"System\CurrentControlSet\Control\FileSystem" ) as key :
68+ with winreg .OpenKeyEx (winreg .HKEY_LOCAL_MACHINE ,
69+ r"System\CurrentControlSet\Control\FileSystem" ) as key :
6370 if winreg .QueryValueEx (key , "LongPathsEnabled" ) == (1 , winreg .REG_DWORD ):
6471 LOGGER .debug ("Check passed: registry key is OK" )
6572 return True
@@ -71,9 +78,14 @@ def check_long_paths(cmd):
7178
7279def check_py_on_path (cmd ):
7380 LOGGER .debug ("Checking for legacy py.exe on PATH" )
74- from _native import get_current_package , read_alias_package
75- if not get_current_package ():
76- LOGGER .debug ("Check skipped: MSI install can't do this check" )
81+ from _native import read_alias_package
82+ try :
83+ if not _package_name ():
84+ LOGGER .debug ("Check skipped: MSI install can't do this check" )
85+ return "skip"
86+ except OSError :
87+ LOGGER .debug ("Failed to get current package name." , exc_info = True )
88+ LOGGER .debug ("Check skipped: can't do this check" )
7789 return "skip"
7890 for p in os .environ ["PATH" ].split (";" ):
7991 if not p :
@@ -90,6 +102,8 @@ def check_py_on_path(cmd):
90102 # Probably not an alias, so we're not good
91103 LOGGER .debug ("Check failed: found %s on PATH" , py )
92104 return False
105+ LOGGER .debug ("Check passed: no py.exe on PATH at all" )
106+ return True
93107
94108
95109def check_global_dir (cmd ):
@@ -104,28 +118,35 @@ def check_global_dir(cmd):
104118 LOGGER .debug ("Check passed: %s is on PATH" , p )
105119 return True
106120 # In case user has updated their registry but not the terminal
107- import winreg
108121 try :
109- with winreg .OpenKeyEx (winreg .HKEY_CURRENT_USER , "Environment" ) as key :
110- path , kind = winreg .QueryValueEx (key , "Path" )
111- LOGGER .debug ("Current registry path: %s" , path )
112- if kind == winreg .REG_EXPAND_SZ :
113- path = os .path .expandvars (path )
114- elif kind != winreg .REG_SZ :
115- LOGGER .debug ("Check skipped: PATH registry key is not a string." )
116- return "skip"
117- for p in path .split (";" ):
118- if not p :
119- continue
120- if Path (p ).absolute ().match (cmd .global_dir ):
121- LOGGER .debug ("Check skipped: %s will be on PATH after restart" , p )
122- return True
122+ r = _check_global_dir_registry (cmd )
123+ if r :
124+ return r
123125 except Exception :
124126 LOGGER .debug ("Failed to read PATH setting from registry" , exc_info = True )
125127 LOGGER .debug ("Check failed: %s not found in PATH" , cmd .global_dir )
126128 return False
127129
128130
131+ def _check_global_dir_registry (cmd ):
132+ import winreg
133+ with winreg .OpenKeyEx (winreg .HKEY_CURRENT_USER , "Environment" ) as key :
134+ path , kind = winreg .QueryValueEx (key , "Path" )
135+ LOGGER .debug ("Current registry path: %s" , path )
136+ if kind == winreg .REG_EXPAND_SZ :
137+ path = os .path .expandvars (path )
138+ elif kind != winreg .REG_SZ :
139+ LOGGER .debug ("Check skipped: PATH registry key is not a string." )
140+ return "skip"
141+ for p in path .split (";" ):
142+ if not p :
143+ continue
144+ if Path (p ).absolute ().match (cmd .global_dir ):
145+ LOGGER .debug ("Check skipped: %s will be on PATH after restart" , p )
146+ return True
147+ return False
148+
149+
129150def do_global_dir_on_path (cmd ):
130151 import winreg
131152 added = notified = False
0 commit comments