annotate make_html.py @ 3:de6d0b7c17af draft

release 1.6.3
author lgueguen
date Mon, 01 Oct 2018 05:07:56 -0400
parents 581d217c7337
children 05c9b1a7f44e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
1 #Author: Loraine Gueguen
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
2
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
3 import sys
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
4 import os
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
5 import time
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
6 import argparse
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
7
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
8 def getfSize(fpath,outpath):
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
9 """
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
10 format a nice file size string
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
11 """
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
12 size = ''
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
13 fp = os.path.join(outpath,fpath)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
14 if os.path.isfile(fp):
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
15 size = '0 B'
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
16 n = float(os.path.getsize(fp))
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
17 if n > 2**20:
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
18 size = '%1.1f MB' % (n/2**20)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
19 elif n > 2**10:
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
20 size = '%1.1f KB' % (n/2**10)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
21 elif n > 0:
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
22 size = '%d B' % (int(n))
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
23 return size
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
24
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
25 # Define option
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
26 parser = argparse.ArgumentParser(description='Create an html page for downloading files from a directory')
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
27 parser.add_argument('--tool', help='Galaxy tool', required=True)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
28 parser.add_argument('--output_type', help='figures or tables', required=True)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
29 parser.add_argument('--output_dir', help='Output directory', required=True)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
30 parser.add_argument('--output_html', help='Output HTML file name', default='', required=True)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
31
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
32 #Parse the command line
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
33 args = parser.parse_args()
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
34 tool=args.tool
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
35 output_type=args.output_type
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
36 output_dir=args.output_dir
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
37 output_html=args.output_html
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
38
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
39
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
40 debut_html="""<!DOCTYPE html>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
41 <html>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
42 <head>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
43 <meta charset="utf-8" />
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
44 <title></title>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
45 </head>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
46 <body>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
47 <h1>Galaxy Tool %s</h1>
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
48 """ %(tool)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
49
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
50 fin_html="</body></html>"
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
51
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
52 html=debut_html
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
53 run_date=time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
54 html+='<div>Run at %s</div><br/>\n'%run_date
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
55 html+='<div><strong>%s</strong> available for downloading</div><br/>\n'%output_type
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
56
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
57
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
58 flist = os.listdir(output_dir)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
59 flist.sort()
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
60
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
61
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
62 html+='<div><table cellpadding="3" cellspacing="3"><tr><th>Output File Name (click to view)</th><th>Size</th></tr>\n'
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
63
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
64 for f in flist :
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
65 size=getfSize(f,output_dir)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
66 html+='<tr><td><a href="%s">%s</a></td><td>%s</td></tr>\n'%(f,f,size)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
67 html+='</table></div><br/>\n'
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
68
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
69 html+=fin_html
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
70
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
71 htmlf = file(output_html,'w')
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
72 htmlf.write(html)
581d217c7337 Planemo upload
lgueguen
parents:
diff changeset
73 htmlf.close()