AWSRequests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2012 Bernard Blackham <bernard@largestprime.net>
  4. # Copyright © 2010-2012 Giovanni Mascellani <mascellani@poisson.phc.unipi.it>
  5. # Copyright © 2010-2017 Stefano Maggiolo <s.maggiolo@gmail.com>
  6. # Copyright © 2010-2012 Matteo Boscariol <boscarim@hotmail.com>
  7. # Copyright © 2016 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  8. # Copyright © 2017 Luca Chiodini <luca@chiodini.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as
  12. # published by the Free Software Foundation, either version 3 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. import re
  23. from bs4 import BeautifulSoup
  24. from cmstestsuite.web import GenericRequest, LoginRequest
  25. class AWSLoginRequest(LoginRequest):
  26. def test_success(self):
  27. if not LoginRequest.test_success(self):
  28. return False
  29. fail_re = re.compile('Failed to log in.')
  30. if fail_re.search(self.res_data) is not None:
  31. return False
  32. username_re = re.compile(self.username)
  33. if username_re.search(self.res_data) is None:
  34. return False
  35. return True
  36. class AWSSubmissionViewRequest(GenericRequest):
  37. """Load the view of a submission in AWS.
  38. """
  39. def __init__(self, browser, submission_id, base_url=None):
  40. GenericRequest.__init__(self, browser, base_url)
  41. self.submission_id = submission_id
  42. self.url = "%s/submission/%s" % (self.base_url, submission_id)
  43. def describe(self):
  44. return "check submission %s" % self.submission_id
  45. def test_success(self):
  46. if not GenericRequest.test_success(self):
  47. return False
  48. try:
  49. self.get_submission_info()
  50. return True
  51. except:
  52. return False
  53. def get_submission_info(self):
  54. # Only valid after self.execute()
  55. # Parse submission information out of response.
  56. # "html.parser" is Python's built-in parser. Alternatives that require
  57. # external dependencies are "lxml" and "html5lib".
  58. soup = BeautifulSoup(self.res_data, "html.parser")
  59. info = {}
  60. # Get submission status.
  61. tag = soup.find_all(id="submission_status")[0]
  62. info['status'] = tag.text.strip()
  63. # Get compilation text.
  64. tags = soup.find_all(id="compilation")
  65. if tags:
  66. content = tags[0]
  67. info['compile_output'] = "\n".join(
  68. [pre.text.strip() for pre in content.findAll("pre")])
  69. else:
  70. info['compile_output'] = None
  71. # Get evaluation results.
  72. evaluations = []
  73. tags = soup.find_all(id=re.compile(r"^eval_outcome_"))
  74. text_tags = soup.find_all(id=re.compile(r"^eval_text_"))
  75. for outcome_tag, text_tag in zip(tags, text_tags):
  76. # Get evaluation text also.
  77. evaluations.append({
  78. 'outcome': outcome_tag.text.strip(),
  79. 'text': text_tag.text.strip(),
  80. })
  81. info['evaluations'] = evaluations
  82. return info
  83. class AWSUserTestViewRequest(GenericRequest):
  84. """Load the view of a user test in AWS."""
  85. def __init__(self, browser, user_test_id, base_url=None):
  86. GenericRequest.__init__(self, browser, base_url)
  87. self.user_test_id = user_test_id
  88. self.url = "%s/user_test/%s" % (self.base_url, user_test_id)
  89. def describe(self):
  90. return "check user_test %s" % self.user_test_id
  91. def test_success(self):
  92. if not GenericRequest.test_success(self):
  93. return False
  94. try:
  95. self.get_user_test_info()
  96. return True
  97. except:
  98. return False
  99. def get_user_test_info(self):
  100. # Only valid after self.execute()
  101. # Parse user test information out of response.
  102. # "html.parser" is Python's built-in parser. Alternatives that require
  103. # external dependencies are "lxml" and "html5lib".
  104. soup = BeautifulSoup(self.res_data, "html.parser")
  105. info = {}
  106. # Get user test status.
  107. tag = soup.findAll(id="user_test_status")[0]
  108. info['status'] = tag.text.strip()
  109. # Get compilation text.
  110. tags = soup.findAll(id="compilation")
  111. if tags:
  112. content = tags[0]
  113. info['compile_output'] = content.pre.text.strip()
  114. else:
  115. info['compile_output'] = None
  116. return info