#!/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("-t", action="store_true", dest="tags", help ="add tags by splitting filenames on '_' ") #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 tags= options.tags readme= options.readme topahtml=""" """ topbhtml="""
""" middleahtml="""
""" bottomahtml="""
""" 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
  • \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] " +i+"/*.svg" ).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(directorypath,
    "+re.sub(r'.*/', "",tmp[j]) +"
    "+re.sub(directorypath, "",tmp[j])+"
    \n") theindex.write(bottomahtml) if(tags): theindex.write(bottoma_Ihtml) theindex.write(bottombhtml) theindex.close() except: parser.print_help() if __name__ == '__main__': main()