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
|
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2012 Bastian Kleineidam
#
# 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.
#
# 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.
"""
Main function module for link checking.
"""
# imports and checks
import sys
if not (hasattr(sys, 'version_info') or
sys.version_info < (2, 7, 0, 'final', 0)):
raise SystemExit("This program requires Python 2.7 or later.")
import os
import re
from . import i18n
import _LinkChecker_configdata as configdata
def main_is_frozen ():
"""Return True iff running inside a py2exe-generated executable."""
return hasattr(sys, "frozen")
def module_path ():
"""Return absolute directory of system executable."""
return os.path.dirname(os.path.abspath(sys.executable))
def get_install_data ():
"""Return absolute path of LinkChecker data installation directory."""
if main_is_frozen():
return module_path()
return configdata.install_data
def get_config_dir ():
"""Return absolute path of LinkChecker example configuration."""
return os.path.join(get_install_data(), "share", "linkchecker")
# application log areas
LOG_ROOT = "linkcheck"
LOG_CMDLINE = "linkcheck.cmdline"
LOG_CHECK = "linkcheck.check"
LOG_CACHE = "linkcheck.cache"
LOG_GUI = "linkcheck.gui"
LOG_THREAD = "linkcheck.thread"
lognames = {
"cmdline": LOG_CMDLINE,
"checking": LOG_CHECK,
"cache": LOG_CACHE,
"gui": LOG_GUI,
"thread": LOG_THREAD,
"all": LOG_ROOT,
}
lognamelist = ", ".join(repr(name) for name in lognames)
# logging configuration
configdict = {
'version': 1,
'loggers': {
},
'root': {
'level': 'DEBUG',
},
}
def init_log_config():
"""Configure the application loggers."""
for applog in lognames.values():
# propagate except for root app logger 'linkcheck'
propagate = (applog != LOG_ROOT)
configdict['loggers'][applog] = dict(level='INFO', propagate=propagate)
init_log_config()
from . import log
class LinkCheckerError (StandardError):
"""Exception to be raised on linkchecker-specific check errors."""
pass
class LinkCheckerInterrupt (StandardError):
"""Used for testing."""
pass
def add_intern_pattern (url_data, config):
"""Add intern URL regex to config."""
pat = url_data.get_intern_pattern()
if pat:
log.debug(LOG_CHECK, "Add intern pattern %r", pat)
config['internlinks'].append(get_link_pat(pat))
def get_link_pat (arg, strict=False):
"""Get a link pattern matcher for intern/extern links.
Returns a compiled pattern and a negate and strict option.
@param arg: pattern from config
@type arg: string
@param strict: if pattern is to be handled strict
@type strict: bool
@return: dictionary with keys 'pattern', 'negate' and 'strict'
@rtype: dict
"""
log.debug(LOG_CHECK, "Link pattern %r strict=%s", arg, strict)
if arg.startswith('!'):
pattern = arg[1:]
negate = True
else:
pattern = arg
negate = False
return {
"pattern": re.compile(pattern),
"negate": negate,
"strict": strict,
}
def init_i18n (loc=None):
"""Initialize i18n with the configured locale dir. The environment
variable LOCPATH can also specify a locale dir.
@return: None
"""
if 'LOCPATH' in os.environ:
locdir = os.environ['LOCPATH']
else:
locdir = os.path.join(get_install_data(), 'share', 'locale')
i18n.init(configdata.name.lower(), locdir, loc=loc)
# install translated log level names
import logging
logging.addLevelName(logging.CRITICAL, _('CRITICAL'))
logging.addLevelName(logging.ERROR, _('ERROR'))
logging.addLevelName(logging.WARN, _('WARN'))
logging.addLevelName(logging.WARNING, _('WARNING'))
logging.addLevelName(logging.INFO, _('INFO'))
logging.addLevelName(logging.DEBUG, _('DEBUG'))
logging.addLevelName(logging.NOTSET, _('NOTSET'))
# initialize i18n, puts _() and _n() function into global namespace
init_i18n()
def drop_privileges ():
"""Make sure to drop root privileges on POSIX systems."""
if os.name != 'posix':
return
if os.geteuid() == 0:
log.warn(LOG_CHECK, _("Running as root user; "
"dropping privileges by changing user to nobody."))
import pwd
os.seteuid(pwd.getpwnam('nobody')[3])
def find_third_party_modules ():
"""Find third party modules and add them to the python path."""
parent = os.path.dirname(os.path.dirname(__file__))
third_party = os.path.join(parent, "third_party")
if os.path.isdir(third_party):
sys.path.append(os.path.join(third_party, "dnspython"))
find_third_party_modules()
|