-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_webapp_db.py
190 lines (150 loc) · 5.51 KB
/
update_webapp_db.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
#!/usr/bin/env python3
"""
This script updates the database of the web server using
db/abbrev0.bib
db/abbrev1.bib
db/abbrev2.bib
db/abbrev3.bib
db/crypto_db.bib
db/crypto_conf_list.bib
db/crypto_db_misc.bib
db/changes.txt
WARNING: THIS SCRIPT NEEDS TO BE EXECUTED in the ROOT folder of the cryptobib project
AND web2py needs to installed as explained in webapp/README.md
In addition, it may be necessary to clear the cache of the web server or to restart it, after updating storage.sqlite.
Please read README.md for details.
"""
import sys
import os
scriptdir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(".")
sys.path.append(os.path.join(scriptdir, "..", "lib"))
sys.path.append(os.path.join(scriptdir, "..", "db"))
sys.path.append(os.path.join(scriptdir, "..", "web2py"))
from mybibtex import tools
import mybibtex.parser
import mybibtex.generator
from confs_years import *
import config
from config import *
mybibtex.generator.config = config
import logging
import re
import datetime
logging.basicConfig(level=logging.DEBUG)
os.chdir("web2py")
import gluon.shell
from gluon.storage import Storage
_re_date = re.compile(r"^\s*(\d\d\d\d)-(\d\d)-(\d\d)\s*$")
def update_changes(db):
"""
Store changes in the table "changes" of storage.sql
changes.txt has to be of the form:
* yyyy-mm-dd
description of change at ...
* yyyy-mm-dd
...
"""
changes = []
fin = open("../db/changes.txt")
lineno = 1
for line in fin:
if lineno == 1 and line[0] != "*":
logging.error("Error: the first line has to start by '*'")
sys.exit(1)
if line[0] == "*":
r = _re_date.match(line[1:])
if r == None:
logging.error("Error: invalid date on line {}. Date format is yyyy-mm-dd".format(lineno))
sys.exit(1)
(yy,mm,dd) = r.groups()
changes.append((datetime.date(int(yy),int(mm),int(dd)),""))
else:
(date_c, desc_c) = changes[-1]
desc_c = desc_c + line
changes[-1] = (date_c, desc_c)
lineno += 1
fin.close()
changes_bulk = [
{"date": date_c.strftime("%Y-%m-%d"), "desc": desc_c}
for (date_c, desc_c) in changes
]
db.change.truncate()
db.change.bulk_insert(changes_bulk)
db.commit()
def update_confs(db, confs_years):
confs = [
{
"type": conf["type"],
"key": confkey,
"name": conf["name"],
"full_name": conf["full_name"],
"start_year": confs_years[confkey][0],
"end_year": confs_years[confkey][1]
}
for (confkey, conf) in sorted(
iter(config.confs.items()),
key = lambda k_x: ("a-" if k_x[1]["type"] == "conf" else "b-") + k_x[1]["name"]
)
]
db.conf.truncate()
db.conf.bulk_insert(confs)
db.commit()
def update_entries(db, cryptodb):
db.entry.truncate()
def aux(cryptodb, entries):
for key, entry in entries:
fields_orig = mybibtex.generator.bibtex_entry_format_fields(db, key, entry, expand_crossrefs=False)
fields = {k: v.to_bib(expand=False) for (k,v) in fields_orig.items()}
fields["type"] = entry.type.lower()
fields["key_conf"] = key.confkey
fields["key_year"] = tools.short_to_full_year(key.year)
fields["key_auth"] = key.auth
fields["key_dis"] = key.dis
start_page = None
end_page = None
if "pages" in fields:
pages = fields["pages"]
if pages.isdigit():
start_page = pages
else:
a = pages[1:-1].split("--")
if len(a) == 1 or len(a) == 2:
start_page = a[0]
if len(a) == 2:
end_page = a[1]
fields["start_page"] = start_page
fields["end_page"] = end_page
if "years" in fields:
fields["years"] = int(fields["years"])
if "pages" in fields:
del fields["pages"]
if "crossref" in fields:
fields["crossref_expanded"] = fields_orig["crossref"].to_bib(expand=True)[1:-1] # expand and remove quotes
db.entry.insert(**fields)
db.commit()
entries = dict(mybibtex.generator.FilterPaper().filter(cryptodb.entries))
aux(cryptodb, mybibtex.generator.SortConfYearPage().sort(iter(entries.items())))
crossrefs = dict()
for k, e in entries.items():
if "crossref" in e.fields:
crossref = mybibtex.generator.EntryKey.from_string(e.fields["crossref"].expand())
if crossref not in crossrefs:
crossrefs[crossref] = cryptodb.entries[crossref]
aux(cryptodb, mybibtex.generator.SortConfYearPage().sort(iter(crossrefs.items())))
def main():
app = Storage(gluon.shell.env("cryptobib", import_models = True))
print("* read crypto_db.bib")
parser = mybibtex.parser.Parser()
parser.parse_file("../db/abbrev0.bib")
parser.parse_file("../db/crypto_db.bib")
cryptodb = parser.parse_file("../db/crypto_conf_list.bib")
confs_years = get_confs_years_inter(cryptodb, confs_missing_years)
print("* update changes table")
update_changes(app.db)
print("* update confs table")
update_confs(app.db, confs_years)
print("* update entries table")
update_entries(app.db, cryptodb)
if __name__ == "__main__":
main()