-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathirclogs.py
executable file
·143 lines (111 loc) · 4.02 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
#!/usr/bin/python3 -u
# 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 datetime
import re
import os
import subprocess
import sys
import glob
from os import path
# ---------------------------------------------------------------------
# Directory where ChannelLogger stores the raw IRC logs
source_dir = '/home/supybot/mantisbot/logs/ChannelLogger'
# Web server directory from which the html pages are served
target_dir = '/srv/www/irclogs'
# Regex for IRC logs archives to process
regexstr_channel = '(?:mantis)'
# ---------------------------------------------------------------------
def log(msg):
"""
Prints log message with timestamp
"""
print("{} {}".format(
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
msg
))
def check_path(p):
"""
Converts path to absolute and check that it exists
"""
p = path.abspath(p)
if not path.isdir(p):
print("ERROR: %s is not a directory" % p)
exit(1)
return p
def run_logs2html(channel, dir_name):
"""
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
path_recent_log = max(glob.glob(path.join(dir_name, '*.log')))
path_recent_html = path_recent_log + '.html'
# Check that html file corresponding to most recent log
# exists and is actually more recent than the log file
if (
path.exists(path_recent_html) and
path.getmtime(path_recent_log) <= path.getmtime(path_recent_html)):
print("up-to-date html exists for newest log file %s" % (
path.basename(path_recent_log)
))
else:
msg = "IRC logs of #%s" % channel
cmd = "logs2html --title='%s' --prefix='%s for ' %s" % (
msg, msg, dir_name
)
print("generating html - %s" % cmd)
# Execute logs2html, redirect stderr to stdout for logging purposes
subprocess.check_call(cmd, shell=True, stderr=sys.stdout.fileno())
def convert_logs(source):
"""
Process source path, convert all logs to html
"""
# Building a list of channels to generate index page later
channels = dict()
# The directories in source are our logged channels
for channel in next(os.walk(source))[1]:
path_src_channel = path.join(source, channel)
channels[channel] = set()
# Skip if channel not matching spec
regex_channel = re.compile(regexstr_channel)
if not regex_channel.match(channel):
continue
print("Processing channel #%s " % channel)
# Check for presence of subdirectories
dirlist = frozenset(next(os.walk(path_src_channel))[1])
if dirlist:
# Found some (directories.timestamp is True), process them
for subdir in dirlist:
channels[channel].add(subdir)
print("\t%s:" % subdir, end=' ')
run_logs2html(channel, path.join(path_src_channel, subdir))
else:
# Empty dirlist = no dir rotation setup, all files are here
print("\t", end=' ')
run_logs2html(channel, path_src_channel)
print("html files generation completed")
print()
return channels
def www_update(source, target):
"""
Copies the generated html pages to the web server
"""
log("Copying HTML pages to '%s'" % target)
rsync = "rsync -av --delete --exclude=*.log %s/ %s" % (
source, target
)
print(rsync)
print()
exitcode = subprocess.call(rsync, shell=True)
if exitcode != 0:
log('ERROR: rsync call failed with exit code %i' % exitcode)
# ---------------------------------------------------------------------
log('Converting logfiles')
source_dir = check_path(source_dir)
target_dir = check_path(target_dir)
convert_logs(source_dir)
www_update(source_dir, target_dir)
log('Completed\n%s' % ('-' * 80))