# walker.py -*- coding: ISO-8859-1 -*- # # Copyright © 2000, 2004 Carey Evans. # # Permission to use, copy, modify, and distribute this software and # its documentation for any purpose and without fee is hereby granted, # provided that the above copyright notice appear in all copies and # that both that copyright notice and this permission notice appear in # supporting documentation. # # $Id: walker.py 15 2004-01-12 05:15:13Z carey $ """walker.py Check all the pages passed from the sitemap, and build where necessary. """ import sys, os, errno import shutil import builder loglevel = 1 def log(message, level=0): if level <= loglevel: sys.stderr.write(message) class PageWalker: def __init__(self, srcdir, dstdir, build_all): self.srcdir = srcdir self.dstdir = dstdir self.all = build_all self.builder = builder.PageBuilder() def process(self, dirs): for d in dirs: log('Entering %s\n' % d.srcfile(self.srcdir), 1) self.process_pages(d) self.process_datafiles(d) def process_pages(self, dir): for p in dir.pages: src = p.srcfile(self.srcdir) dst = p.dstfile(self.dstdir) if self.checkbuild(p.path[-1], src, dst): d = os.path.dirname(dst) if not os.path.isdir(d): os.makedirs(d) self.builder.build(p, src, dst) def process_datafiles(self, dir): for d in dir.datafiles: src = d.srcfile(self.srcdir) dst = d.dstfile(self.dstdir) if self.checkbuild(d.path[-1], src, dst): shutil.copyfile(src, dst) def checkbuild(self, file, src, dst): try: srctime = os.path.getmtime(src) except OSError, e: if e.errno == errno.ENOENT: log(' ! Source not found, skipping %s\n' % (file,)) return 0 else: raise if self.all: log(' + Build all, processing %s\n' % (file,), 1) return 1 try: dsttime = os.path.getmtime(dst) except OSError, e: if e.errno == errno.ENOENT: log(' + Dest not found, processing %s\n' % (file,), 1) return 1 raise if srctime > dsttime: log(' + Source is newer, processing %s\n' % (file,), 1) return 1 else: log(' - Dest is newer, skipping %s\n' % (file,), 2) return 0