2014-01-08 21:33:04 +00:00
|
|
|
# Copyright (C) 2000-2014 Bastian Kleineidam
|
2004-08-16 19:28:42 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
A failures logger.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
import os
|
2020-06-05 15:59:46 +00:00
|
|
|
|
2013-12-11 17:41:55 +00:00
|
|
|
from . import _Logger
|
2020-08-13 18:32:21 +00:00
|
|
|
from .. import log, LOG_CHECK
|
2021-12-20 19:34:58 +00:00
|
|
|
from ..configuration import get_user_data
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
|
2020-08-23 16:19:26 +00:00
|
|
|
class FailuresLogger(_Logger):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
Updates a list of failed links. If a link already on the list
|
|
|
|
|
is found to be working, it is removed. After n days
|
|
|
|
|
we only have links on the list which failed within those n days.
|
2004-08-16 19:28:42 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-08-23 16:19:26 +00:00
|
|
|
LoggerName = "failures"
|
2013-12-11 17:41:55 +00:00
|
|
|
|
|
|
|
|
LoggerArgs = {
|
2020-08-23 16:19:26 +00:00
|
|
|
"filename": os.path.join(get_user_data(), "failures"),
|
2013-12-11 17:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def __init__(self, **kwargs):
|
2022-09-02 09:20:02 +00:00
|
|
|
"""Initialize with old failures data (if found)."""
|
2020-08-23 16:19:26 +00:00
|
|
|
blacklist = os.path.join(get_user_data(), "blacklist")
|
|
|
|
|
if os.path.isfile(blacklist):
|
|
|
|
|
self.LoggerArgs["filename"] = blacklist
|
|
|
|
|
log.warn(
|
|
|
|
|
LOG_CHECK,
|
|
|
|
|
_("%(blacklist)s file is deprecated please rename to failures")
|
|
|
|
|
% {"blacklist": blacklist}
|
|
|
|
|
)
|
2013-12-11 17:41:55 +00:00
|
|
|
args = self.get_args(kwargs)
|
2020-06-03 19:06:36 +00:00
|
|
|
super().__init__(**args)
|
2005-05-06 13:05:16 +00:00
|
|
|
self.init_fileoutput(args)
|
2020-08-23 16:19:26 +00:00
|
|
|
self.failures = {}
|
2006-06-05 20:21:01 +00:00
|
|
|
if self.filename is not None and os.path.exists(self.filename):
|
2020-08-23 16:19:26 +00:00
|
|
|
self.read_failures()
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def comment(self, s, **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-07-16 07:12:44 +00:00
|
|
|
Write nothing.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2004-11-25 16:20:58 +00:00
|
|
|
pass
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_url(self, url_data):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
Add invalid url to failures, delete valid url from failures.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2014-03-08 18:35:10 +00:00
|
|
|
key = (url_data.parent_url, url_data.cache_url)
|
|
|
|
|
key = repr(key)
|
2020-08-23 16:19:26 +00:00
|
|
|
if key in self.failures:
|
2012-11-06 20:34:22 +00:00
|
|
|
if url_data.valid:
|
2020-08-23 16:19:26 +00:00
|
|
|
del self.failures[key]
|
2004-08-16 19:28:42 +00:00
|
|
|
else:
|
2020-08-23 16:19:26 +00:00
|
|
|
self.failures[key] += 1
|
2012-11-06 20:34:22 +00:00
|
|
|
else:
|
|
|
|
|
if not url_data.valid:
|
2020-08-23 16:19:26 +00:00
|
|
|
self.failures[key] = 1
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def end_output(self, **kwargs):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
Write failures file.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
self.write_failures()
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2020-08-23 16:19:26 +00:00
|
|
|
def read_failures(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
Read a previously stored failures from file fd.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2022-11-08 19:21:29 +00:00
|
|
|
with open(self.filename, encoding=self.output_encoding,
|
2020-06-05 15:59:46 +00:00
|
|
|
errors=self.codec_errors) as fd:
|
2006-06-05 20:21:01 +00:00
|
|
|
for line in fd:
|
2010-11-21 19:19:27 +00:00
|
|
|
line = line.rstrip()
|
2006-06-05 20:21:01 +00:00
|
|
|
if line.startswith('#') or not line:
|
|
|
|
|
continue
|
|
|
|
|
value, key = line.split(None, 1)
|
2020-08-13 18:32:21 +00:00
|
|
|
key = key.strip('"')
|
|
|
|
|
if not key.startswith('('):
|
|
|
|
|
log.critical(
|
|
|
|
|
LOG_CHECK,
|
2020-08-23 16:19:26 +00:00
|
|
|
_("invalid line starting with '%(linestart)s' in %(failures)s")
|
|
|
|
|
% {"linestart": line[:12], "failures": self.filename}
|
2020-08-13 18:32:21 +00:00
|
|
|
)
|
|
|
|
|
raise SystemExit(2)
|
2020-08-23 16:19:26 +00:00
|
|
|
self.failures[key] = int(value)
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2020-08-23 16:19:26 +00:00
|
|
|
def write_failures(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-08-23 16:19:26 +00:00
|
|
|
Write the failures file.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2019-04-13 19:37:39 +00:00
|
|
|
oldmask = os.umask(0o077)
|
2020-08-23 16:19:26 +00:00
|
|
|
for key, value in self.failures.items():
|
2020-04-30 19:11:59 +00:00
|
|
|
self.write("%d %s%s" % (value, repr(key), os.linesep))
|
2005-05-06 13:05:16 +00:00
|
|
|
self.close_fileoutput()
|
2004-08-16 19:28:42 +00:00
|
|
|
# restore umask
|
|
|
|
|
os.umask(oldmask)
|