42
|
1
|
|
2 class tooloutHTMLifyer(self):
|
|
3
|
|
4 def compressPDF(self,inpdf=None,thumbformat='png'):
|
|
5 """need absolute path to pdf
|
|
6 note that GS gets confoozled if no $TMP or $TEMP
|
|
7 so we set it
|
|
8 """
|
|
9 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
|
|
10 hlog = os.path.join(self.opts.output_dir,"compress_%s.txt" % os.path.basename(inpdf))
|
|
11 sto = open(hlog,'a')
|
|
12 our_env = os.environ.copy()
|
|
13 our_tmp = our_env.get('TMP',None)
|
|
14 if not our_tmp:
|
|
15 our_tmp = our_env.get('TEMP',None)
|
|
16 if not (our_tmp and os.path.exists(our_tmp)):
|
|
17 newtmp = os.path.join(self.opts.output_dir,'tmp')
|
|
18 try:
|
|
19 os.mkdir(newtmp)
|
|
20 except:
|
|
21 sto.write('## WARNING - cannot make %s - it may exist or permissions need fixing\n' % newtmp)
|
|
22 our_env['TEMP'] = newtmp
|
|
23 if not self.temp_warned:
|
|
24 sto.write('## WARNING - no $TMP or $TEMP!!! Please fix - using %s temporarily\n' % newtmp)
|
|
25 self.temp_warned = True
|
|
26 outpdf = '%s_compressed' % inpdf
|
|
27 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dUseCIEColor", "-dBATCH","-dPDFSETTINGS=/printer", "-sOutputFile=%s" % outpdf,inpdf]
|
|
28 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env)
|
|
29 retval1 = x.wait()
|
|
30 sto.close()
|
|
31 if retval1 == 0:
|
|
32 os.unlink(inpdf)
|
|
33 shutil.move(outpdf,inpdf)
|
|
34 os.unlink(hlog)
|
|
35 hlog = os.path.join(self.opts.output_dir,"thumbnail_%s.txt" % os.path.basename(inpdf))
|
|
36 sto = open(hlog,'w')
|
|
37 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
|
|
38 if self.useGM:
|
|
39 cl2 = ['gm', 'convert', inpdf, outpng]
|
|
40 else: # assume imagemagick
|
|
41 cl2 = ['convert', inpdf, outpng]
|
|
42 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env)
|
|
43 retval2 = x.wait()
|
|
44 sto.close()
|
|
45 if retval2 == 0:
|
|
46 os.unlink(hlog)
|
|
47 retval = retval1 or retval2
|
|
48 return retval
|
|
49
|
|
50
|
|
51 def getfSize(self,fpath,outpath):
|
|
52 """
|
|
53 format a nice file size string
|
|
54 """
|
|
55 size = ''
|
|
56 fp = os.path.join(outpath,fpath)
|
|
57 if os.path.isfile(fp):
|
|
58 size = '0 B'
|
|
59 n = float(os.path.getsize(fp))
|
|
60 if n > 2**20:
|
|
61 size = '%1.1f MB' % (n/2**20)
|
|
62 elif n > 2**10:
|
|
63 size = '%1.1f KB' % (n/2**10)
|
|
64 elif n > 0:
|
|
65 size = '%d B' % (int(n))
|
|
66 return size
|
|
67
|
|
68 def makeHtml(self):
|
|
69 """ Create an HTML file content to list all the artifacts found in the output_dir
|
|
70 """
|
|
71
|
|
72 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
73 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
74 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
75 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
|
|
76 <title></title>
|
|
77 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
|
|
78 </head>
|
|
79 <body>
|
|
80 <div class="toolFormBody">
|
|
81 """
|
|
82 galhtmlattr = """<hr/><div class="infomessage">This tool (%s) was generated by the <a href="https://bitbucket.org/fubar/galaxytoolfactory/overview">Galaxy Tool Factory</a></div><br/>"""
|
|
83 galhtmlpostfix = """</div></body></html>\n"""
|
|
84
|
|
85 flist = os.listdir(self.opts.output_dir)
|
|
86 flist = [x for x in flist if x != 'Rplots.pdf']
|
|
87 flist.sort()
|
|
88 html = []
|
|
89 html.append(galhtmlprefix % progname)
|
|
90 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.toolname,timenow()))
|
|
91 fhtml = []
|
|
92 if len(flist) > 0:
|
|
93 logfiles = [x for x in flist if x.lower().endswith('.log')] # log file names determine sections
|
|
94 logfiles.sort()
|
|
95 logfiles = [x for x in logfiles if os.path.abspath(x) != os.path.abspath(self.tlog)]
|
|
96 logfiles.append(os.path.abspath(self.tlog)) # make it the last one
|
|
97 pdflist = []
|
|
98 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf'])
|
|
99 for rownum,fname in enumerate(flist):
|
|
100 dname,e = os.path.splitext(fname)
|
|
101 sfsize = self.getfSize(fname,self.opts.output_dir)
|
|
102 if e.lower() == '.pdf' : # compress and make a thumbnail
|
|
103 thumb = '%s.%s' % (dname,self.thumbformat)
|
|
104 pdff = os.path.join(self.opts.output_dir,fname)
|
|
105 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
|
|
106 if retval == 0:
|
|
107 pdflist.append((fname,thumb))
|
|
108 else:
|
|
109 pdflist.append((fname,fname))
|
|
110 if (rownum+1) % 2 == 0:
|
|
111 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
|
|
112 else:
|
|
113 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize))
|
|
114 for logfname in logfiles: # expect at least tlog - if more
|
|
115 if os.path.abspath(logfname) == os.path.abspath(self.tlog): # handled later
|
|
116 sectionname = 'All tool run'
|
|
117 if (len(logfiles) > 1):
|
|
118 sectionname = 'Other'
|
|
119 ourpdfs = pdflist
|
|
120 else:
|
|
121 realname = os.path.basename(logfname)
|
|
122 sectionname = os.path.splitext(realname)[0].split('_')[0] # break in case _ added to log
|
|
123 ourpdfs = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] == sectionname]
|
|
124 pdflist = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] != sectionname] # remove
|
|
125 nacross = 1
|
|
126 npdf = len(ourpdfs)
|
|
127
|
|
128 if npdf > 0:
|
|
129 nacross = math.sqrt(npdf) ## int(round(math.log(npdf,2)))
|
|
130 if int(nacross)**2 != npdf:
|
|
131 nacross += 1
|
|
132 nacross = int(nacross)
|
|
133 width = min(400,int(1200/nacross))
|
|
134 html.append('<div class="toolFormTitle">%s images and outputs</div>' % sectionname)
|
|
135 html.append('(Click on a thumbnail image to download the corresponding original PDF image)<br/>')
|
|
136 ntogo = nacross # counter for table row padding with empty cells
|
|
137 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>')
|
|
138 for i,paths in enumerate(ourpdfs):
|
|
139 fname,thumb = paths
|
|
140 s= """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d"
|
|
141 alt="Image called %s"/></a></td>\n""" % (fname,thumb,fname,width,fname)
|
|
142 if ((i+1) % nacross == 0):
|
|
143 s += '</tr>\n'
|
|
144 ntogo = 0
|
|
145 if i < (npdf - 1): # more to come
|
|
146 s += '<tr>'
|
|
147 ntogo = nacross
|
|
148 else:
|
|
149 ntogo -= 1
|
|
150 html.append(s)
|
|
151 if html[-1].strip().endswith('</tr>'):
|
|
152 html.append('</table></div>\n')
|
|
153 else:
|
|
154 if ntogo > 0: # pad
|
|
155 html.append('<td> </td>'*ntogo)
|
|
156 html.append('</tr></table></div>\n')
|
|
157 logt = open(logfname,'r').readlines()
|
|
158 logtext = [x for x in logt if x.strip() > '']
|
|
159 html.append('<div class="toolFormTitle">%s log output</div>' % sectionname)
|
|
160 if len(logtext) > 1:
|
|
161 html.append('\n<pre>\n')
|
|
162 html += logtext
|
|
163 html.append('\n</pre>\n')
|
|
164 else:
|
|
165 html.append('%s is empty<br/>' % logfname)
|
|
166 if len(fhtml) > 0:
|
|
167 fhtml.insert(0,'<div><table class="colored" cellpadding="3" cellspacing="3"><tr><th>Output File Name (click to view)</th><th>Size</th></tr>\n')
|
|
168 fhtml.append('</table></div><br/>')
|
|
169 html.append('<div class="toolFormTitle">All output files available for downloading</div>\n')
|
|
170 html += fhtml # add all non-pdf files to the end of the display
|
|
171 else:
|
|
172 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter)
|
|
173 html.append(galhtmlpostfix)
|
|
174 htmlf = file(self.opts.output_html,'w')
|
|
175 htmlf.write('\n'.join(html))
|
|
176 htmlf.write('\n')
|
|
177 htmlf.close()
|
|
178 self.html = html
|
|
179
|
|
180
|