109 lines
3.5 KiB
Python
Executable File
109 lines
3.5 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
''' A script that creates index.html indexes for a folder.
|
|
'''
|
|
|
|
import os,sys,time
|
|
import urllib
|
|
INDEXFILE='index.html'
|
|
|
|
def setup():
|
|
''' Setup the command line options '''
|
|
from argparse import ArgumentParser
|
|
parser=ArgumentParser()
|
|
parser.add_argument("-f",action="store_true",dest="overwrite",default=False,
|
|
help="Overwrite existing "+INDEXFILE)
|
|
parser.add_argument("-H",action="store_true",dest="hidden",default=False,
|
|
help="Show hidden files")
|
|
parser.add_argument("-t",type=str,dest="title",default=None,
|
|
help="Name for the title (Default: Folder name)")
|
|
parser.add_argument("-p",action="store_false",dest="parent",default=True,
|
|
help="Do no print .. link for parent folder.")
|
|
parser.add_argument("startpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
|
|
help="Root path of the index")
|
|
options=parser.parse_args()
|
|
options.startpath=os.path.abspath(options.startpath)
|
|
if options.title==None:
|
|
options.title=os.path.basename(options.startpath)
|
|
return options
|
|
|
|
def generate_index(opts):
|
|
for path,dirs,files in os.walk(opts.startpath):
|
|
if INDEXFILE in files:
|
|
if not opts.overwrite:
|
|
print(INDEXFILE+" exists")
|
|
sys.exit(1)
|
|
files = [ f for f in files if f != INDEXFILE]
|
|
if not opts.hidden:
|
|
files = [ f for f in files if not f.startswith(".")]
|
|
dirs = [ d for d in dirs if not d.startswith(".")]
|
|
f=open(os.path.join(path,INDEXFILE),'wt')
|
|
dirs.sort()
|
|
files.sort()
|
|
f.write(get_header(opts.title))
|
|
if opts.parent:
|
|
f.write(get_pathlink(path,'..'))
|
|
for di in dirs:
|
|
f.write(get_pathlink(path,di))
|
|
for fi in files:
|
|
f.write(get_filelink(path,fi))
|
|
f.write(get_footer())
|
|
f.close()
|
|
return
|
|
|
|
def get_filelink(path,fname):
|
|
fsize=os.path.getsize(os.path.join(path,fname))
|
|
fsstr=sizeof(fsize)
|
|
fdate=time.localtime(os.path.getmtime(os.path.join(path,fname)))
|
|
fdstr=time.strftime("%Y/%m/%d %H:%M:%S",fdate)
|
|
return '<tr><td><a href="'+urllib.quote(fname)+'">'+fname+'</a><td class="right">'+fsstr+'</td><td class="right">'+fdstr+'</td></tr>\n'
|
|
|
|
def get_pathlink(path,dname):
|
|
fdate=time.localtime(os.path.getmtime(os.path.join(path,dname)))
|
|
fdstr=time.strftime("%Y/%m/%d %H:%M:%S",fdate)
|
|
return '<tr><td><a href="'+urllib.quote(dname)+'">'+dname+'</a><td class="right">[DIR]</td><td class="right">'+fdstr+'</td></tr>\n'
|
|
|
|
def get_header(title):
|
|
header='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<html>
|
|
<head>
|
|
<title>Index of '''+title+'''</title>
|
|
<style>
|
|
body {
|
|
font-family: monospace;
|
|
}
|
|
table {
|
|
background-color: gray;
|
|
}
|
|
td, th {
|
|
text-align: left;
|
|
background-color: lightgray;
|
|
padding: 0.5ex;
|
|
}
|
|
.right {
|
|
text-align: right;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Index of '''+title+'''</h1>
|
|
<table><tr><th>Name</th><th class="right">Size</th><th class="right">Modified</th></tr>
|
|
'''
|
|
return header
|
|
|
|
def get_footer():
|
|
footer='''</table>
|
|
</body></html>'''
|
|
return footer
|
|
|
|
def sizeof(num):
|
|
for x in [' B','KB','MB','GB','TB']:
|
|
if num < 1024.0:
|
|
if x==' B':
|
|
return "%d %s" % (num, x)
|
|
return "%3.1f %s" % (num, x)
|
|
num /= 1024.0
|
|
|
|
opts=setup()
|
|
generate_index(opts)
|