-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-directory-index.py
150 lines (123 loc) · 4.13 KB
/
update-directory-index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/python
# Quick directory index creator for cheap maven repositories by Darien Hager
# Inspired by the bash script from http://chkal.blogspot.com/2010/09/maven-repositories-on-github.html
from string import Template
from time import strftime
import os, sys
# The magic token is used to guard against accidents. This script will not overwrite files that do not contain it.
# The token must not be split over two lines.
INDEX_FNAME = "index.html"
MAGIC_TOKEN = "711CD4AFDDF4F6E6E1A9986267B5FEC62DD273FE8A63E236D3351E3E846CCDE2"
SELF_PATH = os.path.abspath(sys.argv[0])
# Taken from http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
def formatSize(bytes):
for x in ['b','k','m','g','t']:
if bytes < 1024.0:
if x != 'b':
return "%3.1f%s" % (bytes, x)
else:
return "%3.0f%s" % (bytes, x)
bytes /= 1024.0
def buildListing(dirs,files,label):
pageTemplate = Template("""<html>
<head>
<title>Directory Listing</title>
<!-- Anti-overwrite token $tok -->
</head>
<body>
<h1>Generated listing for $label</h1>
<h2>Created $time</h2>
<table>
<tr>
<th><!--Icon--></th>
<th>Name</th>
<th>Size</th>
</tr>
$rowdata
</table>
</body>
</html>
""")
rowTemplate = Template("""
<tr>
<td>$icon</td>
<td><a href="$link">$name</a></td>
<td>$size</td>
</tr>
""")
rowFragment = ""
for d in dirs:
bname = os.path.basename(d)
rowFragment += rowTemplate.substitute(
icon="[DIR]",
name=bname + "/",
link="./"+bname+"/index.html",
size=""
)
for f in files:
bname = os.path.basename(f)
rowFragment += rowTemplate.substitute(
icon="[FILE]",
name=bname,
link="./"+bname,
size=formatSize(os.path.getsize(f))
)
html = pageTemplate.substitute(
tok=MAGIC_TOKEN,
label=label,
time=strftime("%Y-%m-%d %H:%M:%S"),
rowdata=rowFragment
)
return html
def listdir(d):
items = os.listdir(d)
dirs = []
files = []
items.sort()
for i in items:
if i.startswith("."): continue # Ignore current/parent/hidden dirs
if i == "index.html" : continue # Ignore indexes
path = os.path.join(d,i)
if os.path.abspath(path) == SELF_PATH: continue # Don't index self
if os.path.isdir(path):
dirs.append(path)
elif os.path.isfile(path):
files.append(path)
return (dirs,files)
if __name__ == "__main__":
rootPath = os.getcwd()
if(len(sys.argv)>=2):
rootPath = sys.argv[1]
if not os.path.isdir(rootPath):
print "Invalid path given: "+rootPath
exit(1)
toVisit = [rootPath]
dryRun = True;
while(len(toVisit) > 0):
current = toVisit.pop()
label = None
try:
label = os.path.relpath(current,rootPath)
except AttributeError:
label = os.path.basename(os.path.abspath(current))
(directories,files) = listdir(current)
html = buildListing(directories,files,label)
target = os.path.join(current,"index.html")
toVisit.extend(directories)
doWrite = True;
if os.path.isfile(target):
# Check anti-accident protection
doWrite = False;
fh = open(target,"r")
for line in fh:
if line.find(MAGIC_TOKEN) >=0 :
doWrite = True;
break;
fh.close()
if(doWrite):
print target
fh = open(target,"w")
fh.write(html)
fh.close()
else:
print "Cautiously refusing to overwrite "+target