Index: package_branches/invirt-web/cherrypy/code/ajaxterm.py
===================================================================
--- package_branches/invirt-web/cherrypy/code/ajaxterm.py	(revision 2454)
+++ package_branches/invirt-web/cherrypy/code/ajaxterm.py	(revision 2455)
@@ -3,11 +3,5 @@
 """ Ajaxterm """
 
-import array,cgi,fcntl,glob,hashlib,mimetypes,optparse,os,pty,random,re,signal,select,sys,threading,time,termios,struct,pwd
-
-os.chdir(os.path.normpath(os.path.dirname(__file__)))
-# Optional: Add QWeb in sys path
-sys.path[0:0]=glob.glob('../../python')
-
-import qweb
+import array,atexit,cgi,fcntl,glob,hashlib,mimetypes,optparse,os,pty,random,re,signal,select,sys,threading,time,termios,struct,pwd
 
 class Terminal:
@@ -372,4 +366,5 @@
 		self.thread.daemon=True
 		self.alive=1
+		atexit.register(self.die)
 		# synchronize methods
 		for name in ['create','fds','proc_read','proc_write','dump','die','run']:
@@ -454,96 +449,2 @@
 			except (IOError,OSError):
 				pass
-
-class AjaxTerm:
-	def __init__(self,cmd=None,index_file='ajaxterm.html'):
-		self.files={}
-		for i in ['css','html','js']:
-			for j in glob.glob('*.%s'%i):
-				self.files[j]=file(j).read()
-		self.files['index']=file(index_file).read()
-		self.mime = mimetypes.types_map.copy()
-		self.mime['.html']= 'text/html; charset=UTF-8'
-		self.multi = Multiplex(cmd)
-		self.session = {}
-	def __call__(self, environ, start_response):
-		req = qweb.QWebRequest(environ, start_response,session=None)
-		if req.PATH_INFO.endswith('/u'):
-			s=req.REQUEST["s"]
-			k=req.REQUEST["k"]
-			c=req.REQUEST["c"]
-			w=req.REQUEST.int("w")
-			h=req.REQUEST.int("h")
-			if s in self.session:
-				term=self.session[s]
-			else:
-				if not (w>2 and w<256 and h>2 and h<100):
-					w,h=80,25
-				term=self.session[s]=self.multi.create(w,h)
-			if k:
-				self.multi.proc_write(term,k)
-			time.sleep(0.002)
-			dump=self.multi.dump(term,c)
-			req.response_headers['Content-Type']='text/xml'
-			if isinstance(dump,str):
-				req.write(dump)
-				req.response_gzencode=1
-			else:
-				del self.session[s]
-				req.write('<?xml version="1.0"?><idem></idem>')
-#			print "sessions %r"%self.session
-		else:
-			n=os.path.basename(req.PATH_INFO)
-			if n in self.files:
-				req.response_headers['Content-Type'] = self.mime.get(os.path.splitext(n)[1].lower(), 'application/octet-stream')
-				req.write(self.files[n])
-			else:
-				req.response_headers['Content-Type'] = 'text/html; charset=UTF-8'
-				req.write(self.files['index'])
-		return req
-
-def main():
-	parser = optparse.OptionParser()
-	parser.add_option("-p", "--port", dest="port", default="8022", help="Set the TCP port (default: 8022)")
-	parser.add_option("-c", "--command", dest="cmd", default=None,help="set the command (default: /bin/login or ssh localhost)")
-	parser.add_option("-l", "--log", action="store_true", dest="log",default=0,help="log requests to stderr (default: quiet mode)")
-	parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=0, help="run as daemon in the background")
-	parser.add_option("-P", "--pidfile",dest="pidfile",default="/var/run/ajaxterm.pid",help="set the pidfile (default: /var/run/ajaxterm.pid)")
-	parser.add_option("-i", "--index", dest="index_file", default="ajaxterm.html",help="default index file (default: ajaxterm.html)")
-	parser.add_option("-u", "--uid", dest="uid", help="Set the daemon's user id")
-	(o, a) = parser.parse_args()
-	if o.daemon:
-		pid=os.fork()
-		if pid == 0:
-			#os.setsid() ?
-			os.setpgrp()
-			nullin = file('/dev/null', 'r')
-			nullout = file('/dev/null', 'w')
-			os.dup2(nullin.fileno(), sys.stdin.fileno())
-			os.dup2(nullout.fileno(), sys.stdout.fileno())
-			os.dup2(nullout.fileno(), sys.stderr.fileno())
-			if os.getuid()==0 and o.uid:
-				try:
-					os.setuid(int(o.uid))
-				except:
-					os.setuid(pwd.getpwnam(o.uid).pw_uid)
-		else:
-			try:
-				file(o.pidfile,'w+').write(str(pid)+'\n')
-			except:
-				pass
-			print 'AjaxTerm at http://localhost:%s/ pid: %d' % (o.port,pid)
-			sys.exit(0)
-	else:
-		print 'AjaxTerm at http://localhost:%s/' % o.port
-	at=AjaxTerm(o.cmd,o.index_file)
-#	f=lambda:os.system('firefox http://localhost:%s/&'%o.port)
-#	qweb.qweb_wsgi_autorun(at,ip='localhost',port=int(o.port),threaded=0,log=o.log,callback_ready=None)
-	try:
-		qweb.QWebWSGIServer(at,ip='localhost',port=int(o.port),threaded=0,log=o.log).serve_forever()
-	except KeyboardInterrupt,e:
-		sys.excepthook(*sys.exc_info())
-	at.multi.die()
-
-if __name__ == '__main__':
-	main()
-
