binary.py 2.1 KB

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