# # Copyright (c) 2004, 2005 Google Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # * Neither the name of Google nor the names of its contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # # The sitemap_gen.py script is written in Python 2.2 and released to # the open source community for continuous improvements under the BSD # 2.0 new license, which can be found at: # # http://www.opensource.org/licenses/bsd-license.php #
usage = \ """A simple script to automatically produce sitemaps for a webserver, in the Google Sitemap Protocol (GSP).
Usage: python sitemap_gen.py --config=config.xml [--help] [--testing]
- --config=config.xml, specifies config file location --help, displays usage message --testing, specified when user is experimenting
"""
# Please be careful that all syntax used in this file can be parsed on # Python 1.5 -- this version check is not evaluated until after the # entire file has been parsed. import sys if sys.hexversion < 0x02020000:
- print 'This script requires Python 2.2 or later.' print 'Currently run with version: %s' % sys.version sys.exit(1)
import fnmatch import glob import gzip import md5 import os import re import stat import time import types import urllib import urlparse import xml.sax
# True and False were introduced in Python2.2.2 try:
- testTrue=True del testTrue
except NameError:
- True=1 False=0
# Text encodings ENC_ASCII = 'ASCII' ENC_UTF8 = 'UTF-8' ENC_IDNA = 'IDNA' ENC_ASCII_LIST = ['ASCII', 'US-ASCII', 'US', 'IBM367', 'CP367', 'ISO646-US'
'ISO_646.IRV:1991', 'ISO-IR-6', 'ANSI_X3.4-1968', 'ANSI_X3.4-1986', 'CPASCII' ]
ENC_DEFAULT_LIST = ['ISO-8859-1', 'ISO-8859-2', 'ISO-8859-5']
# Maximum number of urls in each sitemap, before next Sitemap is created MAXURLS_PER_SITEMAP = 50000
# Suffix on a Sitemap index file SITEINDEX_SUFFIX = '_index.xml'
# Regular expressions tried for extracting URLs from access logs. ACCESSLOG_CLF_PATTERN = re.compile(
r'.+\s+"([\s]+)\s+([\s]+)\s+HTTP/\d+\.\d+"\s+200\s+.*' )
# Match patterns for lastmod attributes LASTMOD_PATTERNS = map(re.compile, [
- r'^\d\d\d\d$', r'^\d\d\d\d-\d\d$', r'^\d\d\d\d-\d\d-\d\d$', r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\dZ$', r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d[+-]\d\d:\d\d$', r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z$', r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?[+-]\d\d:\d\d$', ])
# Match patterns for changefreq attributes CHANGEFREQ_PATTERNS = [
- 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never' ]
# XML formats SITEINDEX_HEADER = \
'<?xml version="1.0" encoding="UTF-8"?>\n' \ '<sitemapindex\n' \ ' xmlns="http://www.google.com/schemas/sitemap/0.84"\n' \ ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n' \ ' xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84\n' \ ' http://www.google.com/schemas/sitemap/0.84/' \ 'siteindex.xsd">\n'
SITEINDEX_FOOTER = '</sitemapindex>\n' SITEINDEX_ENTRY = \
' <sitemap>\n' \ ' <loc>%(loc)s</loc>\n' \ ' <lastmod>%(lastmod)s</lastmod>\n' \ ' </sitemap>\n'
SITEMAP_HEADER = \
'<?xml version="1.0" encoding="UTF-8"?>\n' \ '<urlset\n' \ ' xmlns="http://www.google.com/schemas/sitemap/0.84"\n' \ ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n' \ ' xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84\n' \ ' http://www.google.com/schemas/sitemap/0.84/' \ 'sitemap.xsd">\n'
SITEMAP_FOOTER = '</urlset>\n' SITEURL_XML_PREFIX = ' <url>\n' SITEURL_XML_SUFFIX = ' </url>\n'
# Search engines to notify with the updated sitemaps # # This list is very non-obvious in what's going on. Here's the gist: # Each item in the list is a 6-tuple of items. The first 5 are "almost" # the same as the input arguments to urlparse.urlunsplit(): # 0 - schema # 1 - netloc # 2 - path # 3 - query <-- EXCEPTION: specify a query map rather than a string # 4 - fragment # Additionally, add item 5: # 5 - query attribute that should be set to the new Sitemap URL # Clear as mud, I know. NOTIFICATION_SITES = [
('http', 'www.google.com', 'webmasters/sitemaps/ping', {}, , 'sitemap') ]
class Error(Exception):
- """ Base exception class. In this module we tend not to use our own exception types for very much, but they come in very handy on XML parsing with SAX. """ pass
#end class Error
class SchemaError(Error):
- """Failure to process an XML file according to the schema we know.""" pass
#end class SchemeError
class Encoder:
- """ Manages wide-character/narrow-character conversions for just about all text that flows into or out of the script. You should always use this class for string coercion, as opposed to letting Python handle coercions automatically. Reason: Python usually assumes ASCII (7-bit) as a default narrow character encoding, which is not the kind of data we generally deal with. General high-level methodologies used in sitemap_gen: [PATHS] File system paths may be wide or narrow, depending on platform. This works fine, just be aware of it and be very careful to not mix them. That is, if you have to pass several file path arguments into a library call, make sure they are all narrow or all wide.
This class has MaybeNarrowPath() which should be called on every file system path you deal with. [URLS] URL locations are stored in Narrow form, already escaped. This has the benefit of keeping escaping and encoding as close as possible to the format we read them in. The downside is we may end up with URLs that have intermingled encodings -- the root path may be encoded in one way while the filename is encoded in another. This is obviously wrong, but it should hopefully be an issue hit by very few users. The workaround from the user level (assuming they notice) is to specify a default_encoding parameter in their config file. [OTHER] Other text, such as attributes of the URL class, configuration options, etc, are generally stored in Unicode for simplicity. """
def init(self):
- self._user = None # User-specified default encoding self._learned = [] # Learned default encodings self._widefiles = False # File system can be wide # Can the file system be Unicode? try:
- self._widefiles = os.path.supports_unicode_filenames
except AttributeError:
- try:
- self._widefiles = sys.getwindowsversion() == os.VER_PLATFORM_WIN32_NT
except AttributeError:
- pass
- encoding = sys.getfilesystemencoding() if encoding and not (encoding.upper() in ENC_ASCII_LIST):
- self._learned = [ encoding ]
except AttributeError:
- pass
- encoding = sys.getdefaultencoding() if encoding and not (encoding.upper() in ENC_ASCII_LIST):
- self._learned = [ encoding ]
- self._learned = ENC_DEFAULT_LIST
#end def init
def SetUserEncoding(self, encoding):
- self._user = encoding
#end def SetUserEncoding
def NarrowText(self, text, encoding):
- """ Narrow a piece of arbitrary text """
if type(text) != types.UnicodeType:
- return text
- try:
- result = text.encode(encoding) if not encoding in self._learned:
- self._learned.append(encoding)
except UnicodeError:
- pass
except LookupError:
- output.Warn('Unknown encoding: %s' % encoding)
- result = text.encode(encoding) if not encoding in self._learned:
- try:
- return text.encode(self._user)
except UnicodeError:
- pass
except LookupError:
- temp = self._user self._user = None output.Warn('Unknown default_encoding: %s' % temp)
- try:
- return text.encode(self._learned[0])
- del self._learned[0]
- return text.encode(ENC_UTF8)
except UnicodeError:
- pass
#end def NarrowText
def MaybeNarrowPath(self, text):
- """ Paths may be allowed to stay wide """ if self._widefiles:
- return text
return self.NarrowText(text, None)
#end def MaybeNarrowPath
def WidenText(self, text, encoding):
- """ Widen a piece of arbitrary text """
if type(text) != types.StringType:
- return text
- try:
- result = unicode(text, encoding) if not encoding in self._learned:
- self._learned.append(encoding)
except UnicodeError:
- pass
except LookupError:
- output.Warn('Unknown encoding: %s' % encoding)
- result = unicode(text, encoding) if not encoding in self._learned:
- try:
- return unicode(text, self._user)
except UnicodeError:
- pass
except LookupError:
- temp = self._user self._user = None output.Warn('Unknown default_encoding: %s' % temp)
- try:
- return unicode(text, self._learned[0])
- del self._learned[0]
- return unicode(text, ENC_UTF8)
except UnicodeError:
- pass
- output.Warn('You may need to set a default_encoding in your '
- 'configuration file.')
#end def WidenText
- self._user = None # User-specified default encoding self._learned = [] # Learned default encodings self._widefiles = False # File system can be wide # Can the file system be Unicode? try:
#end class Encoder encoder = Encoder()
class Output:
- """ Exposes logging functionality, and tracks how many errors we have thus output. Logging levels should be used as thus:
- Fatal -- extremely sparingly Error -- config errors, entire blocks of user 'intention' lost Warn -- individual URLs lost Log(,0) -- Un-suppressable text that's not an error Log(,1) -- touched files, major actions Log(,2) -- parsing notes, filtered or duplicated URLs Log(,3) -- each accepted URL
def init(self):
- self.num_errors = 0 # Count of errors self.num_warns = 0 # Count of warnings self._errors_shown = {} # Shown errors self._warns_shown = {} # Shown warnings self._verbose = 0 # Level of verbosity
#end def init def Log(self, text, level):
- """ Output a blurb of diagnostic text, if the verbose level allows it """ if text:
text = encoder.NarrowText(text, None) if self._verbose >= level:
- print text
- """ Output and count a warning. Suppress duplicate warnings. """ if text:
text = encoder.NarrowText(text, None) hash = md5.new(text).digest() if not self._warns_shown.has_key(hash):
- self._warns_shown[hash] = 1 print '[WARNING] ' + text
- self.Log('(suppressed) [WARNING] ' + text, 3)
- """ Output and count an error. Suppress duplicate errors. """ if text:
text = encoder.NarrowText(text, None) hash = md5.new(text).digest() if not self._errors_shown.has_key(hash):
- self._errors_shown[hash] = 1 print '[ERROR] ' + text
- self.Log('(suppressed) [ERROR] ' + text, 3)
- """ Output an error and terminate the program. """ if text:
text = encoder.NarrowText(text, None) print '[FATAL] ' + text
- print 'Fatal error.'
def SetVerbose(self, level):
- """ Sets the verbose level. """ try:
if type(level) != types.IntType:
- level = int(level)
if (level >= 0) and (level <= 3):
- self._verbose = level return
except ValueError:
- pass
#end def SetVerbose
#end class Output output = Output()
class URL(object):
- """ URL is a smart structure grouping together the properties we care about for a single web reference. """
slots = 'loc', 'lastmod', 'changefreq', 'priority'
def init(self):
- self.loc = None # URL -- in Narrow characters self.lastmod = None # ISO8601 timestamp of last modify self.changefreq = None # Text term for update frequency self.priority = None # Float between 0 and 1 (inc)
#end def init
def cmp(self, other):
if self.loc < other.loc:
- return -1
if self.loc > other.loc:
- return 1
#end def cmp
def TrySetAttribute(self, attribute, value):
- """ Attempt to set the attribute to the value, with a pretty try block around it. """ if attribute == 'loc':
- self.loc = self.Canonicalize(value)
- try:
- setattr(self, attribute, value)
except AttributeError:
- output.Warn('Unknown URL attribute: %s' % attribute)
#end def TrySetAttribute
def IsAbsolute(loc):
- """ Decide if the URL is absolute or not """ if not loc:
- return False
narrow = encoder.NarrowText(loc, None) (scheme, netloc, path, query, frag) = urlparse.urlsplit(narrow) if (not scheme) or (not netloc):
- return False
#end def IsAbsolute IsAbsolute = staticmethod(IsAbsolute) def Canonicalize(loc):
- """ Do encoding and canonicalization on a URL string """ if not loc:
- return loc
narrow = encoder.NarrowText(loc, None) # Escape components individually (scheme, netloc, path, query, frag) = urlparse.urlsplit(narrow) unr = '-._~'
sub = '!$&\'()*+,;=' netloc = urllib.quote(netloc, unr + sub + '%:@/[]') path = urllib.quote(path, unr + sub + '%:@/') query = urllib.quote(query, unr + sub + '%:@/?') frag = urllib.quote(frag, unr + sub + '%:@/?') # Try built-in IDNA encoding on the netloc try:
- (ignore, widenetloc, ignore, ignore, ignore) = urlparse.urlsplit(loc) for c in widenetloc:
if c >= unichr(128):
- netloc = widenetloc.encode(ENC_IDNA) netloc = urllib.quote(netloc, unr + sub + '%:@/[]') break
except UnicodeError:
- # urlsplit must have failed, based on implementation differences in the # library. There is not much we can do here, except ignore it. pass
except LookupError:
- output.Warn('An International Domain Name (IDN) is being used, but this '
- 'version of Python does not have support for IDNA encoding. ' ' (IDNA support was introduced in Python 2.3) The encoding ' 'we have used instead is wrong and will probably not yield ' 'valid URLs.')
- bad_netloc = True
if (len(item) >= 2) and (item[0] in HEXDIG) and (item[1] in HEXDIG):
- narrow = narrow + '%' + item
- narrow = narrow + '%25' + item
- output.Warn('Invalid characters in the host or domain portion of a URL: '
- + narrow)
- """ Verify the data in this URL is well-formed, and override if not. """
assert type(base_url) == types.StringType # Test (and normalize) the ref if not self.loc:
- output.Warn('Empty URL') return False
- self.loc = urlparse.urljoin(base_url, self.loc)
- output.Warn('Discarded URL for not starting with the base_url: %s' %
- self.loc)
- match = False self.lastmod = self.lastmod.upper() for pattern in LASTMOD_PATTERNS:
- match = pattern.match(self.lastmod) if match:
- break
- output.Warn('Lastmod "%s" does not appear to be in ISO8601 format on '
- 'URL: %s' % (self.lastmod, self.loc))
- match = pattern.match(self.lastmod) if match:
- match = False self.changefreq = self.changefreq.lower() for pattern in CHANGEFREQ_PATTERNS:
- if self.changefreq == pattern:
- match = True break
- output.Warn('Changefreq "%s" is not a valid change frequency on URL '
- ': %s' % (self.changefreq, self.loc))
- if self.changefreq == pattern:
- priority = -1.0 try:
- priority = float(self.priority)
except ValueError:
- pass
if (priority < 0.0) or (priority > 1.0):
- output.Warn('Priority "%s" is not a number between 0 and 1 inclusive '
- 'on URL: %s' % (self.priority, self.loc))
def MakeHash(self):
- """ Provides a uniform way of hashing URLs """ if not self.loc:
- return None
- return md5.new(self.loc[:-1]).digest()
#end def MakeHash def Log(self, prefix='URL', level=3):
- """ Dump the contents, empty or not, to the log. """ out = prefix + ':'
for attribute in self.slots:
- value = getattr(self, attribute) if not value:
value =
output.Log('%s' % encoder.NarrowText(out, None), level)
- value = getattr(self, attribute) if not value:
- """ Dump non-empty contents to the output file, in XML format. """ if not self.loc:
- return
for attribute in self.slots:
- value = getattr(self, attribute) if value:
if type(value) == types.UnicodeType:
value = encoder.NarrowText(value, None)
elif type(value) != types.StringType:
- value = str(value)
out = out + (' <%s>%s</%s>\n' % (attribute, value, attribute))
#end class URL
class Filter:
- """ A filter on the stream of URLs we find. A filter is, in essence, a wildcard applied to the stream. You can think of this as an operator that returns a tri-state when given a URL:
- True -- this URL is to be included in the sitemap None -- this URL is undecided False -- this URL is to be dropped from the sitemap
def init(self, attributes):
- self._wildcard = None # Pattern for wildcard match self._regexp = None # Pattern for regexp match self._pass = False # "Drop" filter vs. "Pass" filter
if not ValidateAttributes('FILTER', attributes,
- ('pattern', 'type', 'action')):
- return
- type = type.lower()
- action = action.lower()
- output.Error('On a filter you must specify a "pattern" to match')
- output.Error('On a filter you must specify either \'type="wildcard"\' '
'or \'type="regexp"\)
- output.Error('If you specify a filter action, it must be either '
'\'action="pass"\' or \'action="drop"\)
- self._pass = False
- self._pass = True
- self._wildcard = pattern
- try:
- self._regexp = re.compile(pattern)
- output.Error('Bad regular expression: %s' % pattern)
- output.Log('Filter: %s any URL that matches %s "%s"' %
- (action, type, pattern), 2)
#end def init def Apply(self, url):
- """ Process the URL, as above. """ if (not url) or (not url.loc):
- return None
- if fnmatch.fnmatchcase(url.loc, self._wildcard):
- return self._pass
- if self._regexp.search(url.loc):
- return self._pass
#end class Filter
class InputURL:
- """ Each Input class knows how to yield a set of URLs from a data source. This one handles a single URL, manually specified in the config file. """
def init(self, attributes):
- self._url = None # The lonely URL
if not ValidateAttributes('URL', attributes,
- ('href', 'lastmod', 'changefreq', 'priority')):
- return
- if attr == 'href':
url.TrySetAttribute('loc', attributes[attr])
url.TrySetAttribute(attr, attributes[attr])
- output.Error('Url entries must have an href attribute.') return
#end def init def ProduceURLs(self, consumer):
- """ Produces URLs from our data source, hands them in to the consumer. """ if self._url:
- consumer(self._url, True)
- self._url = None # The lonely URL
#end class InputURL
class InputURLList:
- """ Each Input class knows how to yield a set of URLs from a data source. This one handles a text file with a list of URLs """
def init(self, attributes):
- self._path = None # The file path self._encoding = None # Encoding of that file
if not ValidateAttributes('URLLIST', attributes, ('path', 'encoding')):
- return
self._path = encoder.MaybeNarrowPath(self._path) if os.path.isfile(self._path):
- output.Log('Input: From URLLIST "%s"' % self._path, 2)
- output.Error('Can not locate file: %s' % self._path) self._path = None
- output.Error('Urllist entries must have a "path" attribute.')
#end def init def ProduceURLs(self, consumer):
- """ Produces URLs from our data source, hands them in to the consumer. """ # Open the file
(frame, file) = OpenFileForRead(self._path, 'URLLIST') if not file:
- return
- linenum = linenum + 1 # Strip comments and empty lines if self._encoding:
line = encoder.WidenText(line, self._encoding)
- continue
- cols[i] = cols[i].strip()
url.TrySetAttribute('loc', cols[0]) # Extract attributes from the other columns for i in range(1,len(cols)):
- if cols[i]:
- try:
- (attr_name, attr_val) = cols[i].split('=', 1)
url.TrySetAttribute(attr_name, attr_val)
except ValueError:
- output.Warn('Line %d: Unable to parse attribute: %s' %
- (linenum, cols[i]))
- (attr_name, attr_val) = cols[i].split('=', 1)
- try:
- frame.close()
- self._path = None # The file path self._encoding = None # Encoding of that file
#end class InputURLList
class InputDirectory:
- """ Each Input class knows how to yield a set of URLs from a data source. This one handles a directory that acts as base for walking the filesystem. """
def init(self, attributes, base_url):
- self._path = None # The directory self._url = None # The URL equivelant self._default_file = None
if not ValidateAttributes('DIRECTORY', attributes, ('path', 'url',
- 'default_file')):
- return
- output.Error('Directory entries must have both "path" and "url" '
- 'attributes')
path = encoder.MaybeNarrowPath(path) if not path.endswith(os.sep):
- path = path + os.sep
- output.Error('Can not locate directory: %s' % path) return
- output.Error('Directory entries must have both "path" and "url" '
- 'attributes')
- url = url + '/'
- url = urlparse.urljoin(base_url, url) if not url.startswith(base_url):
- output.Error('The directory URL "%s" is not relative to the '
- 'base_url: %s' % (url, base_url))
- output.Error('The directory URL "%s" is not relative to the '
file = encoder.MaybeNarrowPath(file) if os.sep in file:
- output.Error('The default_file "%s" can not include path information.'
- % file)
- output.Error('The default_file "%s" can not include path information.'
- output.Log('Input: From DIRECTORY "%s" (%s) with default file "%s"'
- % (path, url, file), 2)
- output.Log('Input: From DIRECTORY "%s" (%s) with no default file'
- % (path, url), 2)
#end def init def ProduceURLs(self, consumer):
- """ Produces URLs from our data source, hands them in to the consumer. """ if not self._path:
- return
def PerFile(dirpath, name):
- """ Called once per file. Note that 'name' will occasionally be None -- for a directory itself """ # Pull a timestamp url = URL() isdir = False try:
- if name:
- path = os.path.join(dirpath, name)
- path = dirpath
- file = os.path.join(path, root_file) try:
- time = os.stat(file)[stat.ST_MTIME];
- pass
- time = os.stat(path)[stat.ST_MTIME];
- pass
except ValueError:
- pass
- middle = middle.replace(os.sep, '/')
- middle = middle + '/'
- middle = middle + name if isdir:
- middle = middle + '/'
url.TrySetAttribute('loc', root_URL + encoder.WidenText(middle, None)) # Suppress default files. (All the way down here so we can log it.) if name and (root_file == name):
- url.Log(prefix='IGNORED (default file)', level=2) return
- if name:
#end def PerFile
def PerDirectory(ignore, dirpath, namelist):
- """ Called once per directory with a list of all the contained files/dirs. """ ignore = ignore # Avoid warnings of an unused parameter if not dirpath.startswith(root_path):
- output.Warn('Unable to decide what the root path is for directory: '
- '%s' % dirpath)
PerFile(dirpath, name)
- output.Warn('Unable to decide what the root path is for directory: '
#end def PerDirectory output.Log('Walking DIRECTORY "%s"' % self._path, 1)
PerFile(self._path, None) os.path.walk(self._path, PerDirectory, None)
- self._path = None # The directory self._url = None # The URL equivelant self._default_file = None
#end class InputDirectory
class InputAccessLog:
- """ Each Input class knows how to yield a set of URLs from a data source. This one handles access logs. It's non-trivial in that we want to auto-detect log files in the Common Logfile Format (as used by Apache, for instance) and the Extended Log File Format (as used by IIS, for instance). """
def init(self, attributes):
- self._path = None # The file path self._encoding = None # Encoding of that file self._is_elf = False # Extended Log File Format? self._is_clf = False # Common Logfile Format? self._elf_status = -1 # ELF field: '200' self._elf_method = -1 # ELF field: 'HEAD' self._elf_uri = -1 # ELF field: '/foo?bar=1' self._elf_urifrag1 = -1 # ELF field: '/foo' self._elf_urifrag2 = -1 # ELF field: 'bar=1'
if not ValidateAttributes('ACCESSLOG', attributes, ('path', 'encoding')):
- return
self._path = encoder.MaybeNarrowPath(self._path) if os.path.isfile(self._path):
- output.Log('Input: From ACCESSLOG "%s"' % self._path, 2)
- output.Error('Can not locate file: %s' % self._path) self._path = None
- output.Error('Accesslog entries must have a "path" attribute.')
#end def init def RecognizeELFLine(self, line):
- """ Recognize the Fields directive that heads an ELF file """ if not line.startswith('#Fields:'):
- return False
- field = fields[i].strip() if field == 'sc-status':
- self._elf_status = i
- self._elf_method = i
- self._elf_uri = i
- self._elf_urifrag1 = i
- self._elf_urifrag2 = i
- """ Fetch the requested URL from an ELF line """ fields = line.split(' ') count = len(fields) # Verify status was Ok
if self._elf_status >= 0:
if self._elf_status >= count:
- return None
- return None
if self._elf_method >= 0:
if self._elf_method >= count:
- return None
- return None
if self._elf_uri >= 0:
if self._elf_uri >= count:
- return None
- return url
if self._elf_urifrag1 >= 0:
if self._elf_urifrag1 >= count or self._elf_urifrag2 >= count:
- return None
if self._elf_urifrag2 >= 0:
- urlfrag2 = fields[self._elf_urifrag2]
- if urlfrag2 and (urlfrag2 != '-'):
- urlfrag1 = urlfrag1 + '?' + urlfrag2
- """ Try to tokenize a logfile line according to CLF pattern and see if it works. """ match = ACCESSLOG_CLF_PATTERN.match(line) recognize = match and (match.group(1) in ('HEAD', 'GET')) if recognize:
- output.Log('Recognized a Common Logfile Format file.', 2)
- """ Fetch the requested URL from a CLF line """ match = ACCESSLOG_CLF_PATTERN.match(line) if match:
- request = match.group(1) if request in ('HEAD', 'GET'):
- return match.group(2)
- request = match.group(1) if request in ('HEAD', 'GET'):
- """ Produces URLs from our data source, hands them in to the consumer. """ # Open the file
(frame, file) = OpenFileForRead(self._path, 'ACCESSLOG') if not file:
- return
- if self._encoding:
line = encoder.WidenText(line, self._encoding)
- self._is_elf = self.RecognizeELFLine(line) self._is_clf = self.RecognizeCLFLine(line)
- match = self.GetELFLine(line)
- match = self.GetCLFLine(line)
- continue
url.TrySetAttribute('loc', match) consumer(url, True)
- frame.close()
- self._path = None # The file path self._encoding = None # Encoding of that file self._is_elf = False # Extended Log File Format? self._is_clf = False # Common Logfile Format? self._elf_status = -1 # ELF field: '200' self._elf_method = -1 # ELF field: 'HEAD' self._elf_uri = -1 # ELF field: '/foo?bar=1' self._elf_urifrag1 = -1 # ELF field: '/foo' self._elf_urifrag2 = -1 # ELF field: 'bar=1'
#end class InputAccessLog
class InputSitemap(xml.sax.handler.ContentHandler):
- """ Each Input class knows how to yield a set of URLs from a data source. This one handles Sitemap files and Sitemap index files. For the sake of simplicity in design (and simplicity in interfacing with the SAX package), we do not handle these at the same time, recursively. Instead we read an index file completely and make a list of Sitemap files, then go back and process each Sitemap. """
class _ContextBase(object):
- """Base class for context handlers in our SAX processing. A context handler is a class that is responsible for understanding one level of depth in the XML schema. The class knows what sub-tags are allowed, and doing any processing specific for the tag we're in. This base class is the API filled in by specific context handlers, all defined below. """
def init(self, subtags):
- """Initialize with a sequence of the sub-tags that would be valid in this context.""" self._allowed_tags = subtags # Sequence of sub-tags we can have self._last_tag = None # Most recent seen sub-tag
#end def init
def AcceptTag(self, tag):
- """Returns True iff opening a sub-tag is valid in this context.""" valid = tag in self._allowed_tags if valid:
- self._last_tag = tag
- self._last_tag = None
#end def AcceptTag
def AcceptText(self, text):
- """Returns True iff a blurb of text is valid in this context.""" return False
#end def AcceptText def Open(self):
- """The context is opening. Do initialization.""" pass
- """The context is closing. Return our result, if any.""" pass
- """We're returning to this context after handling a sub-tag. This method is called with the result data from the sub-tag that just
closed. Here in _ContextBase, if we ever see a result it means the derived child class forgot to override this method.""" if result:
raise NotImplementedError
#end class _ContextBase
class _ContextUrlSet(_ContextBase):
- """Context handler for the document node in a Sitemap."""
def init(self):
InputSitemap._ContextBase.init(self, ('url',))
#end def init
#end class _ContextUrlSet
class _ContextUrl(_ContextBase):
- """Context handler for a URL node in a Sitemap."""
def init(self, consumer):
- """Initialize this context handler with the callable consumer that wants our URLs."""
InputSitemap._ContextBase.init(self, URL.slots) self._url = None # The URL object we're building self._consumer = consumer # Who wants to consume it
#end def init def Open(self):
- """Initialize the URL.""" assert not self._url self._url = URL()
- """Pass the URL to the consumer and reset it to None.""" assert self._url self._consumer(self._url, False) self._url = None
- """A value context has closed, absorb the data it gave us.""" assert self._url if result:
self._url.TrySetAttribute(self._last_tag, result)
- """Initialize this context handler with the callable consumer that wants our URLs."""
#end class _ContextUrl
class _ContextSitemapIndex(_ContextBase):
- """Context handler for the document node in an index file."""
def init(self):
InputSitemap._ContextBase.init(self, ('sitemap',)) self._loclist = [] # List of accumulated Sitemap URLs
#end def init def Open(self):
- """Just a quick verify of state.""" assert not self._loclist
- """Return our list of accumulated URLs.""" if self._loclist:
- temp = self._loclist self._loclist = [] return temp
- """Getting a new loc URL, add it to the collection.""" if result:
- self._loclist.append(result)
#end class _ContextSitemapIndex
class _ContextSitemap(_ContextBase):
- """Context handler for a Sitemap entry in an index file."""
def init(self):
InputSitemap._ContextBase.init(self, ('loc', 'lastmod')) self._loc = None # The URL to the Sitemap
#end def init def Open(self):
- """Just a quick verify of state.""" assert not self._loc
- """Return our URL to our parent.""" if self._loc:
- temp = self._loc self._loc = None return temp
- """A value has closed. If it was a 'loc', absorb it.""" if result and (self._last_tag == 'loc'):
- self._loc = result
#end class _ContextSitemap
class _ContextValue(_ContextBase):
- """Context handler for a single value. We return just the value. The higher level context has to remember what tag led into us."""
def init(self):
InputSitemap._ContextBase.init(self, ()) self._text = None
#end def init
def AcceptText(self, text):
- """Allow all text, adding it to our buffer.""" if self._text:
- self._text = self._text + text
- self._text = text
#end def AcceptText def Open(self):
- """Initialize our buffer.""" self._text = None
- """Return what's in our buffer.""" text = self._text self._text = None if text:
- text = text.strip()
#end class _ContextValue
def init(self, attributes):
- """Initialize with a dictionary of attributes from our entry in the config file."""
xml.sax.handler.ContentHandler.init(self) self._pathlist = None # A list of files self._current = -1 # Current context in _contexts self._contexts = None # The stack of contexts we allow self._contexts_idx = None # ...contexts for index files self._contexts_stm = None # ...contexts for Sitemap files
if not ValidateAttributes('SITEMAP', attributes, ['path']):
- return
path = encoder.MaybeNarrowPath(path) if os.path.isfile(path):
- output.Log('Input: From SITEMAP "%s"' % path, 2) self._pathlist = [path]
- output.Error('Can not locate file "%s"' % path)
- output.Error('Sitemap entries must have a "path" attribute.')
#end def init def ProduceURLs(self, consumer):
- """In general: Produces URLs from our data source, hand them to the callable consumer. In specific: Iterate over our list of paths and delegate the actual processing to helper methods. This is a complexity no other data source needs to suffer. We are unique in that we can have files that tell us to bring in other files. Note the decision to allow an index file or not is made in this method. If we call our parser with (self._contexts == None) the parser will grab whichever context stack can handle the file. IE: index is allowed. If instead we set (self._contexts = ...) before parsing, the parser will only use the stack we specify. IE: index not allowed. """ # Set up two stacks of contexts
self._contexts_idx = [InputSitemap._ContextSitemapIndex(),
self._contexts_stm = [InputSitemap._ContextUrlSet(),
InputSitemap._ContextUrl(consumer), InputSitemap._ContextValue()]
self._ProcessFile(path) # Iterate over remaining files self._contexts = self._contexts_stm # No index files allowed for path in self._pathlist[1:]:
self._ProcessFile(path)
def _ProcessFile(self, path):
- """Do per-file reading/parsing/consuming for the file path passed in.""" assert path # Open our file
(frame, file) = OpenFileForRead(path, 'SITEMAP') if not file:
- return
- self._current = -1 xml.sax.parse(file, self)
except SchemaError:
- output.Error('An error in file "%s" made us abort reading the Sitemap.'
- % path)
- output.Error('Cannot read from file "%s"' % path)
- output.Error('XML error in the file "%s" (line %d, column %d): %s' %
- (path, e._linenum, e._colnum, e.getMessage()))
- frame.close()
#end def _ProcessFile
def _MungeLocationListIntoFiles(self, urllist):
- """Given a list of URLs, munge them into our self._pathlist property. We do this by assuming all the files live in the same directory as the first file in the existing pathlist. That is, we assume a Sitemap index points to Sitemaps only in the same directory. This is not true in general, but will be true for any output produced by this script. """ assert self._pathlist path = self._pathlist[0] path = os.path.normpath(path) dir = os.path.dirname(path) wide = False
if type(path) == types.UnicodeType:
- wide = True
- url = URL.Canonicalize(url) output.Log('Index points to Sitemap file at: %s' % url, 2) (scheme, netloc, path, query, frag) = urlparse.urlsplit(url) file = os.path.basename(path) file = urllib.unquote(file) if wide:
file = encoder.WidenText(file)
- file = dir + os.sep + file
- self._pathlist.append(file) output.Log('Will attempt to read Sitemap file: %s' % file, 1)
#end def _MungeLocationListIntoFiles def startElement(self, tag, attributes):
- """SAX processing, called per node in the config stream. As long as the new tag is legal in our current context, this becomes an Open call on one context deeper. """ # If this is the document node, we may have to look for a context stack
if (self._current < 0) and not self._contexts:
- assert self._contexts_idx and self._contexts_stm if tag == 'urlset':
- self._contexts = self._contexts_stm
- self._contexts = self._contexts_idx output.Log('File is a Sitemap index.', 2)
- output.Error('The document appears to be neither a Sitemap nor a '
- 'Sitemap index.')
raise SchemaError
if (self._current < 0) and (self._contexts == self._contexts_stm) and (
- tag == 'sitemapindex'): output.Error('A Sitemap index can not refer to another Sitemap index.')
raise SchemaError
text = for attr in attributes.keys():
- # The document node will probably have namespaces
if self._current < 0:
if attr.find('xmlns') >= 0:
- continue
if attr.find('xsi') >= 0:
- continue
- text = text + ', '
- output.Warn('Did not expect any attributes on any tag, instead tag '
- '"%s" had attributes: %s' % (tag, text))
- # The document node will probably have namespaces
if (self._current < 0) or (self._contexts[self._current].AcceptTag(tag)):
- self._current = self._current + 1
assert self._current < len(self._contexts) self._contexts[self._current].Open()
- output.Error('Can not accept tag "%s" where it appears.' % tag)
raise SchemaError
- assert self._contexts_idx and self._contexts_stm if tag == 'urlset':
- """SAX processing, called per node in the config stream. This becomes a call to Close on one context followed by a call to Return on the previous. """ tag = tag # Avoid warning on unused argument
assert self._current >= 0 retval = self._contexts[self._current].Close() self._current = self._current - 1 if self._current >= 0:
- self._contexts[self._current].Return(retval)
self._MungeLocationListIntoFiles(retval)
- """SAX processing, called when text values are read. Important to note that one single text value may be split across multiple calls of this method. """
if (self._current < 0) or (
not self._contexts[self._current].AcceptText(text)): if text.strip():
- output.Error('Can not accept text "%s" where it appears.' % text)
raise SchemaError
- output.Error('Can not accept text "%s" where it appears.' % text)
- """Base class for context handlers in our SAX processing. A context handler is a class that is responsible for understanding one level of depth in the XML schema. The class knows what sub-tags are allowed, and doing any processing specific for the tag we're in. This base class is the API filled in by specific context handlers, all defined below. """
#end class InputSitemap
class FilePathGenerator:
- """ This class generates filenames in a series, upon request. You can request any iteration number at any time, you don't have to go in order. Example of iterations for '/path/foo.xml.gz':
0 --> /path/foo.xml.gz 1 --> /path/foo1.xml.gz 2 --> /path/foo2.xml.gz _index.xml --> /path/foo_index.xml
def init(self):
- self.is_gzip = False # Is this a GZIP file? self._path = None # '/path/' self._prefix = None # 'foo' self._suffix = None # '.xml.gz'
#end def init def Preload(self, path):
- """ Splits up a path into forms ready for recombination. """
path = encoder.MaybeNarrowPath(path) # Get down to a base name path = os.path.normpath(path) base = os.path.basename(path).lower() if not base:
- output.Error('Couldn\'t parse the file path: %s' % path) return False
- if base.endswith(suffix):
- lensuffix = len(suffix) break
- output.Error('The path "%s" doesn\'t end in a supported file '
- 'extension.' % path)
def GeneratePath(self, instance):
- """ Generates the iterations, as described above. """ prefix = self._path + self._prefix
if type(instance) == types.IntType:
- if instance:
- return '%s%d%s' % (prefix, instance, self._suffix)
- if instance:
#end def GeneratePath def GenerateURL(self, instance, root_url):
- """ Generates iterations, but as a URL instead of a path. """ prefix = root_url + self._prefix retval = None
if type(instance) == types.IntType:
- if instance:
- retval = '%s%d%s' % (prefix, instance, self._suffix)
- retval = prefix + self._suffix
- retval = prefix + instance
- if instance:
- """ Generates a wildcard that should match all our iterations """ prefix = URL.Canonicalize(root_url + self._prefix) temp = URL.Canonicalize(prefix + self._suffix) suffix = temp[len(prefix):] return prefix + '*' + suffix
#end class FilePathGenerator
class PerURLStatistics:
- """ Keep track of some simple per-URL statistics, like file extension. """
def init(self):
- self._extensions = {} # Count of extension instances
#end def init def Consume(self, url):
- """ Log some stats for the URL. At the moment, that means extension. """ if url and url.loc:
- (scheme, netloc, path, query, frag) = urlparse.urlsplit(url.loc) if not path:
- return
- if self._extensions.has_key('/'):
- self._extensions['/'] = self._extensions['/'] + 1
- self._extensions['/'] = 1
if i >= 0:
assert i < len(path) path = path[i:]
if i > 0:
assert i < len(path) ext = path[i:].lower() if self._extensions.has_key(ext):
- self._extensions[ext] = self._extensions[ext] + 1
- self._extensions[ext] = 1
- if self._extensions.has_key('(no extension)'):
- self._extensions['(no extension)'] = self._extensions[
- '(no extension)'] + 1
- self._extensions['(no extension)'] = 1
- self._extensions['(no extension)'] = self._extensions[
- (scheme, netloc, path, query, frag) = urlparse.urlsplit(url.loc) if not path:
- """ Dump out stats to the output. """ if len(self._extensions):
- output.Log('Count of file extensions on URLs:', 1) set = self._extensions.keys() set.sort() for ext in set:
- output.Log(' %7d %s' % (self._extensions[ext], ext), 1)
- output.Log('Count of file extensions on URLs:', 1) set = self._extensions.keys() set.sort() for ext in set:
class Sitemap(xml.sax.handler.ContentHandler):
- """ This is the big workhorse class that processes your inputs and spits out sitemap files. It is built as a SAX handler for set up purposes. That is, it processes an XML stream to bring itself up. """
def init(self, suppress_notify):
xml.sax.handler.ContentHandler.init(self) self._filters = [] # Filter objects self._inputs = [] # Input objects self._urls = {} # Maps URLs to count of dups self._set = [] # Current set of URLs self._filegen = None # Path generator for output files self._wildurl1 = None # Sitemap URLs to filter out self._wildurl2 = None # Sitemap URLs to filter out self._sitemaps = 0 # Number of output files # We init _dup_max to 2 so the default priority is 0.5 instead of 1.0 self._dup_max = 2 # Max number of duplicate URLs self._stat = PerURLStatistics() # Some simple stats self._in_site = False # SAX: are we in a Site node? self._in_Site_ever = False # SAX: were we ever in a Site? self._default_enc = None # Best encoding to try on URLs self._base_url = None # Prefix to all valid URLs self._store_into = None # Output filepath self._suppress = suppress_notify # Suppress notify of servers
#end def init
def ValidateBasicConfig(self):
- """ Verifies (and cleans up) the basic user-configurable options. """ all_good = True if self._default_enc:
encoder.SetUserEncoding(self._default_enc)
- output.Error('A site needs a "base_url" attribute.') all_good = False
if all_good and not URL.IsAbsolute(self._base_url):
- output.Error('The "base_url" must be absolute, not relative: %s' %
- self._base_url)
- self._base_url = URL.Canonicalize(self._base_url) if not self._base_url.endswith('/'):
- self._base_url = self._base_url + '/'
- if self._store_into:
self._filegen = FilePathGenerator() if not self._filegen.Preload(self._store_into):
- all_good = False
- output.Error('A site needs a "store_into" attribute.') all_good = False
- self._wildurl1 = self._filegen.GenerateWildURL(self._base_url) self._wildurl2 = self._filegen.GenerateURL(SITEINDEX_SUFFIX,
- self._base_url)
- if self._suppress:
if (type(self._suppress) == types.StringType) or (type(self._suppress)
== types.UnicodeType):
- if (self._suppress == '0') or (self._suppress.lower() == 'false'):
- self._suppress = False
- output.Log('See "example_config.xml" for more information.', 0)
#end def ValidateBasicConfig def Generate(self):
- """ Run over all the Inputs and ask them to Produce """ # Run the inputs for input in self._inputs:
- input.ProduceURLs(self.ConsumeURL)
self.FlushSet()
- output.Warn('No URLs were recorded, writing an empty sitemap.')
self.FlushSet()
if self._sitemaps > 1:
self.WriteIndex()
self.NotifySearch() # Dump stats self._stat.Log()
- """ All per-URL processing comes together here, regardless of Input. Here we run filters, remove duplicates, spill to disk as needed, etc. """ if not url:
- return
- return
- accept = filter.Apply(url) if accept != None:
- break
- url.Log(prefix='FILTERED', level=2) return
- url.loc, self._wildurl2): url.Log(prefix='IGNORED (output file)', level=2) return
hash = url.MakeHash() if self._urls.has_key(hash):
- dup = self._urls[hash]
if dup > 0:
- dup = dup + 1 self._urls[hash] = dup
if self._dup_max < dup:
- self._dup_max = dup
- dup = dup + 1 self._urls[hash] = dup
if len(self._set) >= MAXURLS_PER_SITEMAP:
self.FlushSet()
def FlushSet(self):
- """ Flush the current set of URLs to the output. This is a little slow because we like to sort them all and normalize the priorities before dumping. """ # Sort and normalize output.Log('Sorting and normalizing collected URLs.', 1) self._set.sort() for url in self._set:
hash = url.MakeHash() dup = self._urls[hash] if dup > 0:
- self._urls[hash] = -1 if not url.priority:
- url.priority = '%.4f' % (float(dup) / float(self._dup_max))
- self._urls[hash] = -1 if not url.priority:
filename = self._filegen.GeneratePath(self._sitemaps) if not filename:
- output.Fatal('Unexpected: Couldn\'t generate output filename.')
- (filename, len(self._set)), 1)
- if self._filegen.is_gzip:
- basename = os.path.basename(filename); frame = open(filename, 'wb')
file = gzip.GzipFile(fileobj=frame, filename=basename, mode='wt')
- file = open(filename, 'wt')
- url.WriteXML(file)
- frame.close()
- basename = os.path.basename(filename); frame = open(filename, 'wb')
- output.Fatal('Couldn\'t write out to file: %s' % filename)
#end def FlushSet
def WriteIndex(self):
- """ Write the master index of all Sitemap files """ # Make a filename
filename = self._filegen.GeneratePath(SITEINDEX_SUFFIX) if not filename:
- output.Fatal('Unexpected: Couldn\'t generate output index filename.')
- (filename, self._sitemaps), 1)
- fd = open(filename, 'wt') fd.write(SITEINDEX_HEADER) for mapnumber in range(0,self._sitemaps):
- # Write the entry mapurl = self._filegen.GenerateURL(mapnumber, self._base_url) mapattributes = { 'loc' : mapurl, 'lastmod' : lastmod } fd.write(SITEINDEX_ENTRY % mapattributes)
- output.Fatal('Couldn\'t write out to file: %s' % filename)
#end def WriteIndex
def NotifySearch(self):
- """ Send notification of the new Sitemap(s) to the search engines. """ if self._suppress:
- output.Log('Search engine notification is suppressed.', 1) return
- def http_error_default(self, url, fp, errcode, errmsg, headers):
- output.Log('HTTP error %d: %s' % (errcode, errmsg), 2) raise IOError
if self._sitemaps > 1:
- url = self._filegen.GenerateURL(SITEINDEX_SUFFIX, self._base_url)
- url = self._filegen.GenerateURL(0, self._base_url)
- u = urllib.urlopen(url) u.close()
- output.Error('When attempting to access our generated Sitemap at the '
'following URL:\n %s\n we failed to read it. Please ' 'verify the store_into path you specified in\n' ' your configuration file is web-accessable. Consult ' 'the FAQ for more\n information.' % url)
- query_map = ping[3] query_attr = ping[5] query_map[query_attr] = url query = urllib.urlencode(query_map) notify = urlparse.urlunsplit((ping[0], ping[1], ping[2], query, ping[4])) # Send the notification output.Log('Notifying: %s' % ping[1], 1) output.Log('Notification URL: %s' % notify, 2) try:
- u = urllib.urlopen(notify) u.read() u.close()
- output.Warn('Cannot contact: %s' % ping[1])
- urllib._urlopener = old_opener
#end def NotifySearch def startElement(self, tag, attributes):
- """ SAX processing, called per node in the config stream. """ if tag == 'site':
- if self._in_site:
- output.Error('Can not nest Site entries in the configuration.')
- self._in_site = True
if not ValidateAttributes('SITE', attributes,
- ('verbose', 'default_encoding', 'base_url', 'store_into',
- 'suppress_search_engine_notify')):
output.SetVerbose(verbose)
- self._suppress = attributes.get('suppress_search_engine_notify',
- False)
self.ValidateBasicConfig()
- ('verbose', 'default_encoding', 'base_url', 'store_into',
- self._filters.append(Filter(attributes))
- self._inputs.append(InputURL(attributes))
for attributeset in ExpandPathAttribute(attributes, 'path'):
- self._inputs.append(InputURLList(attributeset))
self._inputs.append(InputDirectory(attributes, self._base_url))
for attributeset in ExpandPathAttribute(attributes, 'path'):
self._inputs.append(InputAccessLog(attributeset))
for attributeset in ExpandPathAttribute(attributes, 'path'):
self._inputs.append(InputSitemap(attributeset))
- output.Error('Unrecognized tag in the configuration: %s' % tag)
- if self._in_site:
- """ SAX processing, called per node in the config stream. """ if tag == 'site':
- assert self._in_site self._in_site = False self._in_site_ever = True
- """ End of SAX, verify we can proceed. """ if not self._in_site_ever:
- output.Error('The configuration must specify a "site" element.')
- if not self._inputs:
- output.Warn('There were no inputs to generate a sitemap from.')
#end class Sitemap
def ValidateAttributes(tag, attributes, goodattributes):
- """ Makes sure 'attributes' does not contain any attribute not
- listed in 'goodattributes' """
- if not attr in goodattributes:
- output.Error('Unknown %s attribute: %s' % (tag, attr)) all_good = False
#end def ValidateAttributes
def ExpandPathAttribute(src, attrib):
- """ Given a dictionary of attributes, return a list of dictionaries
- with all the same attributes except for the one named attrib. That one, we treat as a file path and expand into all its possible variations. """
- return [src]
path = encoder.MaybeNarrowPath(path); pathlist = glob.glob(path) if not pathlist:
- return [src]
if type(src) != types.DictionaryType:
- tmp = {} for key in src.keys():
- tmp[key] = src[key]
- dst = src.copy() dst[attrib] = path retval.append(dst)
#end def ExpandPathAttribute
def OpenFileForRead(path, logtext):
- """ Opens a text file, be it GZip or plain """ frame = None file = None if not path:
- return (frame, file)
- if path.endswith('.gz'):
- frame = open(path, 'rb')
file = gzip.GzipFile(fileobj=frame, mode='rt')
- file = open(path, 'rt')
- output.Log('Opened %s file: %s' % (logtext, path), 1)
- output.Log('Opened file: %s' % path, 1)
- frame = open(path, 'rb')
- output.Error('Can not open file: %s' % path)
#end def OpenFileForRead
def TimestampISO8601(t):
"""Seconds since epoch (1970-01-01) --> ISO 8601 time string.""" return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(t))
#end def TimestampISO8601
def CreateSitemapFromFile(configpath, suppress_notify):
- """ Sets up a new Sitemap object from the specified configuration file. """ # Remember error count on the way in num_errors = output.num_errors # Rev up SAX to parse the config sitemap = Sitemap(suppress_notify) try:
- output.Log('Reading configuration file: %s' % configpath, 0) xml.sax.parse(configpath, sitemap)
- output.Error('Cannot read configuration file: %s' % configpath)
- output.Error('XML error in the config file (line %d, column %d): %s' %
- (e._linenum, e._colnum, e.getMessage()))
- output.Error('Some installs of Python 2.2 did not include complete support'
- ' for XML.\n Please try upgrading your version of Python' ' and re-running the script.')
- return sitemap
#end def CreateSitemapFromFile
def ProcessCommandFlags(args):
- """ Parse command line flags per specified usage, pick off key, value pairs
All flags of type "--key=value" will be processed as flags[key] = value,
"--option" will be processed as
rkeyval = '?P<key>\S*)[=](?P<value>\S*)' # --key=val roption = '--(?P<option>\S*)' # --key r = '(' + rkeyval + ')|(' + roption + ')' rc = re.compile(r) for a in args: except AttributeError: #end def ProcessCommandFlags # # main # if name == 'main': flags = ProcessCommandFlags(sys.argv[1:]) if not flags or not flags.has_key('config') or flags.has_key('help'): output.Log(usage, 0) sitemap = CreateSitemapFromFile(flags['config'], suppress_notify) if not sitemap:
return flags
if rcg.has_key('option'):
else:
else:
