]> git.donarmstrong.com Git - dak.git/blob - dakweb/queries/suite.py
Close our SQLAlchemy sessions
[dak.git] / dakweb / queries / suite.py
1 #!/usr/bin/python
2
3 import bottle
4 import json
5
6 from daklib.dbconn import DBConn, Suite
7 from dakweb.webregister import QueryRegister
8
9 @bottle.route('/suites')
10 def suites():
11     """
12     suites()
13
14     returns: list of dictionaries
15
16     Give information about all known suites.
17
18     name maps to Suite: in the release file
19     codename maps to Codename: in the release file.
20     dakname is an internal name and should not be relied upon.
21     """
22
23     s = DBConn().session()
24     q = s.query(Suite)
25     q = q.order_by(Suite.suite_name)
26     ret = []
27     for p in q:
28         ret.append({'name':       p.release_suite_output,
29                     'codename':   p.codename,
30                     'dakname':    p.suite_name,
31                     'archive':    p.archive.archive_name,
32                     'architectures': [x.arch_string for x in p.architectures],
33                     'components': [x.component_name for x in p.components]})
34
35     s.close()
36
37     return json.dumps(ret)
38
39 QueryRegister().register_path('/suites', suites)
40
41 @bottle.route('/suite/<suite>')
42 def suite(suite=None):
43     """
44     suite(suite)
45
46     returns: dictionary
47
48     Gives information about a single suite.  Note that this routine will look
49     up a suite first by the main suite_name, but then also by codename if no
50     suite is initially found.  It can therefore be used to canonicalise suite
51     names
52     """
53
54     if suite is None:
55         return bottle.HTTPError(503, 'Suite not specified.')
56
57     # TODO: We should probably stick this logic into daklib/dbconn.py
58     so = None
59
60     s = DBConn().session()
61     q = s.query(Suite)
62     q = q.filter(Suite.suite_name == suite)
63
64     if q.count() > 1:
65         # This would mean dak is misconfigured
66         s.close()
67         return bottle.HTTPError(503, 'Multiple suites found: configuration error')
68     elif q.count() == 1:
69         so = q[0]
70     else:
71         # Look it up by suite_name
72         q = s.query(Suite).filter(Suite.codename == suite)
73         if q.count() > 1:
74             # This would mean dak is misconfigured
75             s.close()
76             return bottle.HTTPError(503, 'Multiple suites found: configuration error')
77         elif q.count() == 1:
78             so = q[0]
79
80     if so is not None:
81         so = {'name':       so.release_suite_output,
82               'codename':   so.codename,
83               'dakname':    so.suite_name,
84               'archive':    so.archive.archive_name,
85               'architectures': [x.arch_string for x in so.architectures],
86               'components': [x.component_name for x in so.components]}
87
88     s.close()
89
90     return json.dumps(so)
91
92 QueryRegister().register_path('/suite', suite)
93