binary.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 daklib.dbconn import DBConn, DBBinary, DBSource, SourceMetadata, MetadataKey
  9. from dakweb.webregister import QueryRegister
  10. @bottle.route('/binary/metadata_keys/')
  11. def binary_metadata_keys():
  12. """
  13. List all possible metadata keys
  14. @rtype: dictionary
  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=None):
  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. @type key: string
  34. @param key: Metadata key of the source package to search for.
  35. @rtype: dictionary
  36. @return: A list of dictionaries of
  37. - binary
  38. - source
  39. - metadata value
  40. """
  41. if not key:
  42. return bottle.HTTPError(503, 'Metadata key not specified.')
  43. s = DBConn().session()
  44. q = s.query(DBBinary.package, DBSource.source, SourceMetadata.value)
  45. q = q.join(DBSource).join(SourceMetadata).join(MetadataKey)
  46. q = q.filter(MetadataKey.key == key)
  47. q = q.group_by(DBBinary.package, DBSource.source, SourceMetadata.value)
  48. ret = []
  49. for p in q:
  50. ret.append({'binary': p.package,
  51. 'source': p.source,
  52. 'metadata_value': p.value})
  53. s.close()
  54. bottle.response.content_type = 'application/json; charset=UTF-8'
  55. return json.dumps(ret)
  56. QueryRegister().register_path('/binary/by_metadata', binary_by_metadata)