44
55from .logging import LOGGER
66from .pathutils import Path
7+ from .tagutils import install_matches_any
78from .verutils import Version
89
910
@@ -136,7 +137,7 @@ def _update_reg_values(key, data, install, exclude=set()):
136137 winreg .SetValueEx (key , k , None , v_kind , v )
137138
138139
139- def _is_tag_managed (company_key , tag_name , * , creating = False ):
140+ def _is_tag_managed (company_key , tag_name , * , creating = False , allow_warn = True ):
140141 try :
141142 tag = winreg .OpenKey (company_key , tag_name )
142143 except FileNotFoundError :
@@ -188,12 +189,15 @@ def _is_tag_managed(company_key, tag_name, *, creating=False):
188189 new_name = f"{ orig_name } .{ i } "
189190 # Raises standard PermissionError (5) if new_name exists
190191 reg_rename_key (tag .handle , orig_name , new_name )
191- LOGGER .warn ("An existing registry key for %s was renamed to %s "
192- "because it appeared to be invalid. If this is "
193- "correct, the registry key can be safely deleted. "
194- "To avoid this in future, ensure that the "
195- "InstallPath key refers to a valid path." ,
196- tag_name , new_name )
192+ if allow_warn :
193+ LOGGER .warn ("An existing registry key for %s was renamed to %s "
194+ "because it appeared to be invalid. If this is "
195+ "correct, the registry key can be safely deleted. "
196+ "To avoid this in future, ensure that the "
197+ "InstallPath key refers to a valid path." ,
198+ tag_name , new_name )
199+ else :
200+ LOGGER .debug ("Renamed %s to %s" , tag_name , new_name )
197201 break
198202 except FileNotFoundError :
199203 LOGGER .debug ("Original key disappeared, so we will claim it" )
@@ -207,8 +211,12 @@ def _is_tag_managed(company_key, tag_name, *, creating=False):
207211 orig_name , new_name , exc_info = True )
208212 raise
209213 else :
210- LOGGER .warn ("Attempted to clean up invalid registry key %s but "
211- "failed after too many attempts." , tag_name )
214+ if allow_warn :
215+ LOGGER .warn ("Attempted to clean up invalid registry key %s but "
216+ "failed after too many attempts." , tag_name )
217+ else :
218+ LOGGER .debug ("Attempted to clean up invalid registry key %s but "
219+ "failed after too many attempts." , tag_name )
212220 return False
213221 return True
214222
@@ -226,31 +234,40 @@ def _split_root(root_name):
226234 return hive , name
227235
228236
229- def update_registry (root_name , install , data ):
237+ def update_registry (root_name , install , data , warn_for = [] ):
230238 hive , name = _split_root (root_name )
231239 with winreg .CreateKey (hive , name ) as root :
232- if _is_tag_managed (root , data ["Key" ], creating = True ):
240+ allow_warn = install_matches_any (install , warn_for )
241+ if _is_tag_managed (root , data ["Key" ], creating = True , allow_warn = allow_warn ):
233242 with winreg .CreateKey (root , data ["Key" ]) as tag :
234243 LOGGER .debug ("Creating/updating %s\\ %s" , root_name , data ["Key" ])
235244 winreg .SetValueEx (tag , "ManagedByPyManager" , None , winreg .REG_DWORD , 1 )
236245 _update_reg_values (tag , data , install , {"kind" , "Key" , "ManagedByPyManager" })
237- else :
246+ elif allow_warn :
238247 LOGGER .warn ("An existing runtime is registered at %s in the registry, "
239248 "and so the new one has not been registered." , data ["Key" ])
240249 LOGGER .info ("This may prevent some other applications from detecting "
241250 "the new installation, although 'py -V:...' will work. "
242251 "To register the new installation, remove the existing "
243252 "runtime and then run 'py install --refresh'" )
253+ else :
254+ LOGGER .debug ("An existing runtime is registered at %s and so the new "
255+ "install has not been registered." , data ["Key" ])
244256
245257
246- def cleanup_registry (root_name , keep ):
258+ def cleanup_registry (root_name , keep , warn_for = [] ):
247259 hive , name = _split_root (root_name )
248260 with _reg_open (hive , name , writable = True ) as root :
249261 for company_name in _iter_keys (root ):
250262 any_left = False
251263 with winreg .OpenKey (root , company_name , access = winreg .KEY_ALL_ACCESS ) as company :
252264 for tag_name in _iter_keys (company ):
253- if f"{ company_name } \\ { tag_name } " in keep or not _is_tag_managed (company , tag_name ):
265+ # Calculate whether to show warnings or not
266+ install = {"company" : company_name , "tag" : tag_name }
267+ allow_warn = install_matches_any (install , warn_for )
268+
269+ if (f"{ company_name } \\ { tag_name } " in keep
270+ or not _is_tag_managed (company , tag_name , allow_warn = allow_warn )):
254271 any_left = True
255272 else :
256273 _reg_rmtree (company , tag_name )
0 commit comments