1"""Debian binary package related queries. 

2 

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""" 

7 

8import json 

9from typing import Optional 

10 

11import bottle 

12 

13from daklib.dbconn import DBBinary, DBConn, DBSource, MetadataKey, SourceMetadata 

14from dakweb.webregister import QueryRegister 

15 

16 

17@bottle.route("/binary/metadata_keys/") 

18def binary_metadata_keys() -> str: 

19 """ 

20 List all possible metadata keys 

21 

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) 

29 

30 s.close() 

31 

32 bottle.response.content_type = "application/json; charset=UTF-8" 

33 return json.dumps(ret) 

34 

35 

36QueryRegister().register_path("/metadata_keys", binary_metadata_keys) 

37 

38 

39@bottle.route("/binary/by_metadata/<key>") 

40def binary_by_metadata(key: Optional[str] = None) -> str: 

41 """ 

42 

43 Finds all Debian binary packages which have the specified metadata set 

44 in their correspondig source package. 

45 

46 E.g., to find out the Go import paths of all Debian Go packages, query 

47 /binary/by_metadata/Go-Import-Path. 

48 

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 """ 

55 

56 if not key: 

57 return bottle.HTTPError(503, "Metadata key not specified.") 

58 

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() 

72 

73 bottle.response.content_type = "application/json; charset=UTF-8" 

74 return json.dumps(ret) 

75 

76 

77QueryRegister().register_path("/binary/by_metadata", binary_by_metadata)