77import re
88import tarfile
99
10+ from tempfile import mkstemp
11+
1012from django .shortcuts import render_to_response , get_object_or_404
1113from ietf .idtracker .models import IETFWG , IRTF , Area
1214from django .views .generic .list_detail import object_list
2123from django .db .models import Count
2224from ietf .idtracker .models import InternetDraft
2325from ietf .idrfc .idrfc_wrapper import IdWrapper
26+ from ietf .utils .pipe import pipe
2427
2528from ietf .proceedings .models import Meeting , MeetingTime , WgMeetingSession , MeetingVenue , IESGHistory , Proceeding , Switches , WgProceedingsActivities
2629
@@ -154,8 +157,6 @@ def session_agenda(request, num, session, ext=None):
154157 raise Http404 ("No agenda for the %s session of IETF %s is available" % (session , num ))
155158
156159def convert_to_pdf (doc_name ):
157- from tempfile import mkstemp
158- from ietf .utils .pipe import pipe
159160 import subprocess
160161 inpath = os .path .join (settings .INTERNET_DRAFT_PATH , doc_name + ".txt" )
161162 outpath = os .path .join (settings .INTERNET_DRAFT_PDF_PATH , doc_name + ".pdf" )
@@ -202,9 +203,9 @@ def convert_to_pdf(doc_name):
202203 os .unlink (psname )
203204
204205
205- def session_draft_tarfile (request , num , session ):
206- from tempfile import mkstemp
206+ def session_draft_list (num , session ):
207207 extensions = ["html" , "htm" , "txt" , "HTML" , "HTM" , "TXT" , ]
208+ result = []
208209 found = False
209210 for wg in [session , session .upper (), session .lower ()]:
210211 for e in extensions :
@@ -222,21 +223,28 @@ def session_draft_tarfile(request, num, session):
222223
223224 drafts = set (re .findall ('(draft-[-a-z0-9]*)' ,agenda ))
224225
225- response = HttpResponse (mimetype = 'application/octet-stream' )
226- response ['Content-Disposition' ] = 'attachment; filename=%s-drafts.tgz' % (session )
227- tarstream = tarfile .open ('' ,'w:gz' ,response )
228- mfh , mfn = mkstemp ()
229- manifest = open (mfn , "w" )
230-
231226 for draft in drafts :
232227 if (re .search ('-[0-9]{2}$' ,draft )):
233228 doc_name = draft
234229 else :
235230 id = get_object_or_404 (InternetDraft , filename = draft )
236231 doc = IdWrapper (id )
237232 doc_name = draft + "-" + id .revision
233+ result .append (doc_name )
238234
239- txt_path = os .path .join (settings .INTERNET_DRAFT_PATH , doc_name + ".txt" )
235+ return sorted (list (set (result )))
236+
237+
238+ def session_draft_tarfile (request , num , session ):
239+ drafts = session_draft_list (num , session );
240+
241+ response = HttpResponse (mimetype = 'application/octet-stream' )
242+ response ['Content-Disposition' ] = 'attachment; filename=%s-drafts.tgz' % (session )
243+ tarstream = tarfile .open ('' ,'w:gz' ,response )
244+ mfh , mfn = mkstemp ()
245+ manifest = open (mfn , "w" )
246+
247+ for doc_name in drafts :
240248 pdf_path = os .path .join (settings .INTERNET_DRAFT_PDF_PATH , doc_name + ".pdf" )
241249
242250 if (not os .path .exists (pdf_path )):
@@ -256,3 +264,45 @@ def session_draft_tarfile(request, num, session):
256264 tarstream .close ()
257265 os .unlink (mfn )
258266 return response
267+
268+ def pdf_pages (file ):
269+ try :
270+ infile = open (file , "r" )
271+ except Exception , e :
272+ return 0
273+ for line in infile :
274+ m = re .match ('\] /Count ([0-9]+)' ,line )
275+ if m :
276+ return int (m .group (1 ))
277+ return 0
278+
279+
280+ def session_draft_pdf (request , num , session ):
281+ drafts = session_draft_list (num , session );
282+ curr_page = 1
283+ pmh , pmn = mkstemp ()
284+ pdfmarks = open (pmn , "w" )
285+ pdf_list = ""
286+
287+ for draft in drafts :
288+ pdf_path = os .path .join (settings .INTERNET_DRAFT_PDF_PATH , draft + ".pdf" )
289+ if (not os .path .exists (pdf_path )):
290+ convert_to_pdf (draft )
291+
292+ if (os .path .exists (pdf_path )):
293+ pages = pdf_pages (pdf_path )
294+ pdfmarks .write ("[/Page " + str (curr_page )+ " /View [/XYZ 0 792 1.0] /Title (" + draft + ") /OUT pdfmark\n " )
295+ pdf_list = pdf_list + " " + pdf_path
296+ curr_page = curr_page + pages
297+
298+ pdfmarks .close ()
299+ pdfh , pdfn = mkstemp ()
300+ pipe ("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=" + pdfn + " " + pdf_list + " " + pmn )
301+
302+ pdf = open (pdfn ,"r" )
303+ pdf_contents = pdf .read ()
304+ pdf .close ()
305+
306+ os .unlink (pmn )
307+ os .unlink (pdfn )
308+ return HttpResponse (pdf_contents , mimetype = "application/pdf" )
0 commit comments