Welcome, guest | Sign In | My Account | Store | Cart

This simple import hook can resolve Windows shortcuts refering to directories. This allows Windows users to simulate UNIX directory structures which contain symlinks.

Python, 82 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import ihooks
import os
from imp import PKG_DIRECTORY
from win32com.shell import shell
import pythoncom

def resolve_shortcut(filename):
    """resolve_shortcut("Notepad.lnk") => "C:\WINDOWS\system32\notepad.exe"
    
    Returns the path refered to by a windows shortcut (.lnk) file.
    """
    
    shell_link = pythoncom.CoCreateInstance(
        shell.CLSID_ShellLink, None,
        pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
    
    persistant_file = shell_link.QueryInterface(pythoncom.IID_IPersistFile)
    
    persistant_file.Load(filename)
    
    shell_link.Resolve(0, 0)
    linked_to_file = shell_link.GetPath(shell.SLGP_UNCPRIORITY)[0]
    return linked_to_file
    
def get_shortcut_paths(directory):
    """get_shortcut_paths("Programs") => ["...\Internet Explorer.lnk", ...]
    
    Returns the list of Windows shortcuts contained in a directory.
    """
    return [os.path.join(directory, f) for f in os.listdir(directory)
                if os.path.splitext(f)[1] == '.lnk']
                 
class ShortcutLoader(ihooks.ModuleLoader):
    """A hook to resolve Python imports through Windows shortcuts"""
    
    def recursive_match(self, name, dir, level=1):
        names = name.split('.')
        short_name = names[0]
        remaining_names = names[1:]
        
        try:
            pairs = [(f, resolve_shortcut(f)) for f in get_shortcut_paths(dir)]
        except WindowsError:
            # might not have permissions, shortcut might be broken, etc.
            pass
        else:
            for alias, resolved_path in pairs:
                if (os.path.splitext(os.path.basename(alias))[0] ==
                                                            short_name) and \
                   os.path.isdir(resolved_path) and \
                   os.path.exists(os.path.join(resolved_path, '__init__.py')):
                    
                    if len(remaining_names) > 0:
                        next_part = self.recursive_match(
                                        '.'.join(remaining_names),
                                        resolved_path,
                                        level + 1)
                        
                        if next_part is not None:                        
                            return os.path.join(resolved_path, next_part)
                    else:
                        return resolved_path
                    
        return None
        
    def find_module_in_dir(self, name, dir, allow_packages=1):
        if dir is None:
            return ihooks.ModuleLoader.find_module_in_dir(
                self, name, dir, allow_packages)
        else:
            if allow_packages:
                resolved_path = self.recursive_match(name, dir)
                
                if resolved_path is not None:
                    return (None, resolved_path, ('', '', PKG_DIRECTORY))
                
            return ihooks.ModuleLoader.find_module_in_dir(
                self, name, dir, allow_packages)

def install():
    """Install the import hook"""
    ihooks.install(ihooks.ModuleImporter(ShortcutLoader()))

This code can be used to simulate some UNIX directory structures containing symlinks. My particular use case was:

user/pylib ui ... project2/pylib ui ... project3/pylib ui ... python-packages/user => /user/pylib project2 => /project2/pylib project3 => /project3/pylib

This implementation only resolves shortcuts refering to directories, not ones refering to files.