-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-indexes.py
More file actions
executable file
·91 lines (78 loc) · 2.66 KB
/
Copy pathgenerate-indexes.py
File metadata and controls
executable file
·91 lines (78 loc) · 2.66 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3
import os
import html
from datetime import datetime
CSS = """
body { font-family: monospace; padding: 20px; }
h1 { border-bottom: 1px solid #ccc; padding-bottom: 10px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 5px 10px; }
tr:hover { background-color: #f5f5f5; }
.back-link { margin-bottom: 20px; display: block; }
"""
def generate_index(path, relative_url):
items = sorted(os.listdir(path))
items = [i for i in items if not i.startswith('.') and i != 'index.html']
html_content = f"""<!DOCTYPE html>
<html>
<head>
<title>Index of {relative_url}</title>
<style>{CSS}</style>
</head>
<body>
<h1>Index of {relative_url}</h1>
{f'<a class="back-link" href="..">Parent Directory</a>' if relative_url != "/" else ""}
<table>
<thead>
<tr>
<th>Name</th>
<th>Last Modified</th>
<th>Size</th>
</tr>
</thead>
<tbody>
"""
for item in items:
full_path = os.path.join(path, item)
is_dir = os.path.isdir(full_path)
display_name = item + ('/' if is_dir else '')
stat = os.stat(full_path)
last_mod = datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
size = f"{stat.st_size:,} B" if not is_dir else "-"
html_content += f"""
<tr>
<td><a href="{item}{'/' if is_dir else ''}">{display_name}</a></td>
<td>{last_mod}</td>
<td>{size}</td>
</tr>"""
html_content += """
</tbody>
</table>
</body>
</html>
"""
tmp_path = os.path.join(path, "index.html.tmp")
final_path = os.path.join(path, "index.html")
with open(tmp_path, "w") as f:
f.write(html_content)
os.replace(tmp_path, final_path)
def walk_and_index(base_dir):
for root, dirs, files in os.walk(base_dir):
relative_url = root.replace(base_dir, "")
if not relative_url: relative_url = "/"
parts = root.split(os.sep)
if "skills" in parts:
print(f"Skipping index for {relative_url} (skills directory)...")
continue
if relative_url == "/" and os.path.exists(os.path.join(root, "index.html")):
print(f"Skipping index for root (custom index.html already exists)...")
continue
print(f"Generating index for {relative_url}...")
generate_index(root, relative_url)
if __name__ == "__main__":
import sys
target = sys.argv[1] if len(sys.argv) > 1 else "public"
if os.path.exists(target):
walk_and_index(target)
else:
print(f"Error: Directory {target} does not exist.")