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 bottle 

9import json 

10from typing import Optional 

11 

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

13from dakweb.webregister import QueryRegister 

14 

15 

16@bottle.route('/binary/metadata_keys/') 

17def binary_metadata_keys() -> str: 

18 """ 

19 List all possible metadata keys 

20 

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) 

28 

29 s.close() 

30 

31 bottle.response.content_type = 'application/json; charset=UTF-8' 

32 return json.dumps(ret) 

33 

34 

35QueryRegister().register_path('/metadata_keys', binary_metadata_keys) 

36 

37 

38@bottle.route('/binary/by_metadata/<key>') 

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

40 """ 

41 

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

43 in their correspondig source package. 

44 

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

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

47 

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

54 

55 if not key: 

56 return bottle.HTTPError(503, 'Metadata key not specified.') 

57 

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

69 

70 bottle.response.content_type = 'application/json; charset=UTF-8' 

71 return json.dumps(ret) 

72 

73 

74QueryRegister().register_path('/binary/by_metadata', binary_by_metadata)