/
irclogs.py
157 lines (121 loc) · 4.65 KB
/
irclogs.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
#!/usr/bin/env python2
# Processes the Supybot ChannelLogger's log directory and generates
# html pages for the IRC logs
# Assumes that the dir / log file names do not have a leading '#'
import re
import os
import glob
from os import path
from datetime import date
# ---------------------------------------------------------------------
# Directory where ChannelLogger stores the raw IRC logs
pathSource = './source'
# Web server directory from which the html pages are served
pathTarget = './target'
# Regex for IRC logs archives to process
strRegexChannel = '(?:mantis)'
# ---------------------------------------------------------------------
def runLogs2html (channel, dirName):
""" Runs the Log2html script for specified directory if the most
recent log file does not have a corresponding and up-to-date
html file"""
# Get most recent log file
pathRecentLog = max(glob.glob(path.join(dirName, '*.log')))
pathRecentHtml = pathRecentLog + '.html'
# Check that html file corresponding to most recent log
# exists and is actually more recent than the log file
if (
os.path.exists(pathRecentHtml) and
os.path.getmtime(pathRecentLog) <= os.path.getmtime(pathRecentHtml)
):
print "up-to-date html exists for newest log file %s" % \
path.basename(pathRecentLog)
else:
msg = "IRC logs of #%s" % channel
cmd = "logs2html --title='%s' --prefix='%s for ' %s" % (msg, msg, dirName)
print "generating html - %s" % cmd
os.system(cmd)
def convertLogs (pathSource):
""" Process source path, convert all logs to html """
# Building a list of channels to generate index page later
channels = dict()
# The directories in pathSource are our logged channels
for channel in os.walk(pathSource).next()[1]:
pathSrcChan = path.join(pathSource, channel)
channels[channel] = set()
# Skip if channel not matching spec
regexChannel = re.compile(strRegexChannel)
if not regexChannel.match(channel):
continue
print "Processing channel #%s " % channel
# Check for presence of subdirectories
dirList = frozenset(os.walk(pathSrcChan).next()[1])
if dirList:
# Found some (directories.timestamp is True), process them
for subdir in dirList:
channels[channel].add(subdir)
print "\t%s:" % subdir,
runLogs2html(channel, path.join(pathSrcChan, subdir))
else:
# Empty dirList = no dir rotation setup, all files are here
print "\t",
runLogs2html(channel, pathSrcChan)
print "html files generation completed"
print
return channels
def wwwUpdate (pathSource, pathTarget):
""" Copies the generated html pages to the web server """
rsync = "rsync -av --delete --exclude=*.log %s/ %s" % (pathSource, pathTarget)
print "Running rsync"
print
os.system(rsync)
print
def createIndex (dir, channels):
# Create index file
fileName = os.path.join(dir, 'test.html')
print "Generating index page %s " % fileName
try:
fileIndex = open(fileName, 'w');
except IOError as e:
raise Error("cannot open %s for writing: %s" % (fileName, e))
# Write file contents
try:
# Header
print >>fileIndex, """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>%(title)s</title>
<link rel="stylesheet" type="text/css" href="../css/mantis.css" />
</head>
<body>
<div>
"""
# Body
for channel in channels:
if channels[channel]:
print >>fileIndex, '<div>%s' % channel
for subdir in channels[channel]:
print >>fileIndex, '<div><a href="%s">#%s</a> <a href="%s">latest log</a></div>' % \
(channel + '/' + subdir, subdir, channel + '/' + subdir + '/latest.log.html')
print >>fileIndex, '</div>'
else:
print >>fileIndex, '<a href="%s">#%s</a> <a href="%s">latest log</a>' % \
(channel, channel, channel + '/latest.log.html')
# Footer
print >>fileIndex, """\
</div>
</body>
</html>
"""
finally:
fileIndex.close()
print "Index file creation complete"
# ---------------------------------------------------------------------
pathSource = os.path.abspath(pathSource)
pathTarget = os.path.abspath(pathTarget)
channels = convertLogs(pathSource)
wwwUpdate(pathSource, pathTarget)
#createIndex(pathTarget, channels)