]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
startstop_node.py: improve the hack of launching twistd
authorZooko O'Whielacronx <zooko@zooko.com>
Thu, 20 Sep 2007 19:37:50 +0000 (12:37 -0700)
committerZooko O'Whielacronx <zooko@zooko.com>
Thu, 20 Sep 2007 19:37:50 +0000 (12:37 -0700)
Thanks to Brian for helping me figure out the cleaner way to do this: take the
first result from which("twistd"), and if it has the extension ".bat" or
".exe" then execute it, else execute python and give it as the first argument.

src/allmydata/scripts/startstop_node.py

index d6420fffb503d97d61e397e2a863564030b99a5f..deea7dded158f1ad44ac94ea541f4c836326a579 100644 (file)
@@ -1,5 +1,5 @@
 
-import os, sys, signal, time, subprocess
+import os, sys, signal, time
 from twisted.python import usage
 from allmydata.scripts.common import BasedirMixin
 from allmydata.util import fileutil
@@ -25,38 +25,6 @@ class RestartOptions(BasedirMixin, usage.Options):
          "of 'restart'"],
         ]
 
-
-def testtwistd(loc):
-    try:
-        return subprocess.call(["python", loc,], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-    except:
-        return -1
-
-def find_twistd():
-    for maybetwistd in which("twistd"):
-        ret = testtwistd(maybetwistd)
-        if ret == 0:
-            return maybetwistd
-
-    for maybetwistd in which("twistd.py"):
-        ret = testtwistd(maybetwistd)
-        if ret == 0:
-            return maybetwistd
-
-    maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd')
-    ret = testtwistd(maybetwistd)
-    if ret == 0:
-        return maybetwistd
-
-    maybetwistd = os.path.join(sys.prefix, 'Scripts', 'twistd.py')
-    ret = testtwistd(maybetwistd)
-    if ret == 0:
-        return maybetwistd
-
-    print "Can't find twistd (it comes with Twisted).  Aborting."
-    sys.exit(1)
-
-
 def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
     print >>out, "STARTING", basedir
     if os.path.exists(os.path.join(basedir, "client.tac")):
@@ -70,10 +38,26 @@ def do_start(basedir, config, out=sys.stdout, err=sys.stderr):
         if not os.path.isdir(basedir):
             print >>err, " in fact, it doesn't look like a directory at all!"
         return 1
-    twistd = find_twistd()
+    twistds = which("twistd")
+    if not twistds:
+        print "Can't find twistd (it comes with Twisted).  Aborting."
+        sys.exit(1)
+    twistd = twistds[0]
+    path, ext = os.path.splitext(twistd)
+    if ext.lower() in [".exe", ".bat",]:
+        cmd = [twistd,]
+    else:
+        cmd = [sys.executable, twistd,]
+    
     fileutil.make_dirs(os.path.join(basedir, "logs"))
-    cmd = ["python", twistd, "-y", tac, "--logfile", "logs/twistd.log"]
-    rc = subprocess.call(cmd, cwd=basedir)
+    cmd.extend(["-y", tac, "--logfile", os.path.join("logs", "twistd.log")])
+    print "os.chdir(%s)" % (basedir,)
+    curdir = os.getcwd()
+    try:
+        os.chdir(basedir)
+        rc = os.system(' '.join(cmd))
+    finally:
+        os.chdir(curdir)
     if rc == 0:
         print >>out, "%s node probably started" % type
         return 0