1"""Debian binary package related queries.
3@copyright: 2017 Michael Stapelberg <stapelberg@debian.org>
4@copyright: 2017 Joerg Jaspert <joerg@debian.org>
5@license: GNU General Public License version 2 or later
6"""
8import bottle
9import json
10from typing import Optional
12from daklib.dbconn import DBConn, DBBinary, DBSource, SourceMetadata, MetadataKey
13from dakweb.webregister import QueryRegister
16@bottle.route('/binary/metadata_keys/')
17def binary_metadata_keys() -> str:
18 """
19 List all possible metadata keys
21 :return: A list of metadata keys
22 """
23 s = DBConn().session()
24 q = s.query(MetadataKey)
25 ret = []
26 for p in q:
27 ret.append(p.key)
29 s.close()
31 bottle.response.content_type = 'application/json; charset=UTF-8'
32 return json.dumps(ret)
35QueryRegister().register_path('/metadata_keys', binary_metadata_keys)
38@bottle.route('/binary/by_metadata/<key>')
39def binary_by_metadata(key: Optional[str] = None) -> str:
40 """
42 Finds all Debian binary packages which have the specified metadata set
43 in their correspondig source package.
45 E.g., to find out the Go import paths of all Debian Go packages, query
46 /binary/by_metadata/Go-Import-Path.
48 :param key: Metadata key of the source package to search for.
49 :return: A list of dictionaries of
50 - binary
51 - source
52 - metadata value
53 """
55 if not key:
56 return bottle.HTTPError(503, 'Metadata key not specified.')
58 s = DBConn().session()
59 q = s.query(DBBinary.package, DBSource.source, SourceMetadata.value)
60 q = q.join(DBSource, DBBinary.source_id == DBSource.source_id).join(SourceMetadata).join(MetadataKey)
61 q = q.filter(MetadataKey.key == key)
62 q = q.group_by(DBBinary.package, DBSource.source, SourceMetadata.value)
63 ret = []
64 for p in q:
65 ret.append({'binary': p.package,
66 'source': p.source,
67 'metadata_value': p.value})
68 s.close()
70 bottle.response.content_type = 'application/json; charset=UTF-8'
71 return json.dumps(ret)
74QueryRegister().register_path('/binary/by_metadata', binary_by_metadata)