-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFCstatus.py
More file actions
260 lines (213 loc) · 7.19 KB
/
RFCstatus.py
File metadata and controls
260 lines (213 loc) · 7.19 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Find obsoleted normative RFCs"""
# Version: 2024-12-18 - original
# Version: 2024-12-19 - tag obsoleted STDs with unknown STD number
# - list IS separately from DS & PS
# Version: 2024-12-24 - switch to proper xml parser
########################################################
# 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 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 interesting(block):
"""Save interesting RFC data from XML block"""
global stds, bcps
status = block['current-status']
if status in ["UNKNOWN", "HISTORIC"]:
return False
elif not field("obsoleted-by", block):
return False
else:
#Potentially interesting
#print(block)
if field("is-also", block):
also = block["is-also"]["doc-id"]
if also.startswith("BCP0"):
also = " (BCP"+also[3:].lstrip("0")+")"
elif also.startswith("STD0"):
also = " (STD"+also[3:].lstrip("0")+")"
elif status == "INTERNET STANDARD":
also = " (STD?)"
else:
also = ""
#print("Also: ",also)
if status == "INTERNET STANDARD":
istds.append(doc_id(block)+also+": "+title(block))
elif "STANDARD" in status:
stds.append(doc_id(block)+also+": "+title(block))
elif status == "BEST CURRENT PRACTICE":
bcps.append(doc_id(block)+also+": "+title(block))
return True
return False
######### Startup
#Define some globals
inrfc = False
new = ''
count = 0
stds = []
istds = []
bcps = []
printing = False # True for extra diagnostic prints
warnings = 0
#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 = "Obsoleted Normative RFCs lister."
printing = askyesno(title=T,
message = "Diagnostic printing?")
os.chdir(askdirectory(title = "Select directory"))
#Open log file
flog = open("RFCstatus.log", "w",encoding='utf-8')
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC%z",time.localtime())
logit("RFCstatus 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']
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC%z",time.localtime())
for r in all_rfcs:
if interesting(r):
count += 1
logit(str(count)+" obsoleted non-historic RFCs found")
md = ["## RFC status","",
"This is a list of all obsoleted normative RFCs that are not marked Historic"]
md += ["","RFC status run at "+timestamp]
md += ["","### Internet Standards ("+str(len(istds))+" RFCs)",""]
md += istds
md += ["","### Draft or Proposed Standards ("+str(len(stds))+" RFCs)",""]
md += stds
md += ["", "### Best Current Practice ("+str(len(bcps))+" RFCs)", ""]
md += bcps
wf("RFCstatus.md", md)
######### Close log and exit
flog.close()
if warnings:
warn = str(warnings)+" warning(s)\n"
else:
warn = ""
show(warn+"Check RFCstatus.log.")