From: Zooko O'Whielacronx Date: Thu, 20 Sep 2007 19:37:50 +0000 (-0700) Subject: startstop_node.py: improve the hack of launching twistd X-Git-Tag: allmydata-tahoe-0.6.0~41 X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/frontends?a=commitdiff_plain;h=c2e310825dea48c8c856d03cdf4e054a38ca2f3b;p=tahoe-lafs%2Ftahoe-lafs.git startstop_node.py: improve the hack of launching twistd 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. --- diff --git a/src/allmydata/scripts/startstop_node.py b/src/allmydata/scripts/startstop_node.py index d6420fff..deea7dde 100644 --- a/src/allmydata/scripts/startstop_node.py +++ b/src/allmydata/scripts/startstop_node.py @@ -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