suite.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """ Suite related queries
  2. @contact: Debian FTPMaster <ftpmaster@debian.org>
  3. @copyright: 2014 Mark Hymers <mhy@debian.org>
  4. @license: GNU General Public License version 2 or later
  5. @newfield maps: Mapping, Mappings
  6. """
  7. import bottle
  8. import json
  9. from typing import Optional
  10. from daklib.dbconn import DBConn, Suite
  11. from dakweb.webregister import QueryRegister
  12. @bottle.route('/suites')
  13. def suites() -> str:
  14. """
  15. Give information about all known suites.
  16. @maps: name maps to Suite: in the release file
  17. @maps: codename maps to Codename: in the release file.
  18. @maps: dakname is an internal name and should not be relied upon.
  19. :return: List of dictionaries made out of
  20. - name
  21. - codename
  22. - dakname
  23. - archive
  24. - architectures
  25. - components
  26. """
  27. s = DBConn().session()
  28. q = s.query(Suite)
  29. q = q.order_by(Suite.suite_name)
  30. ret = []
  31. for p in q:
  32. ret.append({'name': p.release_suite_output,
  33. 'codename': p.codename,
  34. 'dakname': p.suite_name,
  35. 'archive': p.archive.archive_name,
  36. 'architectures': [x.arch_string for x in p.architectures],
  37. 'components': [x.component_name for x in p.components]})
  38. s.close()
  39. bottle.response.content_type = 'application/json; charset=UTF-8'
  40. return json.dumps(ret)
  41. QueryRegister().register_path('/suites', suites)
  42. @bottle.route('/suite/<suite>')
  43. def suite(suite: Optional[str] = None) -> str:
  44. """
  45. Gives information about a single suite. Note that this routine will look
  46. up a suite first by the main suite_name, but then also by codename if no
  47. suite is initially found. It can therefore be used to canonicalise suite
  48. names.
  49. :param suite: Name or codename of the suite.
  50. :return: A dictionary of
  51. - name: maps to `Suite:` in the Release file
  52. - codename: maps to `Codename:` in the Release file
  53. - dakname: internal name that should not be relied upload
  54. - archive
  55. - architectures
  56. - components
  57. .. seealso:: :func:`~dakweb.queries.suite.suites` on how to receive a list of valid suites.
  58. """
  59. if suite is None:
  60. return bottle.HTTPError(503, 'Suite not specified.')
  61. # TODO: We should probably stick this logic into daklib/dbconn.py
  62. so = None
  63. s = DBConn().session()
  64. q = s.query(Suite)
  65. q = q.filter(Suite.suite_name == suite)
  66. if q.count() > 1:
  67. # This would mean dak is misconfigured
  68. s.close()
  69. return bottle.HTTPError(503, 'Multiple suites found: configuration error')
  70. elif q.count() == 1:
  71. so = q[0]
  72. else:
  73. # Look it up by suite_name
  74. q = s.query(Suite).filter(Suite.codename == suite)
  75. if q.count() > 1:
  76. # This would mean dak is misconfigured
  77. s.close()
  78. return bottle.HTTPError(503, 'Multiple suites found: configuration error')
  79. elif q.count() == 1:
  80. so = q[0]
  81. if so is not None:
  82. so = {'name': so.release_suite_output,
  83. 'codename': so.codename,
  84. 'dakname': so.suite_name,
  85. 'archive': so.archive.archive_name,
  86. 'architectures': [x.arch_string for x in so.architectures],
  87. 'components': [x.component_name for x in so.components]}
  88. s.close()
  89. bottle.response.content_type = 'application/json; charset=UTF-8'
  90. return json.dumps(so)
  91. QueryRegister().register_path('/suite', suite)