]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/pkgresutil.py
revert previous commit to fix attribution (vanity)
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / pkgresutil.py
1
2 def install():
3     """
4     This installs a hook into setuptools' pkg_resources infrastructure, so that resource
5     files can be found in files relative to the runnin executable, in addition to the
6     usual egg and source lookup mechanisms.  This overrides the ZipProvider, since that
7     is the lookup mechanism triggered within pkg_resources when running code out of a
8     py2exe or py2app build's library.zip.
9     """
10     import os, sys
11     import pkg_resources, zipimport
12
13     platform_libdirs = {
14         'darwin': '../Resources/pkg_resources',
15         }
16     exedir = os.path.dirname(sys.executable)
17     libdir = platform_libdirs.get(sys.platform, 'pkg_resources')
18
19     class Provider(pkg_resources.ZipProvider):
20
21         def __init__(self, module):
22             self._module_name = module.__name__
23             pkg_resources.ZipProvider.__init__(self, module)
24
25         def get_resource_filename(self, manager, resource_name):
26             #print 'get_resource_filename(%s, %s)' % (manager, resource_name)
27             path = [exedir, libdir] + self._module_name.split('.') + [resource_name]
28             localfile = os.path.join(*path)
29             #print '             checking(%s)' % (localfile,)
30             if os.path.exists(localfile):
31                 #print 'found locally'
32                 return localfile
33             else:
34                 try:
35                     ret = pkg_resources.ZipProvider.get_resource_filename(self, manager, resource_name)
36                     #print 'returning %s' % (ret,)
37                     return ret
38                 except NotImplementedError:
39                     #print 'get_resource_filename(%s,%s): not found' % (self._module_name, resource_name)
40                     #import traceback
41                     #traceback.print_exc()
42                     return ''
43
44     pkg_resources.register_loader_type(zipimport.zipimporter, Provider)
45
46