#!/usr/bin/python
import commands
import re
import os
import sys
from optparse import OptionParser
usage = "usage: %prog [options] -d directory"
parser = OptionParser(usage=usage)
parser.add_option("-d", "--dir", dest="rep", help ="Picture folder (ending with /)")
parser.add_option("-r", action="store_true", dest="recursive", help ="recursive mode")
#parser.add_option("-R", action="store_true", dest="readme", help ="Include a README")
parser.add_option("-R", type='string', dest="readme", action="store", help="use a directory description file")
#parser.set_defaults(readme="README")
(options, args) = parser.parse_args()
rep = options.rep
recursive= options.recursive
readme= options.readme
topahtml="""
"""
topbhtml="""
"""
def main():
try:
global directorypath
directorypath=rep
if(recursive):
rec="";
else:
rec=" -maxdepth 0";
theindex=open(directorypath+"index.html","w")
print "index.html created in "+directorypath
theindex.write(topahtml)
theindex.write(directorypath)
theindex.write(topbhtml)
theindex.write("Plots in "+directorypath+" ")
if(readme):
theindex.write(""" Folder details
""")
theindex.writelines([l for l in open(directorypath+"README").readlines()])
theindex.write(" ")
theindex.write(middleahtml)
myoutls=commands.getoutput("find "+directorypath+rec+" -type d -print").split("\n")
print "Including the following folder(s):"
for i in sorted(myoutls):
print "\t"+i
i=i.replace(" ","\ ")
theindex.write("in ./"+re.sub(directorypath, "",i)+" \n ")
#print "ls "+i+"/*.png "+i+"/*.txt " +i+"/*.[jJ][pP][gG]"
#tmp=commands.getoutput("ls "+i+"/*.png "+i+"/*.[jJ][pP][gG] "+i+"/*.gif" ).split("\n")
tmp=commands.getoutput("ls "+i+"/*.png "+i+"/*.gif "+i+"/*.txt " +i+"/*.[jJ][pP][gG]" ).split("\n")
tmpsize=len(tmp)
#print tmp
# if((tmp[0].find("No such file or directory"))==-1):
for ii in range(0,tmpsize):
if((tmp[ii].find("No such file or directory"))==-1):
# theindex.write(""+re.sub(r'.*/', "",tmp[ii])+" ")
theindex.write(""+re.sub(r'.*/', "",tmp[ii]) +" ")
#print ""+re.sub(r'.*/', "",tmp[ii])+" "
theindex.write(" \n")
theindex.write(middlebhtml);
for i in sorted(myoutls):
theindex.write("in ./"+re.sub(directorypath, "",i)+" \n")
i=i.replace(" ","\ ")
tmp=commands.getoutput("ls "+i+"/*.png "+i+"/*.gif " +i+"/*.[jJ][pP][gG]" ).split("\n")
tmpsize=len(tmp)
#print tmp
for j in range(0,tmpsize):
if((tmp[j].find("No such file or directory"))==-1):
hrefbase="./"
theindex.write(" "+re.sub(r'.*/', "",tmp[j]) +" "+re.sub(directorypath, "",tmp[j])+" \n")
theindex.write(bottomhtml)
theindex.close()
except:
parser.print_help()
if __name__ == '__main__':
main()