@@ -249,21 +249,14 @@ def _write_alias(cmd, install, alias, target):
249249 LOGGER .debug ("Skipping %s alias because the launcher template was not found." , alias ["name" ])
250250 return
251251
252- launcher_remap = cmd .scratch .setdefault ("install_command._write_alias.launcher_remap" , {})
253- LOGGER .debug ("remap %s" , launcher_remap )
254- try :
255- launcher = launcher_remap [str (launcher )]
256- except KeyError :
257- pass
258-
259252 try :
260253 launcher_bytes = launcher .read_bytes ()
261254 except OSError :
262255 warnings_shown = cmd .scratch .setdefault ("install_command._write_alias.warnings_shown" , set ())
263256 if str (launcher ) not in warnings_shown :
264257 LOGGER .warn ("Failed to read launcher template at %s." , launcher )
265258 warnings_shown .add (str (launcher ))
266- LOGGER .debug (exc_info = True )
259+ LOGGER .debug ("Failed to read %s" , launcher , exc_info = True )
267260 return
268261
269262 existing_bytes = b''
@@ -275,7 +268,13 @@ def _write_alias(cmd, install, alias, target):
275268 except OSError :
276269 LOGGER .debug ("Failed to read existing alias launcher." )
277270
278- if existing_bytes != launcher_bytes :
271+ launcher_remap = cmd .scratch .setdefault ("install_command._write_alias.launcher_remap" , {})
272+
273+ if existing_bytes == launcher_bytes :
274+ # Valid existing launcher, so save its path in case we need it later
275+ # for a hard link.
276+ launcher_remap .setdefault (launcher .name , p )
277+ else :
279278 # First try and create a hard link
280279 unlink (p )
281280 try :
@@ -285,13 +284,21 @@ def _write_alias(cmd, install, alias, target):
285284 if ex .winerror != 17 :
286285 # Report errors other than cross-drive links
287286 LOGGER .debug ("Failed to create hard link for command." , exc_info = True )
288- try :
289- p .write_bytes (launcher_bytes )
290- LOGGER .debug ("Created %s as copy of %s" , p .name , launcher .name )
291- launcher_remap [str (launcher )] = p
292- except OSError :
293- LOGGER .error ("Failed to create global command %s." , alias ["name" ])
294- LOGGER .debug (exc_info = True )
287+ launcher2 = launcher_remap .get (launcher .name )
288+ if launcher2 :
289+ try :
290+ os .link (launcher , p )
291+ except OSError :
292+ LOGGER .debug ("Failed to create hard link from fallback launcher" )
293+ launcher2 = None
294+ if not launcher2 :
295+ try :
296+ p .write_bytes (launcher_bytes )
297+ LOGGER .debug ("Created %s as copy of %s" , p .name , launcher .name )
298+ launcher_remap [launcher .name ] = p
299+ except OSError :
300+ LOGGER .error ("Failed to create global command %s." , alias ["name" ])
301+ LOGGER .debug (exc_info = True )
295302
296303 p_target = p .with_name (p .name + ".__target__" )
297304 try :
0 commit comments