reject_files.py
2.04 KB
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
#!/usr/bin/env python
import sys
import os
import re
import shutil
from collections import defaultdict
from optparse import OptionParser
# Solution with no hard coded path would be welcome
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), ".."))
from dfs.database import Database
from dfs.repo import Repo
from dfs.config import Config
def get_reason(db, file_id):
reason = ""
file_elem = db.file_index[file_id]
for ann in file_elem.findall("ann"):
if db.returned(ann):
reason = db.get_reason(ann)
break
for ann in file_elem.findall("s_ann"):
if db.returned(ann):
reason = db.get_reason(ann)
break
return reason
def reject_file(db, file_id, reason):
try:
if reason == "":
reason = get_reason(db, file_id)
print "Rejecting", file_id, "because", reason
db.reject(file_id, reason)
except Exception as ex:
print file_id, "-", str(ex)
return False
return True
if __name__ == "__main__":
optparser = OptionParser(usage="""usage: %prog -r REASON CONFIG FILES""")
optparser.add_option("-r", dest="reason", default="", help="Reason to reject files")
(options, args) = optparser.parse_args()
if len(args) < 2:
optparser.print_help()
sys.exit(0)
conf_path = args[0]
cfg = Config(conf_path)
ids = args[1:]
reason = options.reason
anno_per_file = int(cfg["anno_per_file"])
wc = Repo(cfg["svn.repository"], cfg["svn.login"], cfg["svn.passwd"])
db = Database(wc.db_path(), anno_per_file)
success = []
fail = []
for file_id in ids:
if reject_file(db, file_id, reason):
success.append(file_id)
else:
fail.append(file_id)
db.save()
wc.commit("Rejected files: "+str(ids))
print ""
if len(success) > 0:
print "Rejected files: "+str(success)
if len(fail) > 0:
print "Failed to reject files: "+str(fail)