Commits

Dan Boitnott  committed 075389b

Web server now runs in background

  • Participants
  • Parent commits e1c6d2c

Comments (0)

Files changed (3)

File src/prise.py

 import argparse
-import sys
 
 import pexpect
 import state
 import scripts
+import web
 
 argParser = argparse.ArgumentParser()
 argParser.add_argument("script")
 env = { "PS1"           : "prise:\\h:\\W \\u\\$ ",
         "PRISE_RUN_DIR" : state.runDir }
 
+# Start the web server
+state.webServer = web.Server()
+print "Web server listening on port " + str(state.webServer.server_address[1])
+state.webServer.start()
+
 # If the script starts with a comment, show the user before spawning the sub-shell
 if type(state.script[0]) is scripts.CommentTask:
     state.runNextCmd()

File src/state.py

 curCmdLog = None
 curCmdLogLock = threading.RLock()
 
+# Web interface
+webServer = None
+
 def inputFilter(str):
     if str not in hotkeys:
         return str
 
 def shutdown():
     multiOutput.close()
+    if webServer:
+        webServer.shutdown()
 
 def getLastCmdIdx():
     with scriptLock:
 import os.path
+import SocketServer
 import BaseHTTPServer
 import mimetypes
 import shutil
 import urlparse
 import traceback
 import socket
+import threading
 
 import pystache
+import state
+import scripts
 
 RESOURCES = "resources"
 STATIC = "static"
 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
     renderer = pystache.Renderer(search_dirs=[resourcePath(TEMPLATES)], missing_tags='strict')
 
+    def log_message(self, format, *args):
+        self.server.logFile.write("%s - - [%s] %s\n" %
+                                  (self.address_string(),
+                                  self.log_date_time_string(),
+                                  format%args))
+
     def do_GET(self):
         if self.path == '/':
             (cmd, subPath) = ("index", '')
         self.wfile.write("1")
 
     def cmd_index(self, path, query):
-        self.sendTemplate("index")
+        self.log_message
+        tasks = []
+        for task in state.script:
+            tasks.append({
+                "isCommentTask" : type(task) is scripts.CommentTask,
+                "isCommandTask" : type(task) is scripts.CommandTask,
+                "content"       : task.content
+            })
+
+        self.sendTemplate("index", tasks=tasks)
+
+class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
+    def __init__(self):
+        BaseHTTPServer.HTTPServer.__init__(self, ('127.0.0.1', 8088), Handler)
+        self.logFile = open(os.path.join(state.runDir, "web.log"), 'a')
+
+    def start(self):
+        self.workThread = threading.Thread(target=self.serve_forever)
+        self.workThread.start()
 
 if __name__ == '__main__':
     server = BaseHTTPServer.HTTPServer(('127.0.0.1', 8088), Handler)