from __future__ import print_function import os import sys import shutil assert sys.getwindowsversion() >= (6,0), "Windows Vista or later required" import ctypes ################################ # first some ctypes symlink implementation since Python doesn't support # symlinks in windows yet. Borrowed from jaraco.windows project. from ctypes import windll from ctypes import c_uint64 from ctypes.wintypes import ( BOOLEAN, LPWSTR, DWORD, LPVOID, HANDLE, FILETIME, WCHAR, ) CreateSymbolicLink = windll.kernel32.CreateSymbolicLinkW CreateSymbolicLink.argtypes = ( LPWSTR, LPWSTR, DWORD, ) CreateSymbolicLink.restype = BOOLEAN def handle_nonzero_success(result): if result == 0: raise RuntimeError(result) def symlink(link, target, target_is_directory = False): """ An implementation of os.symlink for Windows (Vista and greater) """ target_is_directory = target_is_directory or os.path.isdir(target) handle_nonzero_success(CreateSymbolicLink(link, target, target_is_directory)) # end ctypes symlink implementation ################################ ################################ # Also need to call _wstat from ctypes import Structure, c_uint, c_ushort, c_short, c_uint64, POINTER, c_int class STAT_STRUCT(Structure): _fields_ = [ # just use a pile of bytes for now ('data', ctypes.c_char*256), ] _wstat = windll.msvcrt._wstat _wstat.argtypes = [LPWSTR, POINTER(STAT_STRUCT)] _wstat.restype = c_int # ################################ # create a sample package package_name = 'sample' tagged = '{0}-tagged'.format(package_name) os.mkdir(tagged) init_file = os.path.join(tagged, '__init__.py') open(init_file, 'w').close() assert os.path.exists(init_file) # now create a symlink to the tagged package so it can be imported symlink(package_name, tagged) assert os.path.isdir(package_name) assert os.path.isfile(os.path.join(package_name, '__init__.py')) try: stat_res = STAT_STRUCT() res = _wstat(package_name, stat_res) print ("wstat returned " + str(res)) finally: # now cleanup os.rmdir(package_name) shutil.rmtree(tagged)