File: sxr-server

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (98 lines) | stat: -rwxr-xr-x 2,836 bytes parent folder | download | duplicates (2)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
#
# Copyright (C) 2004 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis import config

from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
import sys, os, os.path, getopt, socket

document_root = '.'
url = ''
src = ''

class RequestHandler(CGIHTTPRequestHandler):
   """This little demo server emulates apache's 'Alias' and 'ScriptAlias'
   options to serve source files and data generated from sxr.cgi"""

   def translate_path(self, path):

      # similar to an 'Alias <src> <document_root>' under apache...
      if path.startswith(src): # a file request...
         path = os.path.join(document_root, path[len(src) + 1:])
      # hack to be able to serve absolute URLs from the document root
      elif not path.endswith('sxr.cgi'):
         path = os.path.join(document_root, path[1:])

      return path

   def is_cgi(self):
      """The path either refers to the sxr cgi or to a (source) file."""

      if self.path.startswith(url):
         self.cgi_info = config.datadir + '/cgi-bin', 'sxr.cgi' + self.path[len(url):]
         return True

      return False
      

def usage():
   print 'Usage : %s [options] <input files>'%sys.argv[0]
   print """
List of options:

  -h, --help             help
  -p, --port             port to listen for requests
  -d, --document_root    document root, i.e. top level directory (for things like stylesheets)
  -u, --url              base url under which to reach sxr.cgi
  -s, --src              base url under which to reach source files
"""

def run():
   global document_root, url, src

   port = 8000
   env = {}
   opts, args = getopt.getopt(sys.argv[1:],
                              'p:d:u:s:h',
                              ['port=', 'document_root=', 'url=', 'src=', 'help'])
   for o, a in opts:
      if o == '-h' or o == '--help':
         usage()
         sys.exit(0)
      elif o == '-p' or o == '--port':
         port = int(a)
      elif o == '-d' or o == '--document_root':
         env['SXR_ROOT_DIR'] = a
         document_root = a
      elif o == '-u' or o == '--url':
         env['SXR_CGI_URL'] = a
         url = a
      elif o == '-s' or o == '--src':
         env['SXR_SRC_URL'] = a
         src = a

   if not os.path.isfile(os.path.join(document_root, 'xref_db')):
      print 'please set the document root to a valid value !'
      usage()
      sys.exit(-1)
      


   os.environ.update(env)

   httpd = HTTPServer(('', port), RequestHandler)
   print 'SXR server running, please connect to http://%s:%d%s ...'%(socket.gethostname(), port, src)
   try:
      httpd.serve_forever()
   except KeyboardInterrupt:
      print 'Keyboard Interrupt: exiting'

if __name__ == '__main__':
   run()