view dogdish/dispatcher.py @ 13:71f9f68986b5

directory scanning + caching
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 17 Oct 2012 10:56:38 -0700
parents 234c2427e52b
children cffb6f681b59
line wrap: on
line source

#!/usr/bin/env python

"""
dogdish
https://bugzilla.mozilla.org/show_bug.cgi?id=800118
"""

import fnmatch
import os
import sys
from urlparse import urlparse
from webob import Request
from webob import Response, exc
from ConfigParser import RawConfigParser as ConfigParser

here = os.path.dirname(os.path.abspath(__file__))


class Application(object):
    """class for storing application.ini data"""

    def __init__(self, filename):
        """
        - filename : path to an application.ini file
        """

### request handlers

class Handler(object):
    """abstract handler object for a request"""

    def __init__(self, request):
        self.request = request
        self.application_path = urlparse(request.application_url)[2]

    def link(self, path=(), permanant=False):
        if isinstance(path, basestring):
            path = [ path ]
        path = [ i.strip('/') for i in path ]
        if permanant:
            application_url = [ self.request.application_url ]
        else:
            application_url = [ self.application_path ]
        path = application_url + path
        return '/'.join(path)

    def redirect(self, location):
        raise exc.HTTPSeeOther(location=location)

class Get(Handler):
    """handle GET requests"""

    # template for response body
    body = """<?xml version="1.0"?>
<updates>
  <update type="minor" appVersion="19.0a1" version="19.0a1" extensionVersion="19.0a1" buildID="20121010114416" licenseURL="http://www.mozilla.com/test/sample-eula.html" detailsURL="http://www.mozilla.com/test/sample-details.html">
    <patch type="complete" URL="http://update.boot2gecko.org/nightly/b2g_update_2012-10-10_114416.mar%(query)s" hashFunction="SHA512" hashValue="84edb1f53891cf983bc0f6066d31492f43e2d063aaceb05e1c51876f4fde81635afeb7ce3203dee6f65dd59be0cae5c73c49093b625c99acd4118000ad72dda8" size="42924805"/>
  </update>
</updates>"""

    ### methods for request handler

    @classmethod
    def match(cls, request):
        return request.method == 'GET'

    def __call__(self):
        body = self.body
        query = {}
        dogfood_id = self.request.GET.get('dogfood_id')
        if dogfood_id:
            query['dogfooding_prerelease_id'] = dogfood_id

        # build query string
        if query:
            query = '?' + '&'.join(['%s=%s' % (key, value) for key, value in query.items()])
        else:
            query = ''

        # template variables
        variables = dict(query=query)

        return Response(content_type='text/xml',
                        body=body % variables)

class Dispatcher(object):
    """web application"""


    ### class level variables
    defaults = {'directory': here}

    def __init__(self, **kw):

        # set defaults
        for key in self.defaults:
            setattr(self, key, kw.get(key, self.defaults[key]))
        self.handlers = [ Get ]
        self.updates = {}
        self.current_update = None
        self.current_stamp = '0000-00-00_000000'

        # scan directory
        self.scan()

    def __call__(self, environ, start_response):
        """WSGI application"""

        request = Request(environ)
        for h in self.handlers:
            if h.match(request):
                handler = h(request)
                break
        else:
            handler = exc.HTTPNotFound
        res = handler()
        return res(environ, start_response)

    def scan(self):
        """scan the directory for updates"""
        prefix = 'b2g_update_'
        suffix = '.mar'
        contents = [i for i in os.listdir(self.directory)
                    if i.startswith(prefix) and i.endswith(suffix)]
        contents = set(contents)
        new = contents.difference(self.updates.keys())
        if not new:
            # directory contents unchanged from cached values
            return

        for update in new:
            stamp = update[len(prefix):-len(suffix)]
            application_ini = 'application_%s.ini' % stamp
            application_ini = os.path.join(self.directory, application_ini)
            assert os.path.exists(application_ini)
            self.updates[update] = Application(application_ini)
            if stamp > self.current_stamp:
                self.current_update = update
                self.current_stamp = stamp

def main(args=sys.argv[1:]):
    """CLI entry point"""

    # imports for CLI
    import optparse
    from wsgiref import simple_server

    # parse CLI options
    parser = optparse.OptionParser()
    parser.add_option('-p', '--port', dest='port',
                      default=8080, type='int',
                      help="port to serve on")
    parser.add_option('-d', '--directory', dest='directory',
                      default=os.getcwd(),
                      help="directory of update files")
    options, args = parser.parse_args()

    # create the app
    app = Dispatcher(directory=options.directory)

    # serve the app
    print "http://localhost:%s/" % options.port
    server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
    server.serve_forever()

if __name__ == '__main__':
    main()