Skip to content

Commit d90a550

Browse files
committed
Added test cases for rfc-index.xml/queue2.xml parsing; refactored mirroring scripts to support testing; fixed minor bug in queue2.xml parsing (was creating unnecessary references)
- Legacy-Id: 1826
1 parent 0f81d3e commit d90a550

3 files changed

Lines changed: 430 additions & 44 deletions

File tree

ietf/idrfc/mirror_rfc_editor_queue.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
log_data = ""
5050
def log(line):
5151
global log_data
52-
if len(sys.argv) > 1:
52+
if __name__ == '__main__' and len(sys.argv) > 1:
5353
print line
5454
else:
5555
log_data += line + "\n"
@@ -125,6 +125,14 @@ def recurse(draft_name, ref_set, level):
125125
ref_set.add(destination)
126126
recurse(destination, ref_set, level+1)
127127
if level == 0:
128+
# Remove self-reference
129+
ref_set.remove(draft_name)
130+
# Remove direct references
131+
for (source, destination, in_queue, direct) in refs:
132+
if source == draft_name:
133+
if destination in ref_set:
134+
ref_set.remove(destination)
135+
# The rest are indirect references
128136
for ref in ref_set:
129137
if draft_name != ref:
130138
result.append([draft_name, ref, ref in draft_names, False])
@@ -151,14 +159,7 @@ def find_document_ids(cursor, drafts, refs):
151159
refs2.append([draft_ids[ref[0]]]+ref[1:])
152160
return (drafts2, refs2)
153161

154-
try:
155-
log("output from mirror_rfc_editor_queue.py:\n")
156-
log("time: "+str(datetime.now()))
157-
log("host: "+socket.gethostname())
158-
log("url: "+QUEUE_URL)
159-
160-
log("downloading...")
161-
response = urllib2.urlopen(QUEUE_URL)
162+
def parse_all(response):
162163
log("parsing...")
163164
(drafts, refs) = parse(response)
164165
log("got "+ str(len(drafts)) + " drafts and "+str(len(refs))+" direct refs")
@@ -168,18 +169,17 @@ def find_document_ids(cursor, drafts, refs):
168169
refs.extend(indirect_refs)
169170
del(indirect_refs)
170171

171-
if len(drafts) < 10 or len(refs) < 10:
172-
raise Exception('not enough data')
173-
174172
# convert filenames to id_document_tags
175173
log("connecting to database...")
176174
cursor = db.connection.cursor()
177175
log("finding id_document_tags...")
178176
(drafts, refs) = find_document_ids(cursor, drafts, refs)
177+
cursor.close()
178+
return (drafts, refs)
179179

180-
if len(drafts) < 10 or len(refs) < 10:
181-
raise Exception('not enough data')
182-
180+
def insert_into_database(drafts, refs):
181+
log("connecting to database...")
182+
cursor = db.connection.cursor()
183183
log("removing old data...")
184184
cursor.execute("DELETE FROM "+TABLE)
185185
cursor.execute("DELETE FROM "+REF_TABLE)
@@ -190,10 +190,26 @@ def find_document_ids(cursor, drafts, refs):
190190
cursor.close()
191191
db.connection._commit()
192192
db.connection.close()
193-
194-
log("all done!")
195-
if log_data.find("WARNING") < 0:
196-
log_data = ""
197-
finally:
198-
if len(log_data) > 0:
199-
print log_data
193+
194+
if __name__ == '__main__':
195+
try:
196+
log("output from mirror_rfc_editor_queue.py:\n")
197+
log("time: "+str(datetime.now()))
198+
log("host: "+socket.gethostname())
199+
log("url: "+QUEUE_URL)
200+
201+
log("downloading...")
202+
response = urllib2.urlopen(QUEUE_URL)
203+
204+
(drafts, refs) = parse_all(response)
205+
if len(drafts) < 10 or len(refs) < 10:
206+
raise Exception('not enough data')
207+
208+
insert_into_database(drafts, refs)
209+
210+
log("all done!")
211+
if log_data.find("WARNING") < 0:
212+
log_data = ""
213+
finally:
214+
if len(log_data) > 0:
215+
print log_data

ietf/idrfc/mirror_rfc_index.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
log_data = ""
4949
def log(line):
5050
global log_data
51-
if len(sys.argv) > 1:
51+
if __name__ == '__main__' and len(sys.argv) > 1:
5252
print line
5353
else:
5454
log_data += line + "\n"
@@ -127,21 +127,7 @@ def getDocList(parentNode, tagName):
127127
d[9] = ",".join(also_list[k])
128128
return data
129129

130-
try:
131-
log("output from mirror_rfc_index.py:\n")
132-
log("time: "+str(datetime.now()))
133-
log("host: "+socket.gethostname())
134-
log("url: "+INDEX_URL)
135-
136-
log("downloading...")
137-
response = urllib2.urlopen(INDEX_URL)
138-
log("parsing...")
139-
data = parse(response)
140-
141-
log("got " + str(len(data)) + " entries")
142-
if len(data) < 5000:
143-
raise Exception('not enough data')
144-
130+
def insert_to_database(data):
145131
log("connecting to database...")
146132
cursor = db.connection.cursor()
147133
log("removing old data...")
@@ -152,9 +138,27 @@ def getDocList(parentNode, tagName):
152138
db.connection._commit()
153139
db.connection.close()
154140

155-
log("all done!")
156-
log_data = ""
141+
if __name__ == '__main__':
142+
try:
143+
log("output from mirror_rfc_index.py:\n")
144+
log("time: "+str(datetime.now()))
145+
log("host: "+socket.gethostname())
146+
log("url: "+INDEX_URL)
147+
148+
log("downloading...")
149+
response = urllib2.urlopen(INDEX_URL)
150+
log("parsing...")
151+
data = parse(response)
152+
153+
log("got " + str(len(data)) + " entries")
154+
if len(data) < 5000:
155+
raise Exception('not enough data')
156+
157+
insert_to_database(data)
158+
159+
log("all done!")
160+
log_data = ""
157161

158-
finally:
159-
if len(log_data) > 0:
160-
print log_data
162+
finally:
163+
if len(log_data) > 0:
164+
print log_data

0 commit comments

Comments
 (0)