Changeset 1082 in products


Ignore:
Timestamp:
Jul 14, 2009 2:18:33 PM (15 years ago)
Author:
koval
Message:

added static wsgi application with folder listings

Location:
quintagroup.distrpoxy/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.distrpoxy/trunk

    • Property svn:ignore set to
      *.egg-info
      *.pyc
  • quintagroup.distrpoxy/trunk/quintagroup/distproxy/wsgi.py

    r1066 r1082  
    44from paste.script.appinstall import Installer as BaseInstaller 
    55from paste.fileapp import FileApp 
     6from paste import urlparser 
     7from paste import request 
    68from paste.httpexceptions import HTTPNotFound 
    79from spider import webmirror 
     
    2426    def __call__(self, environ, start_response): 
    2527        path = environ.get('PATH_INFO', '').strip() 
    26         filename = self.checkCache(path)         
    27         if path.split('/')[0] == "favicon.ico": 
    28                 return HTTPNotFound()(environ, start_response) 
     28        if path.split('/')[1] == "favicon.ico": 
     29            return HTTPNotFound()(environ, start_response) 
     30        filename = self.checkCache(path) 
    2931        if filename is None: 
    3032            return HTTPNotFound()(environ, start_response) 
     
    3941            pth = '/'.join(pth.split('/')[:-1]) 
    4042        if not os.path.exists(pth1): 
    41             webmirror(pth,1,self.index_url+path,0,1)             
     43            webmirror(pth,1,self.index_url+path,0,1) 
    4244        if pth1: 
    4345            return pth1 
     
    5052    index_url = local_conf.get('index', None) 
    5153    return PackageProxyApp(index_url, pack_dir) 
     54 
     55class StaticURLParser(urlparser.StaticURLParser): 
     56 
     57    def __call__(self, environ, start_response): 
     58        path_info = environ.get('PATH_INFO', '') 
     59        if not path_info: 
     60            return self.add_slash(environ, start_response) 
     61        if path_info == '/': 
     62            # @@: This should obviously be configurable 
     63            filename = 'index.html' 
     64        else: 
     65            filename = request.path_info_pop(environ) 
     66        full = os.path.normcase(os.path.normpath( 
     67            os.path.join(self.directory, filename))) 
     68        if os.path.sep != '/': 
     69            full = full.replace('/', os.path.sep) 
     70        if self.root_directory is not None and not full.startswith(self.root_directory): 
     71            # Out of bounds 
     72            return self.not_found(environ, start_response) 
     73        if not os.path.exists(full): 
     74            if full.endswith('index.html') and not os.path.isfile(full): 
     75                start_response('200 OK', []) 
     76                return [self.get_index_html()] 
     77            return self.not_found(environ, start_response) 
     78        if os.path.isdir(full): 
     79            # @@: Cache? 
     80            child_root = self.root_directory is not None and \ 
     81                self.root_directory or self.directory 
     82            return self.__class__(full, root_directory=child_root, 
     83                                  cache_max_age=self.cache_max_age)(environ, 
     84                                                                   start_response) 
     85        if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/': 
     86            return self.error_extra_path(environ, start_response) 
     87        if_none_match = environ.get('HTTP_IF_NONE_MATCH') 
     88        if if_none_match: 
     89            mytime = os.stat(full).st_mtime 
     90            if str(mytime) == if_none_match: 
     91                headers = [] 
     92                ETAG.update(headers, mytime) 
     93                start_response('304 Not Modified', headers) 
     94                return [''] # empty body 
     95 
     96        fa = self.make_app(full) 
     97        if self.cache_max_age: 
     98            fa.cache_control(max_age=self.cache_max_age) 
     99        return fa(environ, start_response) 
     100 
     101    def get_index_html(self): 
     102        path = self.directory 
     103        # create sorted lists of directories and files 
     104        names = os.listdir(path) 
     105        dirs = [i for i in names if os.path.isdir(os.path.join(path, i))] 
     106        dirs.sort() 
     107        files = [i for i in names if os.path.isfile(os.path.join(path, i))] 
     108        files.sort() 
     109        names = dirs + files 
     110        links = '\n'.join(['<li><a href="%s">%s</a></li>' %  (i, i) for i in names]) 
     111        template = open(os.path.join(os.path.dirname(__file__), 'index.html')).read() 
     112        return template % {'path': path[len(self.root_directory):], 'links': links} 
     113 
     114def make_static(global_conf, document_root, cache_max_age=None): 
     115    """ 
     116    Return a WSGI application that serves a directory (configured 
     117    with document_root) 
     118 
     119    cache_max_age - integer specifies CACHE_CONTROL max_age in seconds 
     120    """ 
     121    if cache_max_age is not None: 
     122        cache_max_age = int(cache_max_age) 
     123    return StaticURLParser( 
     124        document_root, cache_max_age=cache_max_age) 
     125 
     126 
    52127""" 
    53128class Installer(BaseInstaller): 
  • quintagroup.distrpoxy/trunk/setup.py

    r1067 r1082  
    3232          'paste.app_factory': [ 
    3333              'main = quintagroup.distproxy.wsgi:app_factory', 
     34              'static = quintagroup.distproxy.wsgi:make_static', 
    3435              ], 
    3536          }, 
Note: See TracChangeset for help on using the changeset viewer.