From f00d1961d2ae8bb3a153bea011d2afa2acfc2102 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Thu, 11 Sep 2008 19:51:38 -0700
Subject: [PATCH] startstop_node.py: find twistd in our supportlib if we had to
 build Twisted as a setuptools dependency. This is a form of cgalvan's #505
 patch, simplified because now 'setup.py trial' takes care of sys.path and
 PYTHONPATH

---
 src/allmydata/scripts/startstop_node.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/allmydata/scripts/startstop_node.py b/src/allmydata/scripts/startstop_node.py
index 81c6f764..5863efb6 100644
--- a/src/allmydata/scripts/startstop_node.py
+++ b/src/allmydata/scripts/startstop_node.py
@@ -53,6 +53,21 @@ def do_start(basedir, profile=False, out=sys.stdout, err=sys.stderr):
         nodetype = "unknown (%s)" % tac
 
     cmd = find_exe.find_exe('twistd')
+    if not cmd:
+        # If 'twistd' wasn't on $PATH, maybe we're running from source and
+        # Twisted was built as one of our dependencies. If so, we're at
+        # BASEDIR/src/allmydata/scripts/startstop_node.py, and it's at
+        # BASEDIR/support/bin/twistd
+        up = os.path.dirname
+        TAHOEDIR = up(up(up(up(os.path.abspath(__file__)))))
+        bindir = os.path.join(TAHOEDIR, "support/bin")
+        maybe = os.path.join(bindir, "twistd")
+        if os.path.exists(maybe):
+            cmd = [maybe]
+            oldpath = os.environ.get("PATH", "").split(os.pathsep)
+            os.environ["PATH"] = os.pathsep.join(oldpath + [bindir])
+            # sys.path and $PYTHONPATH are taken care of by the extra code in
+            # 'setup.py trial'
     if not cmd:
         print "Can't find twistd (it comes with Twisted).  Aborting."
         sys.exit(1)
-- 
2.45.2