1
1
import os
2
2
import sys
3
3
import time
4
+ import winreg
4
5
5
6
from . import logging
6
7
from .pathutils import Path
@@ -51,14 +52,20 @@ def check_app_alias(cmd):
51
52
LOGGER .debug ("Check passed: aliases are correct" )
52
53
return True
53
54
55
+ _LONG_PATH_KEY = r"System\CurrentControlSet\Control\FileSystem"
56
+ _LONG_PATH_VALUENAME = "LongPathsEnabled"
54
57
55
- def check_long_paths (cmd ):
58
+ def check_long_paths (
59
+ cmd ,
60
+ * ,
61
+ hive = winreg .HKEY_LOCAL_MACHINE ,
62
+ keyname = _LONG_PATH_KEY ,
63
+ valuename = _LONG_PATH_VALUENAME ,
64
+ ):
56
65
LOGGER .debug ("Checking long paths setting" )
57
- import winreg
58
66
try :
59
- with winreg .OpenKeyEx (winreg .HKEY_LOCAL_MACHINE ,
60
- r"System\CurrentControlSet\Control\FileSystem" ) as key :
61
- if winreg .QueryValueEx (key , "LongPathsEnabled" ) == (1 , winreg .REG_DWORD ):
67
+ with winreg .OpenKeyEx (hive , keyname ) as key :
68
+ if winreg .QueryValueEx (key , valuename ) == (1 , winreg .REG_DWORD ):
62
69
LOGGER .debug ("Check passed: registry key is OK" )
63
70
return True
64
71
except FileNotFoundError :
@@ -67,6 +74,42 @@ def check_long_paths(cmd):
67
74
return False
68
75
69
76
77
+ def do_configure_long_paths (
78
+ cmd ,
79
+ * ,
80
+ hive = winreg .HKEY_LOCAL_MACHINE ,
81
+ keyname = _LONG_PATH_KEY ,
82
+ valuename = _LONG_PATH_VALUENAME ,
83
+ startfile = os .startfile ,
84
+ ):
85
+ LOGGER .debug ("Updating long paths setting" )
86
+ try :
87
+ with winreg .CreateKeyEx (hive , keyname ) as key :
88
+ winreg .SetValueEx (key , valuename , None , winreg .REG_DWORD , 1 )
89
+ LOGGER .info ("The setting has been successfully updated, and will "
90
+ "take effect after the next reboot." )
91
+ return
92
+ except OSError :
93
+ pass
94
+ if not cmd .confirm :
95
+ # Without confirmation, we assume we can't elevate, so attempt
96
+ # as the current user and if it fails just print a message.
97
+ LOGGER .warn ("The setting has not been updated. Please rerun '!B!py "
98
+ "install --configure!W! with administrative privileges." )
99
+ return
100
+ startfile (sys .executable , "runas" , "**configure-long-paths" , show_cmd = 0 )
101
+ for _ in range (5 ):
102
+ time .sleep (0.25 )
103
+ if check_long_paths (cmd ):
104
+ LOGGER .info ("The setting has been successfully updated, and will "
105
+ "take effect after the next reboot." )
106
+ break
107
+ else :
108
+ LOGGER .warn ("The setting may not have been updated. Please "
109
+ "visit the additional help link at the end for "
110
+ "more assistance." )
111
+
112
+
70
113
def check_py_on_path (cmd ):
71
114
LOGGER .debug ("Checking for legacy py.exe on PATH" )
72
115
from _native import read_alias_package
@@ -120,7 +163,6 @@ def check_global_dir(cmd):
120
163
121
164
122
165
def _check_global_dir_registry (cmd ):
123
- import winreg
124
166
with winreg .OpenKeyEx (winreg .HKEY_CURRENT_USER , "Environment" ) as key :
125
167
path , kind = winreg .QueryValueEx (key , "Path" )
126
168
LOGGER .debug ("Current registry path: %s" , path )
@@ -139,7 +181,6 @@ def _check_global_dir_registry(cmd):
139
181
140
182
141
183
def do_global_dir_on_path (cmd ):
142
- import winreg
143
184
added = notified = False
144
185
try :
145
186
LOGGER .debug ("Adding %s to PATH" , cmd .global_dir )
@@ -290,17 +331,8 @@ def first_run(cmd):
290
331
"may need an administrator to approve, and will require a "
291
332
"reboot. Some packages may fail to install without long "
292
333
"path support enabled.\n " , wrap = True )
293
- if cmd .confirm and not cmd .ask_ny ("Update setting now?" ):
294
- os .startfile (sys .executable , "runas" , "**configure-long-paths" , show_cmd = 0 )
295
- for _ in range (5 ):
296
- time .sleep (0.25 )
297
- if check_long_paths (cmd ):
298
- LOGGER .info ("The setting has been successfully updated." )
299
- break
300
- else :
301
- LOGGER .warn ("The setting may not have been updated. Please "
302
- "visit the additional help link at the end for "
303
- "more assistance." )
334
+ if not cmd .confirm or not cmd .ask_ny ("Update setting now?" ):
335
+ do_configure_long_paths (cmd )
304
336
elif cmd .explicit :
305
337
LOGGER .info ("Checked system long paths setting" )
306
338
@@ -314,10 +346,7 @@ def first_run(cmd):
314
346
LOGGER .print ("\n This may interfere with launching the new 'py' "
315
347
"command, and may be resolved by uninstalling "
316
348
"'!B!Python launcher!W!'.\n " , wrap = True )
317
- if (
318
- cmd .confirm and
319
- not cmd .ask_ny ("Open Installed apps now?" )
320
- ):
349
+ if cmd .confirm and not cmd .ask_ny ("Open Installed apps now?" ):
321
350
os .startfile ("ms-settings:appsfeatures" )
322
351
elif cmd .explicit :
323
352
if r == "skip" :
@@ -342,7 +371,7 @@ def first_run(cmd):
342
371
"must manually edit environment variables to later "
343
372
"remove the entry.\n " , wrap = True )
344
373
if (
345
- cmd .confirm and
374
+ not cmd .confirm or
346
375
not cmd .ask_ny ("Add commands directory to your PATH now?" )
347
376
):
348
377
do_global_dir_on_path (cmd )
@@ -352,7 +381,7 @@ def first_run(cmd):
352
381
else :
353
382
LOGGER .info ("Checked PATH for versioned commands directory" )
354
383
355
- # This check must be last, because 'do_install' will exit the program.
384
+ # This check must be last, because a failed install may exit the program.
356
385
if cmd .check_any_install :
357
386
if not check_any_install (cmd ):
358
387
welcome ()
@@ -361,11 +390,11 @@ def first_run(cmd):
361
390
LOGGER .print ("!Y!You do not have any Python runtimes installed.!W!" ,
362
391
level = logging .WARN )
363
392
LOGGER .print ("\n Install the current latest version of CPython? If "
364
- "not, you can use !B!py install default!W! later to "
393
+ "not, you can use ' !B!py install default!W!' later to "
365
394
"install, or one will be installed automatically when "
366
395
"needed.\n " , wrap = True )
367
396
LOGGER .info ("" )
368
- if cmd .ask_yn ("Install CPython now?" ):
397
+ if not cmd . confirm or cmd .ask_yn ("Install CPython now?" ):
369
398
do_install (cmd )
370
399
elif cmd .explicit :
371
400
LOGGER .info ("Checked for any Python installs" )
0 commit comments