summaryrefslogtreecommitdiff
path: root/pagehandler.py
blob: 3933fce8b9165cdd95f68ed44622cab926a71e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# -*- coding: utf-8 -*-

# main (wsgi) request handler for patch-tracking system
# this handler file basically farms out all its work to the ReqHandler
# module, which in turn splits up the request URI and acts accordingly.

import os
import sys

def application(env, resp):
  # everything is currently run relative to the pagehandler script.
  whereami = os.path.dirname(env['SCRIPT_FILENAME'])
  sys.path+=[whereami]
  os.chdir(whereami)

  import patchtracker.ReqHandler as ReqHandler
  try:
    cmdh = ReqHandler.CmdHandler(env)
    resp(cmdh.status, cmdh.headers)
    return cmdh.output()
  except ReqHandler.ReqHandlerException, e:
    resp(e.status, [('Content-Type', 'text/html')])
    return ReqHandler.ErrorCmd(str(e), e.status).output()

if __name__ == '__main__':
  os.environ['SCRIPT_FILENAME'] = sys.argv[0]
  from wsgiref import simple_server as ss
  import getopt
  import profile, pstats

  def cmd_help():
    print """
usage: %s [-hp]

  -h: help message
  -p: enable profiling
"""%(os.path.basename(sys.argv[0]))

  opts,args = getopt.getopt(sys.argv[1:], "hp")
  profiling = False
  for o,v in opts:
    if o == "-h":
      cmd_help()
      sys.exit(0)
    if o == "-p":
      profiling = True

  print "pagehandler test server running..."
  server = ss.WSGIServer( ('',8080), ss.WSGIRequestHandler)
  server.set_app(application)
  while True:
    try:
      if not profiling:
        server.handle_request()
      else:
        profile.run("server.handle_request()", "profile.out")
        p = pstats.Stats("profile.out")
        p.strip_dirs().sort_stats('time').print_stats(20)
    except IOError, e:
      print "adsf: ",e