-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriviata.py
More file actions
280 lines (229 loc) · 7.83 KB
/
triviata.py
File metadata and controls
280 lines (229 loc) · 7.83 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Find trivial errata"""
# Version: 2024-12-20 - original
# Version: 2024-12-21 - some tidying & caching
# Version: 2024-12-21 - cosmetics
# Version: 2024-12-24 - switch to proper xml parser (for speed)
########################################################
# Copyright (C) 2023-24 Brian E. Carpenter.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with
# or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of
# its contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
# AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
########################################################
from tkinter import Tk
from tkinter.filedialog import askdirectory
from tkinter.messagebox import askokcancel, askyesno, showinfo
import time
import os
import sys
import requests
import json
import xmltodict
def show(msg):
"""Show a message"""
global T, cmd_line
if cmd_line:
print(msg)
else:
showinfo(title=T, message = msg)
def logit(msg):
"""Add a message to the log file"""
global flog, printing
flog.write(msg+"\n")
if printing:
print(msg)
def logitw(msg):
"""Add a warning message to the log file"""
global warnings
logit("WARNING: "+msg)
warnings += 1
def dprint(*msg):
""" Diagnostic print """
global printing
if printing:
print(*msg)
def crash(msg):
"""Log and crash"""
global printing
printing = True
logit("CRASH "+msg)
flog.close()
exit()
def rf(f):
"""Return a file as a list of strings"""
file = open(f, "r",encoding='utf-8', errors='replace')
l = file.readlines()
file.close()
#ensure last line has a newline
if l[-1][-1] != "\n":
l[-1] += "\n"
return l
def wf(f,l):
"""Write list of strings to file"""
file = open(f, "w",encoding='utf-8')
for line in l:
file.write(line+"\n")
file.close()
logit("'"+f+"' written")
def field(fname, block):
"""Extract named field from XML block"""
try:
return block[fname]
except:
return None
def title(block):
"""Extract title from XML block"""
return block["title"]
def doc_id(block):
"""Extract doc-id from XML block"""
return block["doc-id"]
def getblock(docid):
"""Get dictionary for a given RFC"""
#if it's in the cache, we're done
try:
return(b_cache[docid])
except:
pass
#not in cache, search the whole list
new = [r for r in all_rfcs if r['doc-id'] == docid]
if len(new) == 1:
b_cache[docid] = new[0]
return new[0]
else:
return False
######### Startup
#Define some globals
printing = False # True for extra diagnostic prints
warnings = 0
b_cache = {}
#Has the user supplied a directory on the command line?
cmd_line = False
if len(sys.argv) > 1:
#user provided directory name?
if os.path.isdir(sys.argv[1]):
#assume user has provided directory
#and set all options to defaults
os.chdir(sys.argv[1])
cmd_line = True
#Announce
if not cmd_line:
Tk().withdraw() # we don't want a full GUI
T = "Find trivial errata."
printing = askyesno(title=T,
message = "Diagnostic printing?")
os.chdir(askdirectory(title = "Select directory"))
#Open log file
flog = open("triviata.log", "w",encoding='utf-8')
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC%z",time.localtime())
logit("triviata run at "+timestamp)
logit("Running in directory "+ os.getcwd())
show("Will read complete RFC index.")
fp = "rfc-index.xml"
if (not os.path.exists(fp)) or (time.time()-os.path.getmtime(fp) > 60*60*24*30):
#need fresh copy of index
try:
if cmd_line or askyesno(title=T, message = "OK to download RFC index?\n(15 MB file)"):
response = requests.get("https://www.rfc-editor.org/rfc/rfc-index.xml")
open(fp, "wb").write(response.content)
logit("Downloaded and cached RFC index")
else:
raise Exception("Invalid choice")
except Exception as E:
logitw(str(E))
crash("Cannot run without RFC index")
xf = open(fp,"r",encoding='utf-8', errors='replace')
index_dict = xmltodict.parse(xf.read())
xf.close()
all_rfcs = index_dict['rfc-index']['rfc-entry']
show("Will read errata.json")
fp = "errata.json"
if (not os.path.exists(fp)) or (time.time()-os.path.getmtime(fp) > 60*60*24*30):
#need fresh copy of errata
try:
if cmd_line or askyesno(title=T, message = "OK to download errata.json?\n(10 MB file)"):
response = requests.get("https://www.rfc-editor.org/errata.json")
open(fp, "wb").write(response.content)
logit("Downloaded and cached errata.json")
else:
raise Exception("Invalid choice")
except Exception as E:
logitw(str(E))
crash("Cannot run without errata.json")
jf=open(fp)
errata=json.load(jf)
jf.close()
ct = 0
ect = 0
old_ct = 0
dead_ct = 0
trivia = []
for e in errata:
if e['errata_status_code'] == 'Reported':
ct += 1
year = e['submit_date'][0:4]
editorial = e['errata_type_code'] == 'Editorial'
docid = e['doc-id']
err_id = e['errata_id']
#print(err_id)
if editorial:
ect += 1
#find info for the RFC
b = getblock(docid)
# if unknown, obsoleted, historic, or legacy - erratum is trivial
if b['current-status']=='UNKNOWN' or b['current-status']=='HISTORIC' or field('obsoleted-by',b) or b['stream']=='Legacy':
trivia += ["Erratum "+err_id+" on "+docid+" (dead RFC)"]
dead_ct += 1
# if editorial before 2020 - erratum trivial
elif editorial and int(b['date']['year']) < 2020:
trivia += ["Erratum "+err_id+" on "+docid+" (old editorial)"]
old_ct += 1
logit(str(ct)+" open errata checked, "+str(ect)+" were editorial")
md = ["## Trivial errata","",
"This is a list of "+str(len(trivia))+" errata reports suggested for rejection",""]
md += ["Generated at "+timestamp,""]
md += [str(ct)+" open reports were checked, of which "+str(ect)+" were editorial."]
md += [str(dead_ct)+" rejects concern dead RFCs (UNKNOWN, HISTORIC, LEGACY or Obsoleted)."]
md += [str(old_ct)+" rejects are editorial reports on pre-2020 RFCs.",""]
md += trivia
wf("triviata.md", md)
######### Close log and exit
flog.close()
if warnings:
warn = str(warnings)+" warning(s)\n"
else:
warn = ""
show(warn+"Check triviata.log.")