Skip to content

Commit

Permalink
Rename control of starting and stopping Consenso
Browse files Browse the repository at this point in the history
ConsensoProcess seems clearer; we'll keep out the IRC controls from this
object, put them somewhere else.
  • Loading branch information
Danny O'Brien authored and Danny O'Brien committed Aug 16, 2012
1 parent 5022345 commit 9076da4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
65 changes: 33 additions & 32 deletions consenso/bot/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
##
# consenso/bot/ircclient.py
# consenso/bot/client
###
"""consenso/bot/ircclient.py
"""consenso/bot/client
"""

Expand All @@ -11,7 +11,6 @@
__contributors__ = None
__license__ = "GPL v3"

import urlparse
import subprocess
import os.path
import tempfile
Expand All @@ -20,56 +19,58 @@

import consenso.directories

furlfile = os.path.join(consenso.directories.foolscap_dir, "root.furl")

class ConsensoProcess(object):

class BotClient(object):
def __init__(self):
self.pid = None
self.furl = None
self.pidfile = None
furlfile = os.path.join(consenso.directories.foolscap_dir, "root.furl")

def run(self, pidfile=None, logfile=None):
this_dir = os.path.split(__file__)[0]
command = ['twistd', '--python={}'.format(os.path.join(this_dir, 'tac.py'))]
def __init__(self, pidfile=None, logfile=None):
self.pid = None
self._furl = None
if not pidfile:
(f, pidfile) = tempfile.NamedTemporaryFile(
prefix="consensobot", suffix="pid", delete=False)
f.close()
if not logfile:
logfile = os.path.join(consenso.directories.log_dir, 'client.log')
self.pidfile = pidfile
command.append("--pidfile={}".format(pidfile))
self.logfile = logfile
command.append("--logfile={}".format(logfile))
if os.path.exists(furlfile):
os.unlink(furlfile)
self._pidfile = pidfile
self._logfile = logfile

def start(self):
this_dir = os.path.split(__file__)[0]
command = ['twistd', '--python={}'.format(os.path.join(this_dir, 'tac.py'))]
command.append("--pidfile={}".format(self._pidfile))
command.append("--logfile={}".format(self._logfile))
if os.path.exists(self.furlfile):
os.unlink(self.furlfile)
subprocess.call(command, stderr=subprocess.STDOUT)
pid = 0
furl = ""
while (pid == 0 or furl == ""): # FIXME shouldn't buzz around a loop, should use select
try:
pid = int(file(self.pidfile, "r").read())
pid = int(file(self._pidfile, "r").read())
except (IOError, ValueError):
pid = 0
try:
furl = file(furlfile, "r").read()
furl = file(self.furlfile, "r").read()
except:
furl = ""
self.pid = pid
self.furl = furl
self._furl = furl

def join(self, url):
components = urlparse.urlparse(url, scheme='irc')
self.url = url
self.server_port = components.port
self.server_hostname = components.hostname
self.server_groups = [components.path.strip('/')]
def furl(self):
if not self._furl:
try:
furl = file(self.furlfile, "r").read()
except IOError:
furl = None
self._furl = furl
return self._furl

def kill(self):
def shutdown(self):
if self.pid:
os.kill(self.pid, signal.SIGTERM)
if self.pidfile:
os.unlink(self.pidfile)
if furlfile:
os.unlink(furlfile)
if self._pidfile:
os.unlink(self._pidfile)
if self.furlfile:
os.unlink(self.furlfile)
25 changes: 11 additions & 14 deletions consenso/bot/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,34 @@
__contributors__ = None
__license__ = "GPL v3"

#import unittest
import os
import os.path
import signal

from consenso.bot.client import BotClient

from consenso.bot.client import ConsensoProcess
import twisted.trial.unittest as unittest


class Test_BotClient(unittest.TestCase):
class Test_ConsensoProcess(unittest.TestCase):
def setUp(self):
self.ic = BotClient()

def test_creates_daemon(self):
if os.path.exists("/tmp/pidfile"):
os.unlink("/tmp/pidfile")
self.ic.run(pidfile="/tmp/pidfile")
self.ic = ConsensoProcess(pidfile="/tmp/pidfile")
self.ic.start()

def test_creates_daemon(self):
pid = int(open('/tmp/pidfile', 'r').read())
if pid:
os.kill(pid, signal.SIGTERM)
self.assertGreater(pid, 0)

def test_got_pid(self):
if os.path.exists("/tmp/pidfile"):
os.unlink("/tmp/pidfile")
self.ic.run(pidfile="/tmp/pidfile")
pid = int(open('/tmp/pidfile', 'r').read())
self.assertEqual(pid, self.ic.pid)

def test_got_furl(self):
self.assert_(self.ic.furl())

def tearDown(self):
self.ic.kill()
self.ic.shutdown()

if __name__ == '__main__':
import __main__
Expand Down

0 comments on commit 9076da4

Please sign in to comment.