annotate toolfactory/html_dir.py @ 43:e7e9732ebed6 draft

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