|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
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 |
4 | 10 |
|
5 | 11 | basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) |
6 | 12 | sys.path = [ basedir ] + sys.path |
|
15 | 21 |
|
16 | 22 | from django.conf import settings |
17 | 23 |
|
| 24 | +import debug # pyflakes:ignore |
| 25 | + |
18 | 26 | from ietf.doc.models import Document |
19 | 27 | from ietf.name.models import FormalLanguageName |
20 | 28 | from ietf.utils.draft import Draft |
|
33 | 41 | if args.document: |
34 | 42 | docs_qs = docs_qs.filter(docalias__name=args.document) |
35 | 43 |
|
| 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() |
36 | 69 | for doc in docs_qs.prefetch_related("docalias_set", "formal_languages", "documentauthor_set", "documentauthor_set__person", "documentauthor_set__person__alias_set"): |
37 | 70 | canonical_name = doc.name |
38 | 71 | for n in doc.docalias_set.all(): |
|
45 | 78 | path = os.path.join(settings.INTERNET_ALL_DRAFTS_ARCHIVE_DIR, canonical_name + "-" + doc.rev + ".txt") |
46 | 79 |
|
47 | 80 | 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)) |
49 | 82 | continue |
50 | 83 |
|
51 | 84 | with open(path, 'r') as f: |
52 | | - print "\nProcessing %s" % doc.name |
| 85 | + say("\nProcessing %s" % doc.name) |
| 86 | + sys.stdout.flush() |
53 | 87 | d = Draft(f.read(), path) |
54 | 88 |
|
55 | 89 | updated = False |
|
92 | 126 | # it's an extra author - skip those extra authors |
93 | 127 | seen = set() |
94 | 128 | for full, _, _, _, _, email, country, company in d.get_author_list(): |
| 129 | + full, email, country, company = [ unicode(s) for s in [full, email, country, company, ] ] |
95 | 130 | if email in seen: |
96 | 131 | continue |
97 | 132 | seen.add(email) |
|
103 | 138 | old_author = old_authors_by_name.get(full) |
104 | 139 |
|
105 | 140 | 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)) |
107 | 142 | continue |
108 | 143 |
|
109 | 144 | 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)) |
111 | 146 | old_author.affiliation = company |
112 | 147 | old_author.save(update_fields=["affiliation"]) |
113 | 148 | updated = True |
114 | 149 |
|
115 | 150 | if country is None: |
116 | 151 | country = "" |
117 | 152 |
|
118 | | - try: |
119 | | - country = country.decode("utf-8") |
120 | | - except UnicodeDecodeError: |
121 | | - country = country.decode("latin-1") |
122 | | - |
123 | 153 | 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)) |
125 | 155 | old_author.country = country |
126 | 156 | old_author.save(update_fields=["country"]) |
127 | 157 | updated = True |
|
132 | 162 | updated = True |
133 | 163 |
|
134 | 164 | 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() |
136 | 175 |
|
0 commit comments