From 9ed5533760d959b617587b54620377ac3dc66480 Mon Sep 17 00:00:00 2001
From: robk-tahoe <robk-tahoe@allmydata.com>
Date: Thu, 10 Jan 2008 15:23:41 -0700
Subject: [PATCH] added tweaked sibpath implementation

use of twisted.python.util.sibpath to find files relative to modules doesn't
work when those modules are bundled into a library by py2exe.  this provides
an alternative implementation (in allmydata.util.sibpath) which checks for
the existence of the file, and if it is not found, attempts to find it relative
to sys.executable instead.
---
 src/allmydata/provisioning.py |  5 ++---
 src/allmydata/util/sibpath.py | 24 ++++++++++++++++++++++++
 src/allmydata/webish.py       |  8 ++++----
 3 files changed, 30 insertions(+), 7 deletions(-)
 create mode 100644 src/allmydata/util/sibpath.py

diff --git a/src/allmydata/provisioning.py b/src/allmydata/provisioning.py
index e7bf5ef0..74d3ff53 100644
--- a/src/allmydata/provisioning.py
+++ b/src/allmydata/provisioning.py
@@ -1,11 +1,10 @@
 
 from nevow import inevow, loaders, rend, tags as T
-from twisted.python import util
 import math
-from allmydata.util import mathutil
+from allmydata.util import mathutil, sibpath
 
 def getxmlfile(name):
-    return loaders.xmlfile(util.sibpath(__file__, "web/%s" % name))
+    return loaders.xmlfile(sibpath.sibpath(__file__, "web/%s" % name))
 
 # factorial and binomial copied from
 # http://mail.python.org/pipermail/python-list/2007-April/435718.html
diff --git a/src/allmydata/util/sibpath.py b/src/allmydata/util/sibpath.py
new file mode 100644
index 00000000..80a2801a
--- /dev/null
+++ b/src/allmydata/util/sibpath.py
@@ -0,0 +1,24 @@
+import os
+import sys
+from twisted.python.util import sibpath as tsibpath
+
+def sibpath(path, sibling):
+    """
+    Looks for a named sibling relative to the given path.  If such a file
+    exists, its path will be returned, otherwise a second search will be
+    made for the named sibling relative to the path of the executable
+    currently running.  This is useful in the case that something built
+    with py2exe, for example, needs to find data files relative to its
+    install.  Note hence that care should be taken not to search for
+    private package files whose names might collide with files which might
+    be found installed alongside the python interpreter itself.  If no
+    file is found in either place, the sibling relative to the given path
+    is returned, likely leading to a file not found error.
+    """
+    sib = tsibpath(path, sibling)
+    if not os.path.exists(sib):
+        exe_sib = tsibpath(sys.executable, sibling)
+        if os.path.exists(exe_sib):
+            return exe_sib
+    return sib
+
diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py
index 43daecea..f9e4aa7d 100644
--- a/src/allmydata/webish.py
+++ b/src/allmydata/webish.py
@@ -3,12 +3,12 @@ from base64 import b32encode
 import os.path
 from twisted.application import service, strports, internet
 from twisted.web import static, resource, server, html, http
-from twisted.python import util, log
+from twisted.python import log
 from twisted.internet import defer
 from twisted.internet.interfaces import IConsumer
 from nevow import inevow, rend, loaders, appserver, url, tags as T
 from nevow.static import File as nevow_File # TODO: merge with static.File?
-from allmydata.util import fileutil
+from allmydata.util import fileutil, sibpath
 import simplejson
 from allmydata.interfaces import IDownloadTarget, IDirectoryNode, IFileNode, \
      IMutableFileNode
@@ -21,7 +21,7 @@ import urllib
 from formless import webform
 
 def getxmlfile(name):
-    return loaders.xmlfile(util.sibpath(__file__, "web/%s" % name))
+    return loaders.xmlfile(sibpath.sibpath(__file__, "web/%s" % name))
 
 class IClient(Interface):
     pass
@@ -1302,7 +1302,7 @@ class Root(rend.Page):
         return rend.Page.locateChild(self, ctx, segments)
 
     child_webform_css = webform.defaultCSS
-    child_tahoe_css = nevow_File(util.sibpath(__file__, "web/tahoe.css"))
+    child_tahoe_css = nevow_File(sibpath.sibpath(__file__, "web/tahoe.css"))
 
     child_provisioning = provisioning.ProvisioningTool()
 
-- 
2.45.2