update_29.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2018 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  4. # Copyright © 2019 Stefano Maggiolo <s.maggiolo@gmail.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """A class to update a dump created by CMS.
  19. Used by DumpImporter and DumpUpdater.
  20. This updater changes the in-database column type for some columns.
  21. """
  22. import json
  23. import logging
  24. logger = logging.getLogger(__name__)
  25. def fix_text(t):
  26. if t is None:
  27. return []
  28. try:
  29. t = json.loads(t)
  30. except ValueError:
  31. t = [t]
  32. t[0] = t[0].replace("%d", "%s")
  33. # Some items were stored as numbers instead of strings.
  34. return [str(x) for x in t]
  35. class Updater:
  36. def __init__(self, data):
  37. assert data["_version"] == 28
  38. self.objs = data
  39. def run(self):
  40. for k, v in self.objs.items():
  41. if k.startswith("_"):
  42. continue
  43. if v["_class"] == "Dataset":
  44. v["task_type_parameters"] = \
  45. json.loads(v["task_type_parameters"])
  46. v["score_type_parameters"] = \
  47. json.loads(v["score_type_parameters"])
  48. if v["_class"] == "SubmissionResult":
  49. if v["score_details"] is not None:
  50. v["score_details"] = json.loads(v["score_details"])
  51. if v["public_score_details"] is not None:
  52. v["public_score_details"] = \
  53. json.loads(v["public_score_details"])
  54. if v["ranking_score_details"] is not None:
  55. v["ranking_score_details"] = \
  56. json.loads(v["ranking_score_details"])
  57. v["compilation_text"] = fix_text(v["compilation_text"])
  58. if v["_class"] == "Evaluation":
  59. v["text"] = fix_text(v["text"])
  60. if v["_class"] == "UserTestResult":
  61. v["compilation_text"] = fix_text(v["compilation_text"])
  62. v["evaluation_text"] = fix_text(v["evaluation_text"])
  63. if v["_class"] == "User":
  64. v["preferred_languages"] = \
  65. json.loads(v["preferred_languages"]) \
  66. if "preferred_languages" in v else []
  67. if v["_class"] == "Task":
  68. v["primary_statements"] = json.loads(v["primary_statements"])
  69. return self.objs