55"""
66import os
77import sys
8+ import subprocess
89
910PY3 = sys .version_info [0 ] == 3
1011system = sys .platform
@@ -66,6 +67,18 @@ def absjoin(*parts):
6667 return os .path .abspath (os .path .join (* parts ))
6768
6869
70+ # Cache whatever symlink function works (native or polyfill)
71+ _os_symlink = None
72+
73+
74+ def create_symlink_polyfill ():
75+ def symlink_ms (source , link_name ):
76+ subprocess .check_output (
77+ ['mklink' , '/D' , link_name , source ], stderr = subprocess .STDOUT , shell = True )
78+
79+ return symlink_ms
80+
81+
6982def create_symlink (source , link_name ):
7083 """Create a symbolic link pointing to source named link_name.
7184
@@ -77,18 +90,26 @@ def create_symlink(source, link_name):
7790 This function is a polyfill of the native ``os.symlink``
7891 for Python 2.x on Windows platforms.
7992 """
80- os_symlink = getattr (os , 'symlink' , None )
93+ global _os_symlink
94+ enable_retry_with_polyfill = False
8195
82- if not callable ( os_symlink ) and os . name == 'nt' :
83- import subprocess
96+ if not _os_symlink :
97+ _os_symlink = getattr ( os , 'symlink' , None )
8498
85- def symlink_ms (source , link_name ):
86- subprocess .check_output (
87- ['mklink' , '/D' , link_name , source ], stderr = subprocess .STDOUT , shell = True )
99+ if os .name == 'nt' :
100+ if not callable (_os_symlink ):
101+ _os_symlink = create_symlink_polyfill ()
102+ else :
103+ enable_retry_with_polyfill = True
88104
89- os_symlink = symlink_ms
105+ try :
106+ _os_symlink (source , link_name )
107+ except OSError :
108+ if not enable_retry_with_polyfill :
109+ raise
90110
91- os_symlink (source , link_name )
111+ _os_symlink = create_symlink_polyfill ()
112+ _os_symlink (source , link_name )
92113
93114
94115def remove_symlink (link ):
0 commit comments