2 Commits

Author SHA1 Message Date
a8ecad9329 Use NilmDB serializer for ProcessManager 2013-07-24 14:55:59 -04:00
5b878378f3 Translate UTF-8 in command output more robustly 2013-07-22 13:03:09 -04:00
4 changed files with 30 additions and 4 deletions

View File

@@ -7,7 +7,7 @@ Prerequisites:
sudo apt-get install python2.7 python-setuptools sudo apt-get install python2.7 python-setuptools
# Plus nilmdb and its dependencies # Plus nilmdb and its dependencies
nilmdb (1.8.2+) nilmdb (1.8.3+)
Install: Install:

View File

@@ -22,6 +22,7 @@ from nilmdb.server.serverutil import (
cherrypy_stop, cherrypy_stop,
bool_param, bool_param,
) )
from nilmdb.utils import serializer_proxy
import nilmrun import nilmrun
import nilmrun.testfilter import nilmrun.testfilter
@@ -60,12 +61,16 @@ class AppProcess(object):
self.manager = manager self.manager = manager
def process_status(self, pid): def process_status(self, pid):
# We need to convert the log (which is bytes) to Unicode
# characters, in order to send it via JSON. Treat it as UTF-8
# but replace invalid characters with markers.
log = self.manager[pid].log.decode('utf-8', errors='replace')
return { return {
"pid": pid, "pid": pid,
"alive": self.manager[pid].alive, "alive": self.manager[pid].alive,
"exitcode": self.manager[pid].exitcode, "exitcode": self.manager[pid].exitcode,
"start_time": self.manager[pid].start_time, "start_time": self.manager[pid].start_time,
"log": self.manager[pid].log, "log": log
} }
# /process/status # /process/status
@@ -200,8 +205,11 @@ class Server(object):
# error messages. # error messages.
cherrypy._cperror._ie_friendly_error_sizes = {} cherrypy._cperror._ie_friendly_error_sizes = {}
# The manager maintains internal state and isn't necessarily
# thread-safe, so wrap it in the serializer.
manager = serializer_proxy(nilmrun.processmanager.ProcessManager)()
# Build up the application and mount it # Build up the application and mount it
manager = nilmrun.processmanager.ProcessManager()
root = App() root = App()
root.process = AppProcess(manager) root.process = AppProcess(manager)
root.run = AppRun(manager) root.run = AppRun(manager)

View File

@@ -61,7 +61,7 @@ setup(name='nilmrun',
long_description = "NILM Database Filter Runner", long_description = "NILM Database Filter Runner",
license = "Proprietary", license = "Proprietary",
author_email = 'jim@jtan.com', author_email = 'jim@jtan.com',
install_requires = [ 'nilmdb >= 1.8.2', install_requires = [ 'nilmdb >= 1.8.3',
'psutil >= 0.3.0', 'psutil >= 0.3.0',
'cherrypy >= 3.2', 'cherrypy >= 3.2',
'simplejson', 'simplejson',

View File

@@ -372,3 +372,21 @@ class TestClient(object):
# kill all processes # kill all processes
for pid in client.get("process/list"): for pid in client.get("process/list"):
client.post("process/remove", { "pid": pid }) client.post("process/remove", { "pid": pid })
def test_client_10_unicode(self):
client = HTTPClient(baseurl = testurl, post_json = True)
eq_(client.get("process/list"), [])
def verify(cmd, result):
pid = client.post("run/command", { "argv": [ "sh", "-c", cmd ] })
eq_(client.get("process/list"), [pid])
status = self.wait_end(client, pid)
eq_(result, status["log"])
# Unicode should work
verify("echo -n hello", "hello")
verify(u"echo -n ☠", u"")
verify("echo -ne \\\\xe2\\\\x98\\\\xa0", u"")
# Programs that spit out invalid UTF-8 should get replacement
# markers
verify("echo -ne \\\\xae", u"\ufffd")