4949log_data = ""
5050def 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
0 commit comments