Skip to content

Commit

Permalink
Check for up-to-date HTML files in the target directory
Browse files Browse the repository at this point in the history
Fixes #29.
  • Loading branch information
mgedmin committed Jul 7, 2021
1 parent fcfa2f4 commit 83a83be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Changelog

- Drop support for Python 3.5.

- Fix a bug where specifying --output-dir (-o) to logs2html would mistakenly
skip generating some HTML files if they existed in the source directory
(`issue 29 <https://github.com/mgedmin/irclog2html/issues/29>`_).


2.17.2 (2019-04-23)
-------------------
Expand Down Expand Up @@ -47,7 +51,8 @@ Changelog
in some cases.

- Add --output-dir (-o) parameter to logs2html so you can place the generated
HTML files in a directory different from the input directory.
HTML files in a directory different from the input directory (`PR #20
<https://github.com/mgedmin/irclog2html/issues/#20>`_).


2.15.3 (2016-12-08)
Expand Down
16 changes: 10 additions & 6 deletions src/irclog2html/logs2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Error(Exception):
class LogFile:
"""IRC log file."""

def __init__(self, filename):
def __init__(self, filename, output_dir=None):
self.filename = filename
basename = os.path.basename(filename)
m = DATE_REGEXP.match(basename)
Expand All @@ -64,6 +64,10 @@ def __init__(self, filename):
self.date = datetime.date(*map(int, m.groups()))
self.link = irclog2html.pick_output_filename(basename)
self.title = self.date.strftime('%Y-%m-%d (%A)')
if output_dir:
self.html_filename = os.path.join(output_dir, basename + '.html')
else:
self.html_filename = self.filename + '.html'

def __eq__(self, other):
return isinstance(other, LogFile) and other.filename == self.filename
Expand All @@ -81,14 +85,14 @@ def newfile(self):
if not hasattr(self, '_newfile'):
# Only do this once, so that self.generate() does not change
# newness
self._newfile = not os.path.exists(self.filename + ".html")
self._newfile = not os.path.exists(self.html_filename)
return self._newfile

def uptodate(self):
"""Check whether the HTML version of the log is up to date."""
log_mtime = os.stat(self.filename).st_mtime
try:
html_mtime = os.stat(self.filename + ".html").st_mtime
html_mtime = os.stat(self.html_filename).st_mtime
except OSError:
return False
return html_mtime > log_mtime
Expand All @@ -113,15 +117,15 @@ def generate(self, style, title_prefix='', prev=None, next=None,
irclog2html.main(argv)


def find_log_files(directory, pattern='*.log'):
def find_log_files(directory, pattern='*.log', output_dir=None):
"""Find all IRC log files in a given directory.
Returns a sorted list of LogFile objects (oldest first).
"""
pattern = os.path.join(directory, pattern)
# ISO 8601 dates sort the way we need them
return sorted([
LogFile(filename)
LogFile(filename, output_dir=output_dir)
for filename in glob.glob(pattern) + glob.glob(pattern + '.gz')
], key=attrgetter('filename'))

Expand Down Expand Up @@ -243,7 +247,7 @@ def process(dir, options):
os.makedirs(out_dir)
except OSError as e:
raise Error("Failed to create directory %s: %s" % (out_dir, e))
logfiles = find_log_files(dir, options.pattern)
logfiles = find_log_files(dir, options.pattern, options.output_dir)
logfiles.reverse() # newest first
for n, logfile in enumerate(logfiles):
if n > 0:
Expand Down
7 changes: 6 additions & 1 deletion src/irclog2html/tests/test_logs2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ def test_process_handles_write_errors(self):
os.chmod(self.filename('index.html'), 0o644)

def test_process_with_output_dir(self):
self.create('somechannel-20130316.log')
self.create('somechannel-20130316.log', mtime=-200)
self.create('somechannel-20130316.log.html', mtime=-250) # outdated
self.create('somechannel-20130317.log', mtime=-100)
self.create('somechannel-20130317.log.html', mtime=-50) # up to date
options = optparse.Values(dict(searchbox=True, dircproxy=True,
pattern='*.log', force=False,
prefix='IRC logs for ',
Expand All @@ -209,6 +212,8 @@ def test_process_with_output_dir(self):
self.assertTrue(os.path.exists(self.filename('new/out/dir/irclog.css')))
self.assertTrue(os.path.exists(
self.filename('new/out/dir/somechannel-20130316.log.html')))
self.assertTrue(os.path.exists(
self.filename('new/out/dir/somechannel-20130317.log.html')))
if hasattr(os, 'symlink'):
self.assertTrue(os.path.exists(
self.filename('new/out/dir/latest.log.html')))
Expand Down

0 comments on commit 83a83be

Please sign in to comment.