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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#!/usr/bin/python
"""check HTML pages for broken links"""
# Copyright (C) 2000,2001 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., 675 Mass Ave, Cambridge, MA 02139, USA.
# imports and checks
import sys
if sys.version[:5] < "2.0":
raise SystemExit, "This program requires Python 2.0 or later."
from linkcheck import timeoutsocket
import getopt, re, os, urlparse, linkcheck
from linkcheck import _,StringUtil,timeoutsocket
Usage = _("""USAGE\tlinkchecker [options] file-or-url...
OPTIONS
For single-letter option arguments the space is not a necessity. So
'-o colored' is the same as '-ocolored'.
-a, --anchors
Check anchor references. Default is don't check anchors.
-d, --denyallow
Swap checking order to extern/intern. Default checking order
is intern/extern.
-D, --debug
Print additional debugging information.
-e regex, --extern=regex
Assume urls that match the given expression as extern.
Only intern HTML links are checked recursively.
-f file, --config=file
Use file as configuration file. LinkChecker first searches
~/.linkcheckerrc and then /etc/linkcheckerrc
(under Windows <path-to-program>\\linkcheckerrc).
-F type[/filename], --file-output=type[/filename]
Same as -o, but write to a file linkchecker-out.<type>
or <filename> if specified. If the file already exists, it
is overwritten. You can specify this option more than once.
There is no file output for the blacklist logger. Default is
no file output.
-i regex, --intern=regex
Assume URLs that match the given expression as intern.
LinkChecker descends recursively only to intern URLs, not to extern.
-h, --help
Help me! Print usage information for this program.
-N server, --nntp-server=server
Specify an NNTP server for 'news:...' links. Default is the
environment variable NNTP_SERVER. If no host is given,
only the syntax of the link is checked.
-o type, --output=type
Specify output type as %s.
Default type is text.
-p pwd, --password=pwd
Try password pwd for HTML and FTP authorization.
Default password is 'joe@'. See also -u.
-P secs, --pause=secs
Pause <secs> seconds between each url check. This option
implies -t0.
Default is no pause between requests.
-q, --quiet
Quiet operation. This is only useful with -F.
-r depth, --recursion-level=depth
Check recursively all links up to given depth (depth >= 0).
Default depth is 1.
-R, --robots-txt
Obey the robots exclusion standard.
-s, --strict
Check only syntax of extern links, do not try to connect to them.
-t num, --threads=num
Generate no more than num threads. Default number of threads is 5.
To disable threading specify a non-positive number.
--timeout=secs
Set the timeout for TCP connection attempts in seconds. The default
timeout is system dependant.
-u name, --user=name
Try username name for HTML and FTP authorization.
Default is 'anonymous'. See also -p.
-V, --version
Print version and exit.
-v, --verbose
Log all checked URLs (implies -w). Default is to log only invalid
URLs.
-w, --warnings
Log warnings.
-W regex, --warning-regex=regex
Define a regular expression which prints a warning if it matches
any content of the checked link.
This applies of course only to pages which are valid, so we can
get their content.
Use this to check for pages that contain some form of error
message, for example 'This page has moved' or 'Oracle
Application Server error'.
This option implies -w.\n") % linkcheck.Config.LoggerKeys
""")
Notes = _("""NOTES
o LinkChecker assumes an http:// resp. ftp:// link when a commandline URL
starts with 'www.' resp. 'ftp.'
You can also give local files as arguments.
o If you have your system configured to automatically establish a
connection to the internet (e.g. with diald), it will connect when
checking links not pointing to your local host.
Use the -s and -i options to prevent this.
o Javascript links are currently ignored.
o If your platform does not support threading, LinkChecker uses -t0.
o You can supply multiple user/password pairs in a configuration file.
o Cookies are not accepted by LinkChecker.
o To use proxies set $http_proxy, $https_proxy on Unix or Windows.
On a Mac use the Internet Config.
o When checking 'news:' links the given NNTP host doesn't need to be the
same as the host of the user browsing your pages!
""")
Examples = _("""EXAMPLES
o linkchecker -v -ohtml -r2 -s -itreasure.calvinsplayground.de \\
http://treasure.calvinsplayground.de/~calvin/ > sample.html
o Local files and syntactic sugar on the command line:
linkchecker c:\\temp\\test.html
linkchecker ../bla.html
linkchecker www.myhomepage.de
linkchecker -r0 ftp.linux.org
""")
def printVersion():
print linkcheck.Config.AppInfo
sys.exit(0)
def printHelp():
if os.name!='posix':
StringUtil.paginate(Usage+"\n"+Notes+"\n"+Examples)
else:
print Usage
print Notes
print Examples
sys.exit(0)
def printUsage(msg):
sys.stderr.write(_("Error: %s\n") % msg)
sys.stderr.write(_("Execute 'linkchecker -h' for help\n"))
sys.exit(1)
# Read command line arguments
try:
# Note: cut out the name of the script
options, args = getopt.getopt(sys.argv[1:],
"adDe:f:F:hi:N:o:p:P:qr:Rst:u:VvwW:", # short options
["anchors", # long options
"config=",
"debug",
"extern=",
"file-output=",
"nntp-server=",
"help",
"intern=",
"denyallow",
"output=",
"password=",
"pause=",
"quiet",
"recursion-level=",
"wischiwaschi",
"robots-txt",
"strict",
"threads=",
"timeout=",
"user=",
"version",
"verbose",
"warnings",
"warning-regex="])
except getopt.error:
type, value = sys.exc_info()[:2]
printUsage(value)
# apply configuration
config = linkcheck.Config.Configuration()
configfiles = []
for opt,arg in options:
if opt=="-f" or opt=="--config":
configfiles.append(arg)
config.read(configfiles)
# apply options and arguments
_user = "anonymous"
_password = "guest@"
constructauth = 0
for opt,arg in options:
if opt=="-a" or opt=="--anchors":
config["anchors"] = 1
elif opt=="-D" or opt=="--debug":
linkcheck.Config.DebugLevel += 1
config.disableThreading()
elif opt=="-e" or opt=="--extern":
config["externlinks"].append((re.compile(arg), 0))
elif opt=="-h" or opt=="--help":
printHelp()
elif opt=="-o" or opt=="--output":
if linkcheck.Config.Loggers.has_key(arg):
config['log'] = config.newLogger(arg)
else:
printUsage((_("Illegal argument '%s' for option ") % arg) +\
"'-o, --output'")
elif opt=="-F" or opt=="--file-output":
ns = {'fileoutput':1}
try:
type, ns['filename'] = arg.split('/', 1)
if not ns['filename']: raise ValueError
except ValueError: type = arg
if linkcheck.Config.Loggers.has_key(type) and type != "blacklist":
config['fileoutput'].append(config.newLogger(type, ns))
else:
printUsage((_("Illegal argument '%s' for option ") % arg) +\
"'-F, --file-output'")
elif opt=="-i" or opt=="--intern":
config["internlinks"].append(re.compile(arg))
elif opt=="-l" or opt=="--denyallow":
config["denyallow"] = 1
elif opt=="-N" or opt=="--nntp-server":
config["nntpserver"] = arg
elif opt=="-p" or opt=="--password":
_password = arg
constructauth = 1
elif opt=="-P" or opt=="--pause":
if int(arg) >= 0:
config["wait"] = int(arg)
else:
printUsage((_("Illegal argument '%s' for option ") % arg) +
"'-P, --pause'")
elif opt=="-q" or opt=="--quiet":
config["quiet"] = 1
elif opt=="-r" or opt=="--recursion-level":
if int(arg) >= 0:
config["recursionlevel"] = int(arg)
else:
printUsage((_("Illegal argument '%s' for option ") % arg) +
"'-r, --recursion-level'")
elif opt=="-R" or opt=="--robots-txt":
config["robotstxt"] = 1
elif opt=="-s" or opt=="--strict":
config["strict"] = 1
elif opt=="-t" or opt=="--threads":
num = int(arg)
if config["threads"] and not linkcheck.Config.DebugLevel:
if num>1:
config.enableThreading(num)
else:
config.disableThreading()
elif opt=="--timeout":
timeoutsocket.setDefaultSocketTimeout(int(arg))
elif opt=="-u" or opt=="--user":
_user = arg
constructauth = 1
elif opt=="-V" or opt=="--version":
printVersion()
elif opt=="-v" or opt=="--verbose":
config["verbose"] = 1
config["warnings"] = 1
elif opt=="--wischiwaschi":
from linkcheck import util1
util1.abbuzze()
sys.exit(0)
elif opt=="-w" or opt=="--warnings":
config["warnings"] = 1
elif opt=="-W" or opt=="--warning-regex":
config["warningregex"] = re.compile(arg)
config["warnings"] = 1
if constructauth:
config["authentication"].insert(0, {'pattern': re.compile(".*"),
'user': _user,
'pass': _password})
# construct the url list
# if we use blacklist mode, try to read ~/.blacklist
if config["log"].__class__ == linkcheck.Logging.BlacklistLogger and \
os.path.exists(config['log'].filename):
args = open(config['log'].filename).readlines()
if len(args)==0:
print _("warning: no files or urls given")
for url in args:
url = url.strip()
if not (":" in url):
if re.compile("^ftp\.").match(url):
url = "ftp://"+url
elif re.compile("^www\.").match(url):
url = "http://"+url
config.appendUrl(linkcheck.UrlData.GetUrlDataFrom(url, 0))
# check the urls
linkcheck.checkUrls(config)
|