AddStatement.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2016 Myungwoo Chun <mc.tamaki@gmail.com>
  4. # Copyright © 2016 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. """This script adds a statement to a specific task in the database.
  19. """
  20. import argparse
  21. import logging
  22. import os
  23. import sys
  24. from cms import utf8_decoder
  25. from cms.db import SessionGen, Statement, Task
  26. from cms.db.filecacher import FileCacher
  27. logger = logging.getLogger(__name__)
  28. def add_statement(task_name, language_code, statement_file, overwrite):
  29. logger.info("Adding the statement(language: %s) of task %s "
  30. "in the database.", language_code, task_name)
  31. if not os.path.exists(statement_file):
  32. logger.error("Statement file (path: %s) does not exist.",
  33. statement_file)
  34. return False
  35. if not statement_file.endswith(".pdf"):
  36. logger.error("Statement file should be a pdf file.")
  37. return False
  38. with SessionGen() as session:
  39. task = session.query(Task)\
  40. .filter(Task.name == task_name).first()
  41. if not task:
  42. logger.error("No task named %s", task_name)
  43. return False
  44. try:
  45. file_cacher = FileCacher()
  46. digest = file_cacher.put_file_from_path(
  47. statement_file,
  48. "Statement for task %s (lang: %s)" %
  49. (task_name, language_code))
  50. except Exception:
  51. logger.error("Task statement storage failed.", exc_info=True)
  52. arr = session.query(Statement)\
  53. .filter(Statement.language == language_code)\
  54. .filter(Statement.task == task)\
  55. .all()
  56. if arr: # Statement already exists
  57. if overwrite:
  58. logger.info("Overwriting already existing statement.")
  59. session.delete(arr[0])
  60. session.commit()
  61. else:
  62. logger.error("A statement with given language already exists. "
  63. "Not overwriting.")
  64. return False
  65. statement = Statement(language_code, digest, task=task)
  66. session.add(statement)
  67. session.commit()
  68. logger.info("Statement added.")
  69. return True
  70. def main():
  71. """Parse arguments and launch process.
  72. """
  73. parser = argparse.ArgumentParser(description="Add a statement to CMS.")
  74. parser.add_argument("task_name", action="store", type=utf8_decoder,
  75. help="short name of the task")
  76. parser.add_argument("language_code", action="store", type=utf8_decoder,
  77. help="language code of statement, e.g. en")
  78. parser.add_argument("statement_file", action="store", type=utf8_decoder,
  79. help="absolute/relative path of statement file")
  80. parser.add_argument("-o", "--overwrite", dest="overwrite",
  81. action="store_true",
  82. help="overwrite existing statement")
  83. parser.set_defaults(overwrite=False)
  84. args = parser.parse_args()
  85. success = add_statement(args.task_name, args.language_code,
  86. args.statement_file, args.overwrite)
  87. return 0 if success is True else 1
  88. if __name__ == "__main__":
  89. sys.exit(main())