binary.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. E.g., to find out the Go import paths of all Debian Go packages, query
  31. /binary/by_metadata/Go-Import-Path.
  32. @type key: string
  33. @param key: Metadata key to search for.
  34. @rtype: dictionary
  35. @return: A list of dictionaries of
  36. - binary
  37. - source
  38. - metadata value
  39. """
  40. if not key:
  41. return bottle.HTTPError(503, 'Metadata key not specified.')
  42. s = DBConn().session()
  43. q = s.query(DBBinary.package, DBSource.source, SourceMetadata.value)
  44. q = q.join(DBSource).join(SourceMetadata).join(MetadataKey)
  45. q = q.filter(MetadataKey.key == key)
  46. q = q.group_by(DBBinary.package, DBSource.source, SourceMetadata.value)
  47. ret = []
  48. for p in q:
  49. ret.append({'binary': p.package,
  50. 'source': p.source,
  51. 'metadata_value': p.value})
  52. s.close()
  53. bottle.response.content_type = 'application/json; charset=UTF-8'
  54. return json.dumps(ret)
  55. QueryRegister().register_path('/binary/by_metadata', binary_by_metadata)