changeset 55:0fbe37b56e84

update index_app to use _indexroot files
author paulo
date Thu, 22 Aug 2013 23:53:56 -0700
parents 496714f2fd8c
children 0249782e231e
files index.fcgi index_app.py index_test_server.py
diffstat 3 files changed, 63 insertions(+), 36 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/index.fcgi	Thu Aug 22 23:53:56 2013 -0700
     1.3 @@ -0,0 +1,8 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +from flup.server.fcgi import WSGIServer
     1.7 +
     1.8 +from index_app import app
     1.9 +
    1.10 +
    1.11 +WSGIServer(app).run()
     2.1 --- a/index_app.py	Mon Jun 03 00:30:24 2013 -0700
     2.2 +++ b/index_app.py	Thu Aug 22 23:53:56 2013 -0700
     2.3 @@ -5,51 +5,46 @@
     2.4  import html
     2.5  
     2.6  
     2.7 -def _get_cwd(environ):
     2.8 -	ROOTPATH = "pauloang.com"
     2.9 -	path_info = environ.get("PATH_INFO", '')
    2.10 -	return [ROOTPATH] + [i for i in path_info.split('/') if len(i) > 0]
    2.11 -
    2.12 -
    2.13 -def _get_breadcrumb(cwd):
    2.14 -	h = html.HTML()
    2.15 -	for (i, d) in enumerate(cwd[:-1]):
    2.16 -		h.a(d, href='/' + '/'.join(cwd[1:i+1]))
    2.17 -		h.text('/')
    2.18 -	h.text(cwd[-1])
    2.19 -	return h
    2.20 +def read_indexroot(indexroot_fn):
    2.21 +	ret = ''
    2.22 +	with open(indexroot_fn) as f:
    2.23 +		try:
    2.24 +			ret = f.next().strip()
    2.25 +		except StopIteration:
    2.26 +			pass
    2.27 +	return ret
    2.28  
    2.29  
    2.30  def main(environ):
    2.31 -	cwd = _get_cwd(environ)
    2.32  	dirs = []
    2.33  	
    2.34  	for i in os.listdir("."):
    2.35 -		if i[0] != '.' and os.path.isdir(i):
    2.36 +		indexroot_fn = os.path.join(i, "_indexroot")
    2.37 +		if i[0] != '.' and os.path.isdir(i) and os.path.exists(indexroot_fn):
    2.38  			statobj = os.stat(i)
    2.39 -			mtime = datetime.datetime.fromtimestamp(statobj.st_mtime)
    2.40 -			modeok = (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH)
    2.41 -			dirs.append((i, modeok, mtime))
    2.42 +			if not (statobj.st_mode & stat.S_IXOTH) and (statobj.st_mode & stat.S_IROTH):
    2.43 +				continue
    2.44 +			indexroot_statobj = os.stat(indexroot_fn)
    2.45 +			mtime = datetime.datetime.fromtimestamp(indexroot_statobj.st_mtime)
    2.46 +			indexroot = read_indexroot(indexroot_fn)
    2.47 +			dirs.append((i, mtime, indexroot))
    2.48  	
    2.49  	root = html.HTML("html")
    2.50 -	TITLE_FMT = "-[%s]-" 
    2.51 -	title = TITLE_FMT % '/'.join(cwd)
    2.52 +	title = "-[pauloang.com]-"
    2.53  	
    2.54  	header = root.header
    2.55  	header.title(title)
    2.56  	header.link(rel="stylesheet", type="text/css", href="index.css")
    2.57  	
    2.58 -	body = root.body
    2.59 -	body.h1(TITLE_FMT % str(_get_breadcrumb(cwd)), escape=False)
    2.60 +	body = root.body(klass="body")
    2.61 +	body.h1(title, escape=False)
    2.62  	
    2.63 -	body_table = body.table(klass="index")
    2.64 +	for (i, mtime, indexroot) in sorted(dirs, key=lambda x: x[1], reverse=True):
    2.65 +		body.a(i, href=os.path.join(i, indexroot))
    2.66 +		body.br
    2.67  	
    2.68 -	for (i, modeok, mtime) in sorted(dirs, key=lambda x: x[2], reverse=True):
    2.69 -		if modeok:
    2.70 -			r = body_table.tr
    2.71 -			r.td.div(mtime.isoformat(), klass="index_date")
    2.72 -			r.td.a(i, href=i)
    2.73 -	
    2.74 +	body.div(klass="email").p.a("pbba13@gmail.com", href="mailto:pbba13@gmail.com")
    2.75 +
    2.76  	return unicode(root).encode("utf-8")
    2.77  
    2.78  
     3.1 --- a/index_test_server.py	Mon Jun 03 00:30:24 2013 -0700
     3.2 +++ b/index_test_server.py	Thu Aug 22 23:53:56 2013 -0700
     3.3 @@ -1,14 +1,38 @@
     3.4 -import wsgiref.simple_server 
     3.5 -import SocketServer
     3.6 +import os
     3.7 +import sys
     3.8 +import signal
     3.9 +
    3.10 +import cherrypy
    3.11 +from cherrypy import wsgiserver
    3.12  
    3.13  import index_app
    3.14  
    3.15  
    3.16 -class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
    3.17 -	pass
    3.18 +def sighandler(signum, frame):
    3.19 +	sys.stderr.write("Caught signal: %s \n" % signum)
    3.20 +	server.stop()
    3.21 +
    3.22 +
    3.23 +class FileServerRoot:
    3.24 +	def default(self, *args):
    3.25 +		if len(args) == 0:
    3.26 +			raise cherrypy.HTTPError(404)
    3.27 +
    3.28 +		filepath = os.path.abspath(os.path.join(*args))
    3.29 +		return cherrypy.lib.static.serve_file(filepath)
    3.30 +
    3.31 +	default.exposed = True
    3.32  
    3.33  
    3.34  if __name__ == "__main__":
    3.35 -	httpd = ThreadingWSGIServer(('', 8000), wsgiref.simple_server.WSGIRequestHandler)
    3.36 -	httpd.set_app(index_app.app)
    3.37 -	httpd.serve_forever()
    3.38 +	fileServerApp = cherrypy.Application(FileServerRoot())
    3.39 +	dispatcher = wsgiserver.WSGIPathInfoDispatcher({
    3.40 +		"/index.fcgi": index_app.app,
    3.41 +		"": fileServerApp,
    3.42 +	})
    3.43 +	server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), dispatcher)
    3.44 +
    3.45 +	signal.signal(signal.SIGINT, sighandler)
    3.46 +	signal.signal(signal.SIGTERM, sighandler)
    3.47 +
    3.48 +	server.start()