import os
import sys
endings = ["~", "#", ".bak", ".toc", ".snm", ".nav", ".log", ".aux"]
unique_string = "This index was generated by index.py"
def allowed(file_name):
return not any(map(file_name.endswith, endings))
def is_listing(file_name):
return unique_string in open(file_name).read()
def write_index(in_dir):
index_name = os.path.join(in_dir, "index.html")
if os.path.exists(index_name) and (not is_listing(index_name)):
index_name = os.path.join(in_dir, "index.list.html")
index = open(index_name, 'w')
print "Generating index for", in_dir
index.write("<html><head> <!-- %s --> <title>%s</title></head><body><ul>"
% (unique_string, in_dir))
for file_name in sorted(filter(allowed, os.listdir(in_dir))):
if os.path.isdir(file_name):
dir_index = os.path.join(file_name, "index.html")
index.write("<li><a href='%s'>%s/</a></li>" % (dir_index, file_name))
write_index(os.path.join(in_dir, file_name))
else:
index.write("<li><a href='%s'>%s</a></li>" % (file_name, file_name))
index.write("</ul></body></html>")
if __name__ == "__main__":
write_index(sys.argv[1])