Skip to content

Commit 8b72b6a

Browse files
committed
Made the stats backfill script slightly more verbose, including some blank lines for readability. Changed output to go both to screen and to file. Made the unicode/latin1/ascii decoding more general.
- Legacy-Id: 13535
1 parent 20b4250 commit 8b72b6a

1 file changed

Lines changed: 51 additions & 12 deletions

File tree

ietf/stats/backfill_data.py

100644100755
Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/env python
22

3-
import sys, os, argparse
3+
from __future__ import print_function, unicode_literals
4+
5+
import sys
6+
import os
7+
import os.path
8+
import argparse
9+
import time
410

511
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
612
sys.path = [ basedir ] + sys.path
@@ -15,6 +21,8 @@
1521

1622
from django.conf import settings
1723

24+
import debug # pyflakes:ignore
25+
1826
from ietf.doc.models import Document
1927
from ietf.name.models import FormalLanguageName
2028
from ietf.utils.draft import Draft
@@ -33,6 +41,31 @@
3341
if args.document:
3442
docs_qs = docs_qs.filter(docalias__name=args.document)
3543

44+
ts = time.strftime("%Y-%m-%d_%H:%M%z")
45+
logfile = open('backfill-authorstats-%s.log'%ts, 'w')
46+
print("Writing log to %s" % os.path.abspath(logfile.name))
47+
48+
def say(msg):
49+
msg = msg.encode('utf8')
50+
sys.stderr.write(msg)
51+
sys.stderr.write('\n')
52+
logfile.write(msg)
53+
logfile.write('\n')
54+
55+
def unicode(text):
56+
if text is None:
57+
return text
58+
# order matters here:
59+
for encoding in ['ascii', 'utf8', 'latin1', ]:
60+
try:
61+
utext = text.decode(encoding)
62+
if encoding == 'latin1':
63+
say("Warning: falling back to latin1 decoding for %s" % utext)
64+
return utext
65+
except UnicodeDecodeError:
66+
pass
67+
68+
start = time.time()
3669
for doc in docs_qs.prefetch_related("docalias_set", "formal_languages", "documentauthor_set", "documentauthor_set__person", "documentauthor_set__person__alias_set"):
3770
canonical_name = doc.name
3871
for n in doc.docalias_set.all():
@@ -45,11 +78,12 @@
4578
path = os.path.join(settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR, canonical_name + "-" + doc.rev + ".txt")
4679

4780
if not os.path.exists(path):
48-
print "skipping", doc.name, "no txt file found at", path
81+
say("Skipping %s, no txt file found at %s" % (doc.name, path))
4982
continue
5083

5184
with open(path, 'r') as f:
52-
print "\nProcessing %s" % doc.name
85+
say("\nProcessing %s" % doc.name)
86+
sys.stdout.flush()
5387
d = Draft(f.read(), path)
5488

5589
updated = False
@@ -92,6 +126,7 @@
92126
# it's an extra author - skip those extra authors
93127
seen = set()
94128
for full, _, _, _, _, email, country, company in d.get_author_list():
129+
full, email, country, company = [ unicode(s) for s in [full, email, country, company, ] ]
95130
if email in seen:
96131
continue
97132
seen.add(email)
@@ -103,25 +138,20 @@
103138
old_author = old_authors_by_name.get(full)
104139

105140
if not old_author:
106-
print "UNKNOWN AUTHOR", doc.name, full, email, country, company
141+
say("UNKNOWN AUTHOR: %s, %s, %s, %s, %s" % (doc.name, full, email, country, company))
107142
continue
108143

109144
if old_author.affiliation != company:
110-
print "new affiliation", canonical_name, "[", full, "]", old_author.affiliation, "->", company
145+
say("new affiliation: %s [ %s <%s> ] %s -> %s" % (canonical_name, full, email, old_author.affiliation, company))
111146
old_author.affiliation = company
112147
old_author.save(update_fields=["affiliation"])
113148
updated = True
114149

115150
if country is None:
116151
country = ""
117152

118-
try:
119-
country = country.decode("utf-8")
120-
except UnicodeDecodeError:
121-
country = country.decode("latin-1")
122-
123153
if old_author.country != country:
124-
print "new country", canonical_name ,"[", full, email, "]", old_author.country.encode("utf-8"), "->", country.encode("utf-8")
154+
say("new country: %s [ %s <%s> ] %s -> %s" % (canonical_name , full, email, old_author.country, country))
125155
old_author.country = country
126156
old_author.save(update_fields=["country"])
127157
updated = True
@@ -132,5 +162,14 @@
132162
updated = True
133163

134164
if updated:
135-
print "updated", canonical_name
165+
say("updated: %s" % canonical_name)
166+
167+
stop = time.time()
168+
dur = stop-start
169+
sec = dur%60
170+
min = dur//60
171+
say("Processing time %d:%02d" % (min, sec))
172+
173+
print("\n\nWrote log to %s" % os.path.abspath(logfile.name))
174+
logfile.close()
136175

0 commit comments

Comments
 (0)