-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathautoindex.py
67 lines (55 loc) · 2.25 KB
/
autoindex.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sys
from datetime import datetime, timezone
ROOT_DIR = "mirror"
PREFIX = ""
if len(sys.argv) > 1:
PREFIX = sys.argv[1] + "/"
HTML_HEADER = """<html><head><title>Index of /{relative_path}</title></head>
<body bgcolor="white">
<h1>Index of /{relative_path}</h1><hr><pre><a href="../">../</a>
"""
HTML_FOOTER = """</pre><hr>
</body></html>
"""
def limit_filename_length(filename, max_length=50):
if len(filename) > max_length:
return filename[:max_length - 3] + '..>'
return filename
def generate_directory_listing_html(current_dir, relative_path, dirs, files):
html = HTML_HEADER.format(relative_path=PREFIX + relative_path)
for directory in sorted(dirs):
dir_path = os.path.join(current_dir, directory)
stat_info = os.stat(dir_path)
modified_time = datetime.fromtimestamp(stat_info.st_mtime,
tz=timezone.utc).strftime("%d-%b-%Y %H:%M")
label = limit_filename_length(directory + '/')
html += f"<a href=\"{directory}/\">{label}</a>" + \
f"{' ' * (51 - len(label))}{modified_time}{' ' * 20}-\n"
for file_name in sorted(files):
if file_name == "index.html":
continue
file_path = os.path.join(current_dir, file_name)
stat_info = os.stat(file_path)
modified_time = datetime.fromtimestamp(stat_info.st_mtime,
tz=timezone.utc).strftime("%d-%b-%Y %H:%M")
file_size = f"{stat_info.st_size:21}"
label = limit_filename_length(file_name)
html += f"<a href=\"{file_name}\">{label}</a>" + \
f"{' ' * (51 - len(label))}{modified_time}{file_size}\n"
html += HTML_FOOTER
return html
def generate_index_files(root_dir):
for current_dir, subdirs, files in os.walk(root_dir):
relative_path = os.path.relpath(current_dir, root_dir)
if relative_path == ".":
relative_path = ""
else:
relative_path += "/"
html_content = generate_directory_listing_html(current_dir,
relative_path, sorted(subdirs), sorted(files))
index_path = os.path.join(current_dir, "index.html")
with open(index_path, "w") as f:
f.write(html_content)
print(f"Generated index.html for: {current_dir}")
generate_index_files(ROOT_DIR)