-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheotasks.py
198 lines (162 loc) · 7.44 KB
/
eotasks.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This file is part of misc-tools.
# Copyright (C) 2014-2020 Sequent Tech Inc <[email protected]>
# misc-tools 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.
# misc-tools 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 misc-tools. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import argparse
from sqlalchemy.sql import text
from frestq.fscheduler import FScheduler
from frestq.utils import (list_messages, list_tasks, task_tree, show_task,
show_message, show_external_task, finish_task, get_tasks, get_external_task)
from prettytable import PrettyTable
import json
def get_election_path(path_type, election_id, *args):
'''
return an election public path. path_type is either 'private' or 'public'.
'''
data_path = app.config.get(path_type.upper() + '_DATA_PATH', '')
election_path = os.path.join(data_path, str(election_id))
return os.path.join(election_path, *args)
def allow_disjoint_multiple_tallies(election_id):
'''
Marks an election as to allow multiple tallies, although with a disjoint
set of votes (no vote can be repeated).
'''
election_privpath = get_election_path('private', election_id)
allow_disjoint_multiple_tallies_path = get_election_path('private',
election_id, 'allow_disjoint_multiple_tallies')
if not os.path.exists(election_privpath):
print("election id='%s' not found in path '%s'" % (str(election_id), election_privpath))
elif not os.path.exists(allow_disjoint_multiple_tallies_path):
open(allow_disjoint_multiple_tallies_path, 'w').close()
else:
print("election id='%s' already has the allow_disjoint_multiple_tallies activated" % str(election_id))
def disallow_disjoint_multiple_tallies(election_id):
'''
Marks an election as to NOT allow multiple tallies.
'''
election_privpath = get_election_path('private', election_id)
allow_disjoint_multiple_tallies_path = get_election_path('private',
election_id, 'allow_disjoint_multiple_tallies')
if not os.path.exists(election_privpath):
print("election id='%s' not found in path '%s'" % (str(election_id), election_privpath))
elif not os.path.exists(allow_disjoint_multiple_tallies_path):
print("election id='%s' already has the allow_disjoint_multiple_tallies NOT activated" % str(election_id))
else:
os.unlink(allow_disjoint_multiple_tallies_path)
def reset_tally(election_id):
'''
Remove traces of previous tallies of the specified election so that a new
fresh tally can be executed.
'''
election_privpath = get_election_path('private', election_id)
delete_paths = [
get_election_path('private', election_id, 'allow_disjoint_multiple_tallies'),
get_election_path('public', election_id, 'tally.tar.gz'),
get_election_path('public', election_id, 'tally.tar.gz.sha256')
]
# check that the election exists
if not os.path.exists(election_privpath):
print("election id='%s' not found in path '%s'" % (str(election_id), election_privpath))
# remove files if they exist
for dpath in delete_paths:
if os.path.exists(dpath):
os.unlink(dpath)
# reset ballot list for this election, so that it's allowed to tally the
# already tallied votes
election = db.session.connection().execute(
text("DELETE FROM ballot WHERE session_id IN (SELECT id FROM session WHERE election_id = :election_id)"),
election_id=str(election_id))
db.session.commit()
def print_task_list(tasks):
table = PrettyTable(['small id', 'label', 'election', 'sender_url', 'created_date'])
for task in tasks:
election = task.input_data['Title']
question = ''
table.add_row([str(task.id)[:8], task.label, election, task.sender_url, task.created_date])
print(table)
def print_task(task):
table = PrettyTable(header=False)
election = task.input_data['Title']
if 'Question data' in task.input_data:
question_data = task.input_data['Question data']
questions = ''
for q in question_data:
questions = questions + q['question'] + " "
table.add_row(["small id", str(task.id)[:8]])
table.add_row(["election", election])
table.add_row(["label", task.label])
if len(questions) > 0:
table.add_row(["questions", questions.strip()])
table.add_row(["sender_url", task.sender_url])
table.add_row(["created_date", task.created_date])
print(table)
# local parser
def process_parser(self, parser):
self.parser = parser
parser.add_argument("--list", help="list last tasks",
action="store_true")
parser.add_argument("--show", help="show task")
parser.add_argument("--show-full", help="show task detail")
parser.add_argument("--accept", help="accept task")
parser.add_argument("--reject", help="reject task")
parser.add_argument("--allow-disjoint-multiple-tallies",
action='store_true',
help="Allow tallies of different subsets of votes. Specify election id with --election-id.")
parser.add_argument("--disallow-disjoint-multiple-tallies",
action='store_true',
help="Allow tallies of different subsets of votes. Specify election id with --election-id.")
parser.add_argument("--reset-tally",
action='store_true', help="Reset tally, allowing a completely fresh tally to be done, even repeating ballots.")
parser.add_argument("--election-id", help="Election id", type=str)
from frestq.frestq_app import FrestqApp
FrestqApp.process_parser = process_parser
from frestq.app import *
parser = app.parser
pargs = app.pargs
app.configure_app(config_object=__name__)
if pargs.list:
target_pargs = parser.parse_args(["--tasks", "--filters", "task_type=external", "status=executing"])
tasks = get_tasks(target_pargs)
print_task_list(tasks)
elif pargs.allow_disjoint_multiple_tallies and pargs.election_id:
allow_disjoint_multiple_tallies(pargs.election_id)
elif pargs.disallow_disjoint_multiple_tallies and pargs.election_id:
disallow_disjoint_multiple_tallies(pargs.election_id)
elif pargs.reset_tally and pargs.election_id:
print("reset_tally")
reset_tally(pargs.election_id)
elif pargs.show:
target_pargs = parser.parse_args(["--show-external", pargs.show])
tasks = get_external_task(target_pargs)
if not tasks:
print("task %s not found" % pargs.show)
else:
task = tasks[0]
print_task(task)
elif pargs.show_full:
target_pargs = parser.parse_args(["--show-external", pargs.show_full])
tasks = get_external_task(target_pargs)
if not tasks:
print("task %s not found" % pargs.show)
else:
task = tasks[0]
print(json.dumps(tasks[0].input_data, indent=4))
elif pargs.accept:
target_pargs = parser.parse_args(["--finish", pargs.accept, '{"status": "accepted"}'])
finish_task(target_pargs)
elif pargs.reject:
target_pargs = parser.parse_args(["--finish", pargs.reject, '{"status": "denied"}'])
finish_task(target_pargs)
else:
app.run_args()