summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-09-11 09:51:00 -0400
committerschneefux <schneefux+commit@schneefux.xyz>2014-09-11 09:51:00 -0400
commitbf4cc2f886c5e3096871f3b8ab050afd388d58bd (patch)
treeaab058032455b428cfe05d98ec341f84028169f2
parentfa926e876485dfe81b288ac3ee8025c2c7f59895 (diff)
parent93280e2059c01e16c51848720d8e4d52b2c3deee (diff)
downloadjasper-client-bf4cc2f886c5e3096871f3b8ab050afd388d58bd.tar.gz
jasper-client-bf4cc2f886c5e3096871f3b8ab050afd388d58bd.zip
Merge pull request #160 from alexsiri7/diagnostic
Simple diagnostic tool for Jasper
-rwxr-xr-xboot/boot.py7
-rwxr-xr-xclient/diagnose.py105
-rw-r--r--client/test.py7
3 files changed, 110 insertions, 9 deletions
diff --git a/boot/boot.py b/boot/boot.py
index 5291ea2..8697c91 100755
--- a/boot/boot.py
+++ b/boot/boot.py
@@ -35,16 +35,15 @@ import traceback
lib_path = os.path.abspath('../client')
sys.path.append(lib_path)
+from diagnose import Diagnostics
import speaker as speak
speaker = speak.newSpeaker()
def testConnection():
- try:
- urllib2.urlopen("http://www.google.com").getcode()
+ if Diagnostics.check_network_connection():
print "CONNECTED TO INTERNET"
-
- except urllib2.URLError:
+ else:
print "COULD NOT CONNECT TO NETWORK"
speaker.say(
"Warning: I was unable to connect to a network. Parts of the system may not work correctly, depending on your setup.")
diff --git a/client/diagnose.py b/client/diagnose.py
new file mode 100755
index 0000000..935abbe
--- /dev/null
+++ b/client/diagnose.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python2
+import time
+import re
+import socket
+import os
+from subprocess import check_output, call
+
+class Diagnostics:
+ """
+ Set of diagnostics to be run for determining the health of the
+ host running Jasper
+
+ To add new checks, add a boolean returning method with a name that starts
+ with `check_`
+ """
+ @classmethod
+ def jasper_modules_path(cls):
+ return os.path.abspath('../../')
+
+ @classmethod
+ def check_network_connection(cls):
+ try:
+ # see if we can resolve the host name -- tells us if there is
+ # a DNS listening
+ host = socket.gethostbyname("www.google.com")
+ # connect to the host -- tells us if the host is actually
+ # reachable
+ s = socket.create_connection((host, 80), 2)
+ except Exception as e:
+ return False
+ else:
+ return True
+
+ @classmethod
+ def check_phonetisaurus_dictionary_file(cls):
+ return os.path.isfile(os.path.join(cls.jasper_modules_path(), "phonetisaurus/g014b2b.fst"))
+
+ @classmethod
+ def check_phonetisaurus_program(cls):
+ return call(['which', 'phonetisaurus-g2p']) == 0
+
+ @classmethod
+ def info_git_revision(cls):
+ return check_output(['git','rev-parse','HEAD'])
+
+class DiagnosticRunner:
+ """
+ Performs a series of checks against the system, printing the results to the
+ console and also saving them to diagnostic.log
+ """
+ def __init__(self, diagnostics):
+ self.diagnostics = diagnostics
+
+ def run(self):
+ self.initialize_log()
+ self.perform_checks()
+
+ def perform_checks(self):
+ self.failed_checks = 0
+ for check in self.select_methods('check'):
+ self.do_check(check)
+ for info in self.select_methods('info'):
+ self.get_info(info)
+ if self.failed_checks == 0:
+ self.log("All checks passed\n")
+ else:
+ self.log("%d checks failed\n" % self.failed_checks)
+
+
+ def select_methods(self, prefix):
+ def is_match(method_name):
+ return callable(getattr(self.diagnostics, method_name)) and re.match(r"\A" + prefix +"_", method_name)
+
+ return [method_name for method_name in dir(self.diagnostics) if is_match(method_name)]
+
+ def initialize_log(self):
+ self.output = open('diagnostic.log', 'w')
+ self.log("Starting jasper diagnostic\n")
+ self.log(time.strftime("%c")+"\n")
+
+ def log(self, msg):
+ print msg,
+ self.output.write(msg)
+
+ def get_info(self, info_name):
+ message = info_name.replace("info_", "").replace("_", " ")
+ info_method = getattr(self.diagnostics, info_name)
+ info = info_method()
+ self.log("%s: %s" % (message, info))
+
+ def do_check(self, check_name):
+ message = check_name.replace("check_", "").replace("_", " ")
+ check = getattr(self.diagnostics, check_name)
+ self.log("Checking %s... " % message)
+ if check():
+ self.log("OK")
+ else:
+ self.failed_checks += 1
+ self.log("FAILED")
+ self.log("\n")
+
+
+if __name__ == '__main__':
+ DiagnosticRunner(Diagnostics).run()
+
diff --git a/client/test.py b/client/test.py
index a8f12be..9f23e00 100644
--- a/client/test.py
+++ b/client/test.py
@@ -13,6 +13,7 @@ from urllib2 import URLError, urlopen
import test_mic
import g2p
import brain
+from diagnose import Diagnostics
DEFAULT_PROFILE = {
'prefers_email': False,
@@ -23,11 +24,7 @@ DEFAULT_PROFILE = {
def activeInternet():
- try:
- urlopen('http://www.google.com', timeout=1)
- return True
- except URLError:
- return False
+ return Diagnostics.check_network_connection()
class TestMic(unittest.TestCase):