-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild_index.py
More file actions
executable file
·44 lines (32 loc) · 914 Bytes
/
build_index.py
File metadata and controls
executable file
·44 lines (32 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import sys
import os
import urllib.parse
import html
def create_index(dir_):
entries = sorted(os.listdir(dir_))
links = []
for e in entries:
if e == "index.html":
continue
elif e.startswith("."):
continue
if os.path.isdir(os.path.join(dir_, e)):
e += "/"
links.append(
"<a href='%s'>%s</a><br>" % (urllib.parse.quote(e), html.escape(e)))
with open(os.path.join(dir_, "index.html"), "w", encoding="utf-8") as h:
h.write("""<!DOCTYPE html>
<html>
<body>
%s
</body>
</html>""" % ("".join(links)))
def main(argv):
def is_hidden(path):
return any(
(p.startswith(".") and p != "." for p in path.split(os.sep)))
for dir_ in [l[0] for l in os.walk(argv[1]) if not is_hidden(l[0])]:
create_index(dir_)
if __name__ == "__main__":
main(sys.argv)