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 json
9from typing import Optional
11import bottle
13from daklib.dbconn import DBBinary, DBConn, DBSource, MetadataKey, SourceMetadata
14from dakweb.webregister import QueryRegister
17@bottle.route("/binary/metadata_keys/")
18def binary_metadata_keys() -> str:
19 """
20 List all possible metadata keys
22 :return: A list of metadata keys
23 """
24 s = DBConn().session()
25 q = s.query(MetadataKey)
26 ret = []
27 for p in q:
28 ret.append(p.key)
30 s.close()
32 bottle.response.content_type = "application/json; charset=UTF-8"
33 return json.dumps(ret)
36QueryRegister().register_path("/metadata_keys", binary_metadata_keys)
39@bottle.route("/binary/by_metadata/<key>")
40def binary_by_metadata(key: Optional[str] = None) -> str:
41 """
43 Finds all Debian binary packages which have the specified metadata set
44 in their correspondig source package.
46 E.g., to find out the Go import paths of all Debian Go packages, query
47 /binary/by_metadata/Go-Import-Path.
49 :param key: Metadata key of the source package to search for.
50 :return: A list of dictionaries of
51 - binary
52 - source
53 - metadata value
54 """
56 if not key:
57 return bottle.HTTPError(503, "Metadata key not specified.")
59 s = DBConn().session()
60 q = s.query(DBBinary.package, DBSource.source, SourceMetadata.value)
61 q = (
62 q.join(DBSource, DBBinary.source_id == DBSource.source_id)
63 .join(SourceMetadata)
64 .join(MetadataKey)
65 )
66 q = q.filter(MetadataKey.key == key)
67 q = q.group_by(DBBinary.package, DBSource.source, SourceMetadata.value)
68 ret = []
69 for p in q:
70 ret.append({"binary": p.package, "source": p.source, "metadata_value": p.value})
71 s.close()
73 bottle.response.content_type = "application/json; charset=UTF-8"
74 return json.dumps(ret)
77QueryRegister().register_path("/binary/by_metadata", binary_by_metadata)