-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathAddStatement.py
executable file
·124 lines (100 loc) · 4.4 KB
/
AddStatement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Contest Management System - http://cms-dev.github.io/
# Copyright © 2016 Myungwoo Chun <[email protected]>
# Copyright © 2016 Stefano Maggiolo <[email protected]>
# Copyright © 2018 William Di Luigi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This script adds a statement to a specific task in the database.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.builtins.disabled import * # noqa
from future.builtins import * # noqa
import argparse
import logging
import sys
import os
from cms import utf8_decoder
from cms.db import SessionGen, Statement
from cms.db.filecacher import FileCacher
from cmscontrib.importing import ImportDataError, task_from_db
logger = logging.getLogger(__name__)
def add_statement(task_name, task_id, language_code, statement_file,
overwrite):
logger.info("Adding the statement(language: %s) of task %s "
"in the database.", language_code, task_name)
if not os.path.exists(statement_file):
raise ImportDataError("Statement file (path: %s) does not exist."
% statement_file)
if not statement_file.endswith(".pdf"):
raise ImportDataError("Statement file should be a pdf file.")
with SessionGen() as session:
task = task_from_db(session, task_name, task_id)
try:
file_cacher = FileCacher()
digest = file_cacher.put_file_from_path(
statement_file,
"Statement for task %s (lang: %s)" %
(task_name, language_code))
except Exception:
logger.error("Task statement storage failed.", exc_info=True)
arr = session.query(Statement)\
.filter(Statement.language == language_code)\
.filter(Statement.task == task)\
.all()
if arr: # Statement already exists
if overwrite:
logger.info("Overwriting already existing statement.")
session.delete(arr[0])
session.commit()
else:
raise ImportDataError("A statement with given language "
"already exists. Not overwriting.")
statement = Statement(language_code, digest, task=task)
session.add(statement)
session.commit()
logger.info("Statement added.")
return True
def main():
"""Parse arguments and launch process.
"""
parser = argparse.ArgumentParser(description="Add a statement to CMS.")
parser.add_argument("task_name", action="store", type=utf8_decoder,
help="short name of the task")
parser.add_argument("language_code", action="store", type=utf8_decoder,
help="language code of statement, e.g. en")
parser.add_argument("statement_file", action="store", type=utf8_decoder,
help="absolute/relative path of statement file")
parser.add_argument("-t", "--task-id", action="store", type=int,
help="optional task ID used for disambiguation")
parser.add_argument("-o", "--overwrite", dest="overwrite",
action="store_true",
help="overwrite existing statement")
parser.set_defaults(overwrite=False)
args = parser.parse_args()
try:
success = add_statement(
args.task_name, args.task_id, args.language_code,
args.statement_file, args.overwrite)
except ImportDataError as e:
logger.error(str(e))
logger.info("Error while importing, no changes were made.")
return 1
return 0 if success is True else 1
if __name__ == "__main__":
sys.exit(main())