-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateHtmlOverview.py
executable file
·258 lines (253 loc) · 11.2 KB
/
createHtmlOverview.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/env python
#
# A simple script to create an html page with an overview of the directory tree.
# This script walks recursively the directory tree and for each sub-directory
# it creates an 'index.html' page which will contain:
# - an overview of the subdirectories
# - a dump of the text files
# - a table (currently with two items per row) with all the images scaled to
# some predefined size (currently 400px X 300px)
#
# davide.gerbaudo@gmail.com, Jan2011
#
# -----------
# | License | GPLv3 (http://www.gnu.org/licenses/gpl.html)
# -----------
#
# Todo:
# - add an option not to overwrite the current index.html, or to skip
# the directories that already have an index.html file.
# - pass the hardcoded parameters on the command line.
# - indent the html based on the directory depth
import os,sys,commands,operator,re
class Directory:
"""A directory object which knows its name, parent, and has a list of daughters.
The directory might also have a short description associated with it,
saying shortly what its contets are."""
def __init__(self,name,parent="None",shortDescriptionFile="shortdescription.txt",verbose=False):
"Constructor"
self.name_ = name
if not parent=="None":
self.parent_ = parent
self.verbose_ = verbose
self.shortDescFile_ = shortDescriptionFile
self.shortdescription_ = ""
self.readDescFile(self.getBasePath()+"/"+self.shortDescFile_)
self.populateDaughterList()
def depth(self):
"depth in the directory tree"
if not hasattr(self,"parent_"): return 0
else: return self.parent_.depth()+1
def readDescFile(self, filename):
"Read the file containing the one-line description of the directory's contents"
if os.path.isfile(filename):
self.description_ = open(filename).read()
if self.verbose_: print self.description_
elif self.verbose_:
print "Directory '%s' does not have a description file '%s'" % (self.name_, self.shortDescFile_)
def populateDaughterList(self):
"Populate the list of daughters directories"
self.daughters_ = []
basepath = self.getBasePath()
names = os.listdir(basepath)
skipNames = ["root-files","pdf-files","C-files"]
for name in names:
if os.path.isdir(basepath+'/'+name):
if name in skipNames:
print "Skipping " + name
continue
self.daughters_.append(Directory(name,self,self.shortDescFile_,self.verbose_))
self.daughters_.sort(key=operator.attrgetter('name_'))
if self.verbose_:
print "%s contains %d directories" % (self.name_,len(self.daughters_))
def getBasePath(self):
"Return a basepath by asking the parents where we are"
if not hasattr(self,"parent_"):
return "./"
else:
return self.parent_.getBasePath()+"/"+self.name_
def htmlSubDirectoryTree(self,relativeTo=None):
"""Print an html unnumbered list showing the subdirectories of this directory.
If relativeTo=="", the links will be relative to this directory.
Otherwise they will be relative to the provided basepath."""
if self.verbose_:
print "calling %s.htmlSubDirectoryTree " % (self.name_)
text = ""
# if this dir does not have a parent, then it needs to open an 'ul' on its own
if not hasattr(self,"parent_"): text += "<ul>\n"
if hasattr(self, "daughters_") and len(self.daughters_):
text += "<ul>\n"
if self.verbose_:
print "%s.htmlSubDirectoryTree(relativeTo=%s)" % (self.name_, relativeTo)
for daughter in self.daughters_:
text += "<li>\n"
target=""
label=""
if not relativeTo:
target="%s/index.html"%daughter.name_
label=daughter.name_
else:
target="%s/%s/index.html"%(relativeTo,daughter.name_)
label=daughter.name_
## UGLY WAY TO FIX THE PROBLEM WITH THE DAUGHTER LINKS
if not ".//"in target:
tmp=target.split("/")
if len(tmp)>2:
target=target.split("/")[1:len(tmp)]
target="/".join(target)
########################
if self.verbose_:print "adding '%s' --> '%s'" % (label,target)
text += "<a href=\"%s\">%s</a>\n" % (target,label)
if hasattr(daughter,"description_") and len(daughter.description_):
text += daughter.description_
text += "</li>\n"
# when we are done with the 1st gen daughters, do the same recursively
relPath=""
if not hasattr(self,"parent_"):
relPath="%s/%s"%(self.name_,daughter.name_)
elif not relativeTo:
relPath="%s/%s"%(self.name_,daughter.name_)
else:
relPath="%s/%s"%(relativeTo,daughter.name_)
if self.verbose_:
print "calling htmlSubDirectoryTree for daughter '%s' with relativeTo='%s'"\
%\
(daughter.name_, relPath)
text += daughter.htmlSubDirectoryTree(relPath)
text += "</ul>\n"
# if this dir does not have a parent, then it needs to close an 'ul' on its own
if not hasattr(self,"parent_"): text += "</ul>\n"
return text
def htmlHeader(self):
"Print the html header"
return '\n'.join(['<html>',
'<head>',
' <title>Tony</title>',
'<!-- Generated with createHtmlOverview.py -->',
'</head>',
'<p>',
"Tony Tong's plot/slides sharing space; baojia.tong@cern.ch; ATLAS only",
'</p>'
'<body>'])
def htmlFooter(self):
"Print the html footer"
return '\n'.join(['</body>',
'</html>'])
def findImages(self):
"Find the images in this directory"
imgExtensions = ["png", "jpg", "gif", "pdf"]
imgFiles = []
for ext in imgExtensions:
files = os.listdir(self.getBasePath())
files.sort()
for file in files:
if file.count(ext) and not file.count("No such file"):
imgFiles.append(file)
return imgFiles
def findLinkFiles(self):
"find the txt files in this directory"
txtExtensions = ["tex"]
txtFiles = []
files = os.listdir(self.getBasePath())
for ext in txtExtensions:
for file in files:
if file.count(ext) \
and not file.count("No such file") \
and not file == self.shortDescFile_:
txtFiles.append(file)
return txtFiles
def findTxtFiles(self):
"find the txt files in this directory"
txtExtensions = ["txt"]
txtFiles = []
files = os.listdir(self.getBasePath())
for ext in txtExtensions:
for file in sorted(files):
if file.count(ext) \
and not file.count("No such file") \
and not file == self.shortDescFile_:
txtFiles.append(os.path.join(self.getBasePath(),file))
return txtFiles
def getImgTable(self, nColumns = 2):
"Provide an html table showing the images of this directory"
thumbWidth=400
thumbHeight=400
#thumbHeight=400
imgFiles = self.findImages()
if self.verbose_:
print "%s %d images" % (self.name_,len(imgFiles))
if not len(imgFiles): return ""
htmlBody = ""
htmlBody += "<table cellspacing=\"10\">\n"
htmlBody += "<tr>\n"
for imgFile in imgFiles:
htmlBody += "<td>\n"
htmlBody += "<a href=\"%s\">" % imgFile
htmlBody += "<img src=\"%s\" width=\"%d\" height=\"%d\">" % (imgFile, thumbWidth, thumbHeight)
htmlBody += "</img>"
htmlBody += "<br>\n"
htmlBody += "%s" % imgFile
htmlBody += "</a>\n"
if ".png" in imgFile:
imgFileEPS = re.sub(".png", ".eps", imgFile)
imgFilePDF = re.sub(".png", ".pdf", imgFile)
#htmlBody += " / <a href=\"%s\">.eps</a>\n" % imgFileEPS
#htmlBody += " / <a href=\"%s\">.pdf</a>\n" % imgFilePDF
htmlBody += "</td>\n"
if (imgFiles.index(imgFile)+1) % nColumns == 0:
htmlBody += "</tr>\n<tr>\n"
htmlBody += "</tr>\n"
htmlBody += "</table>\n"
return htmlBody
def getLinkFilesDump(self):
"Get a preformatted dump of the txt files in this directory"
txtFiles = self.findLinkFiles()
text = ""
if len(txtFiles)>0:
for txtFile in txtFiles:
text += "<a href=\" %s \">" % txtFile
text += txtFile
text += "</a><br>"
return text
def getTxtFilesDump(self):
"Get a preformatted dump of the txt files in this directory"
txtFiles = self.findTxtFiles()
text = ""
if len(txtFiles)>0:
for txtFile in txtFiles:
text += "<pre>"
text += commands.getoutput("cat %s" % txtFile)
text += "</pre>"
return text
def createHtmlIndex(self, outfile="index.html", nColumns=2):
"""This function creates an index.html page with (in this order):
- a list of the subdirectories
- a dump of the txt files in this directory
- a table with all the images
This function is also called recursively for all subdirectories"""
try:
file = open(self.getBasePath()+"/"+outfile, 'w')
except IOError:
print "WARNING: No permission to write ",self.getBasePath()+"/"+outfile
else:
file.write(self.htmlHeader()+"\n")
file.write(self.htmlSubDirectoryTree(self.name_)+"\n")
file.write(self.getTxtFilesDump()+"\n")
# file.write(self.getLinkFilesDump()+"\n")
file.write(self.getImgTable(nColumns)+"\n")
file.write(self.htmlFooter()+"\n")
file.close()
if hasattr(self,"daughters_"):
for daughter in self.daughters_:
daughter.createHtmlIndex(nColumns = nColumns)
if __name__ == '__main__':
topDirPath = ""
if len(sys.argv) == 2:
topDirPath = sys.argv[1]
nColumns = 3
if len(sys.argv) == 3:
nColumns = int(sys.argv[2])
#print topDirPath
topDir = Directory(topDirPath,verbose=False)
#print topDir
topDir.createHtmlIndex(nColumns = nColumns)