From: Mark Hymers Date: Wed, 20 Nov 2013 19:58:46 +0000 (+0000) Subject: Initial pass at dak web server X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=47cd096281f1cc36dfe9818ef1fbae8d02f6ada6;p=dak.git Initial pass at dak web server Signed-off-by: Mark Hymers --- diff --git a/dakweb/__init__.py b/dakweb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/dakweb/dakwebserver.py b/dakweb/dakwebserver.py new file mode 100755 index 00000000..181a2e74 --- /dev/null +++ b/dakweb/dakwebserver.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +# Main script to run the dakweb server and also +# to provide the list_paths and path_help functions + +from sqlalchemy import or_ +import bottle +from daklib.dbconn import DBConn, DBSource, Suite, DSCFile, PoolFile +import json + +from dakweb.webregister import QueryRegister + +@bottle.route('/') +def root_path(): + """Returns a useless welcome message""" + return json.dumps('Use the /list_paths path to list all available paths') +QueryRegister().register_path('/', root_path) + +@bottle.route('/list_paths') +def list_paths(): + """Returns a list of available paths""" + return json.dumps(QueryRegister().get_paths()) +QueryRegister().register_path('/list_paths', list_paths) + +@bottle.route('/path_help/') +def path_help(path=None): + + if path is None: + return bottle.HTTPError(503, 'Path not specified.') + + return json.dumps(QueryRegister().get_path_help(path)) +QueryRegister().register_path('/path_help', list_paths) + +# Import our other methods +from queries.source import * + +print "Connecting" +# Set up our initial database connection +d = DBConn() +#bottle.run(host='localhost', port=8765) +bottle.run() diff --git a/dakweb/queries/__init__.py b/dakweb/queries/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/dakweb/queries/source.py b/dakweb/queries/source.py new file mode 100644 index 00000000..d65283f6 --- /dev/null +++ b/dakweb/queries/source.py @@ -0,0 +1,37 @@ +#!/usr/bin/python + +from sqlalchemy import or_ +import bottle +import json + +from daklib.dbconn import DBConn, DBSource, Suite, DSCFile, PoolFile +from dakweb.webregister import QueryRegister + +@bottle.route('/dsc_in_suite//') +def dsc_in_suite(suite=None, source=None): + """ + Find all dsc files for a given source package name in a given suite. + + suite and source must be supplied + """ + if suite is None: + return bottle.HTTPError(503, 'Suite not specified.') + if source is None: + return bottle.HTTPError(503, 'Source package not specified.') + + s = DBConn().session() + q = s.query(DSCFile).join(PoolFile) + q = q.join(DBSource).join(Suite, DBSource.suites) + q = q.filter(or_(Suite.suite_name == suite, Suite.codename == suite)) + q = q.filter(DBSource.source == source) + q = q.filter(PoolFile.filename.endswith('.dsc')) + ret = [] + for p in q: + ret.append({'version': p.source.version, + 'component': p.poolfile.component.component_name, + 'filename': p.poolfile.filename}) + + return json.dumps(ret) + +QueryRegister().register_path('/dsc_in_suite', dsc_in_suite) + diff --git a/dakweb/webregister.py b/dakweb/webregister.py new file mode 100644 index 00000000..d7e7990d --- /dev/null +++ b/dakweb/webregister.py @@ -0,0 +1,25 @@ +class QueryRegister(object): + __shared_state = {} + + def __init__(self, *args, **kwargs): + self.__dict__ = self.__shared_state + + if not getattr(self, 'initialised', False): + self.initialised = True + + # Dictionary of query paths to help mappings + self.queries = {} + + def register_path(self, path, func): + self.queries[path] = func.__doc__ + + def get_paths(self): + return sorted(self.queries.keys()) + + def get_path_help(self, path): + # We always register with the leading / + if not path.startswith('/'): + path = '/' + path + return self.queries.get(path, 'Unknown path') + +__all__ = ['QueryRegister']