/
obsol.py
275 lines (226 loc) · 7.42 KB
/
obsol.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Analyse obsoleted/obsoleting RFCs"""
# Version: 2024-02-17 - original
########################################################
# Copyright (C) 2024 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 requests
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"""
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 tot_up(item):
global nochange
head, tail = item.split(" obsoleted ")
_, head = head.split(" ", maxsplit=1)
tail, _ = tail.rsplit(" ", maxsplit=1)
#print(head,tail,head==tail)
if head == tail:
nochange += 1
else:
orig = states.index(tail)
new = states.index(head)
counts[orig][new] += 1
def field(fname, block):
"""Extract 1st version of a named field from XML block"""
if fname in block:
_,temp = block.split("<"+fname+">", maxsplit=1)
result,_ = temp.split("</"+fname+">", maxsplit=1)
return result
return ""
def title(block):
"""Extract 1st title from XML block"""
return field("title", block)
def doc_id(block):
"""Extract 1st doc-id from XML block"""
return field("doc-id", block)
def obsoleted(block):
"""Save obsolescence data from XML block"""
global stds
if "<obsoleted-by>" in block:
#print(block)
statuses.append(field("current-status", block)+" "+doc_id(block))
i=block.index("<obsoleted-by>")
obsols.append(field("doc-id", block[i:]))
return True
else:
return False
def obsoleting(block):
"""Complete obsolescence data from XML block"""
global obsols, statuses, results
if doc_id(block) in obsols:
status = field("current-status", block)
old = statuses[obsols.index(doc_id(block))]
result = doc_id(block)+" "+status+" obsoleted "+old
tot_up(result)
results.append(result)
return True
else:
return False
######### Startup
#Define some globals
inrfc = False
new = ''
numberfound = False
count = 0
nochange = 0
obsols = []
statuses = []
results = []
states = ["UNKNOWN","HISTORIC","PROPOSED STANDARD","DRAFT STANDARD",
"INTERNET STANDARD", "BEST CURRENT PRACTICE", "EXPERIMENTAL",
"INFORMATIONAL"]
abbrevs = ["UNK","HIS","PS ","DS ","IS ","BCP","EXP","INF"]
header = " "
for x in abbrevs:
header += x+" "
counts = [[0 for col in range(len(states))] for row in range(len(states))]
printing = False # True for extra diagnostic prints
warnings = 0
#Announce
Tk().withdraw() # we don't want a full GUI
T = "Analyse RFC obsolescence"
printing = askyesno(title=T,
message = "Diagnostic printing?")
#Open log file
flog = open("obsol.log", "w",encoding='utf-8')
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC%z",time.localtime())
logit("obsol run at "+timestamp)
logit("Running in directory "+ os.getcwd())
showinfo(title=T,
message = "Will read complete RFC index.\nTouch no files until done!")
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 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")
whole = rf(fp)
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC%z",time.localtime())
for line in whole:
if (not inrfc) and (not "<rfc-entry>" in line):
continue
inrfc = True
new += line
if inrfc and "</rfc-entry>" in line:
#end of an rfc entry
if obsoleted(new):
count += 1
inrfc = False
numberfound = False
new = ''
continue
for line in whole:
if (not inrfc) and (not "<rfc-entry>" in line):
continue
inrfc = True
new += line
if inrfc and "</rfc-entry>" in line:
#end of an rfc entry
if obsoleting(new):
count += 1
inrfc = False
numberfound = False
new = ''
continue
logit(str(len(obsols))+" obsoleted RFCs found")
logit(str(nochange)+": no change of status\n")
logit(header+" <-- new status")
for i in range(len(states)):
out = abbrevs[i]
for j in range(len(states)):
out += " " +format(counts[i][j], '03d')
logit(out)
logit("|\n|___old status\n")
wf("obsoleters.txt", results)
######### Close log and exit
flog.close()
if warnings:
warn = str(warnings)+" warning(s)\n"
else:
warn = ""
showinfo(title=T,
message = warn+"Done, summary in obsol.log")