From 4662f282cf736e5f7c7e79ffd020b20995c73f5d Mon Sep 17 00:00:00 2001
From: robk-tahoe <robk-tahoe@allmydata.com>
Date: Wed, 23 Jan 2008 18:52:43 -0700
Subject: [PATCH] further fixes to windows build (pkg_resources / web
 templates)

now that web templates are found via pkg_resources, then the windows build
should in fact _use_ pkg_resources, rather than exclude it from the build
to prevent nevow exploding upon import due to the zip provider exception,
so that the pkgreshook can do install location based lookups
---
 windows/pkgreshook.py | 46 +++++++++++++++++++++++++++++++++++++++++++
 windows/setup.py      |  1 -
 windows/tahoe.py      |  2 ++
 windows/tahoesvc.py   |  3 +++
 4 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 windows/pkgreshook.py

diff --git a/windows/pkgreshook.py b/windows/pkgreshook.py
new file mode 100644
index 00000000..bb1d985c
--- /dev/null
+++ b/windows/pkgreshook.py
@@ -0,0 +1,46 @@
+
+def install():
+    """
+    This installs a hook into setuptools' pkg_resources infrastructure, so that resource
+    files can be found in files relative to the runnin executable, in addition to the
+    usual egg and source lookup mechanisms.  This overrides the ZipProvider, since that
+    is the lookup mechanism triggered within pkg_resources when running code out of a
+    py2exe or py2app build's library.zip.
+    """
+    import os, sys
+    import pkg_resources, zipimport
+
+    platform_libdirs = {
+        'darwin': '../Resources/pkg_resources',
+        }
+    exedir = os.path.dirname(sys.executable)
+    libdir = platform_libdirs.get(sys.platform, 'pkg_resources')
+
+    class Provider(pkg_resources.ZipProvider):
+
+        def __init__(self, module):
+            self._module_name = module.__name__
+            pkg_resources.ZipProvider.__init__(self, module)
+
+        def get_resource_filename(self, manager, resource_name):
+            #print 'get_resource_filename(%s, %s)' % (manager, resource_name)
+            path = [exedir, libdir] + self._module_name.split('.') + [resource_name]
+            localfile = os.path.join(*path)
+            #print '             checking(%s)' % (localfile,)
+            if os.path.exists(localfile):
+                #print 'found locally'
+                return localfile
+            else:
+                try:
+                    ret = pkg_resources.ZipProvider.get_resource_filename(self, manager, resource_name)
+                    #print 'returning %s' % (ret,)
+                    return ret
+                except NotImplementedError:
+                    #print 'get_resource_filename(%s,%s): not found' % (self._module_name, resource_name)
+                    #import traceback
+                    #traceback.print_exc()
+                    return ''
+
+    pkg_resources.register_loader_type(zipimport.zipimporter, Provider)
+
+
diff --git a/windows/setup.py b/windows/setup.py
index 9ebf9166..62e5e71e 100644
--- a/windows/setup.py
+++ b/windows/setup.py
@@ -65,7 +65,6 @@ setup_args = {
     'options': {
         "py2exe": {
             "excludes": [
-                "pkg_resources",
             ],
             "includes": [
             ],
diff --git a/windows/tahoe.py b/windows/tahoe.py
index f7ceecda..50dd05c7 100644
--- a/windows/tahoe.py
+++ b/windows/tahoe.py
@@ -1,3 +1,5 @@
+import pkgreshook # override the pkg_resources zip provider for py2exe deployment
+pkgreshook.install() # this is done before nevow is imported by depends
 import depends # import dependencies so that py2exe finds them
 _junk = depends # appease pyflakes
 
diff --git a/windows/tahoesvc.py b/windows/tahoesvc.py
index d338b95d..f5db9d23 100644
--- a/windows/tahoesvc.py
+++ b/windows/tahoesvc.py
@@ -22,6 +22,9 @@ logmsg('service loaded')
 #
 # Now with some bootstrap util functions in place, let's try and init things:
 try:
+    import pkgreshook # override the pkg_resources zip provider for py2exe deployment
+    pkgreshook.install() # this is done before nevow is imported
+
     logmsg('loading base dir')
     from allmydata.windows import registry
     basedir = registry.get_base_dir_path()
-- 
2.45.2