update_2.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2013 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. """A class to update a dump created by CMS.
  18. Used by DumpImporter and DumpUpdater.
  19. This converts the dump to the new schema introduced to support task
  20. versioning in commits
  21. bd80d0c930e25972eeda861719f96990de6e7822
  22. 8ee8fa7496d53ff4a8638804eb3aa497586fc6a3
  23. 6ee99114f5aca4ad1a94a76f0b11b5363ba2ffd5
  24. """
  25. def split_dict(src, *keys):
  26. ret = dict()
  27. for k in list(src.keys()):
  28. v = src[k]
  29. if k in keys:
  30. ret[k] = v
  31. del src[k]
  32. return ret
  33. class Updater:
  34. def __init__(self, data):
  35. assert data["_version"] == 1
  36. self.objs = data
  37. self.next_id = len(data)
  38. def get_id(self):
  39. while str(self.next_id) in self.objs:
  40. self.next_id += 1
  41. return str(self.next_id)
  42. def run(self):
  43. for k in list(self.objs.keys()):
  44. if k.startswith("_"):
  45. continue
  46. v = self.objs[k]
  47. if v["_class"] == "Task":
  48. self.split_task(k, v)
  49. return self.objs
  50. def split_task(self, task_id, task_data):
  51. dataset_id = self.get_id()
  52. dataset_data = split_dict(
  53. task_data,
  54. "time_limit", "memory_limit",
  55. "task_type", "task_type_parameters",
  56. "score_type", "score_type_parameters",
  57. "managers", "testcases")
  58. self.objs[dataset_id] = dataset_data
  59. task_data["_class"] = "Task"
  60. dataset_data["_class"] = "Dataset"
  61. task_data["datasets"] = [dataset_id]
  62. task_data["active_dataset"] = dataset_id
  63. dataset_data["task"] = task_id
  64. dataset_data["description"] = "Default"
  65. dataset_data["autojudge"] = False
  66. for id_ in dataset_data["managers"].values():
  67. del self.objs[id_]["task"]
  68. self.objs[id_]["dataset"] = dataset_id
  69. for id_ in dataset_data["testcases"]:
  70. del self.objs[id_]["task"]
  71. self.objs[id_]["dataset"] = dataset_id
  72. for id_ in task_data["submissions"]:
  73. self.split_submission(id_, self.objs[id_], dataset_id)
  74. for id_ in task_data["user_tests"]:
  75. self.split_user_test(id_, self.objs[id_], dataset_id)
  76. def split_submission(self, submission_id, sr_data, dataset_id):
  77. sr_id = self.get_id()
  78. submission_data = split_dict(
  79. sr_data,
  80. "user", "task",
  81. "timestamp", "language",
  82. "files", "token")
  83. self.objs[submission_id] = submission_data
  84. self.objs[sr_id] = sr_data
  85. submission_data["_class"] = "Submission"
  86. sr_data["_class"] = "SubmissionResult"
  87. submission_data["results"] = [sr_id]
  88. sr_data["submission"] = submission_id
  89. sr_data["dataset"] = dataset_id
  90. for id_ in sr_data["executables"].values():
  91. self.objs[id_]["submission"] = submission_id
  92. self.objs[id_]["dataset"] = dataset_id
  93. self.objs[id_]["submission_result"] = sr_id
  94. for id_ in sr_data["evaluations"]:
  95. self.objs[id_]["submission"] = submission_id
  96. self.objs[id_]["dataset"] = dataset_id
  97. self.objs[id_]["submission_result"] = sr_id
  98. def split_user_test(self, user_test_id, ur_data, dataset_id):
  99. ur_id = self.get_id()
  100. user_test_data = split_dict(
  101. ur_data,
  102. "user", "task",
  103. "timestamp", "language", "input",
  104. "files", "managers")
  105. self.objs[user_test_id] = user_test_data
  106. self.objs[ur_id] = ur_data
  107. user_test_data["_class"] = "UserTest"
  108. ur_data["_class"] = "UserTestResult"
  109. user_test_data["results"] = [ur_id]
  110. ur_data["user_test"] = user_test_id
  111. ur_data["dataset"] = dataset_id
  112. for id_ in ur_data["executables"].values():
  113. self.objs[id_]["submission"] = user_test_id
  114. self.objs[id_]["dataset"] = dataset_id
  115. self.objs[id_]["submission_result"] = ur_id