constance

Scripts for generating (an earlier obsolete version of) my personal web site
git clone https://code.djc.id.au/git/constance/
commit 584924028e8dee85f5b9701c629feb2a4e319870
parent 4b2a8e441cc5365422141a005ca42b3969861eb1
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sun, 15 Aug 2010 14:15:24 +1000

only write output if file has changed (to preserve modtimes)

Diffstat:
Mblog.py | 7++++---
Mconstance.py | 10++++++++++
Mhomepage.py | 5+++--
Mreading.py | 5+++--
Mtags.py | 5+++--
5 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/blog.py b/blog.py
@@ -8,6 +8,7 @@ from datetime import datetime
 import genshi.template
 import lxml.etree
 
+import constance
 import viewutils
 
 template_loader = genshi.template.TemplateLoader(
@@ -69,15 +70,15 @@ def generate(dir, xslt):
     for entry in entries:
         rendered = template_loader.load('entry.html').generate(item=entry).render('xhtml')
         transformed = str(xslt(lxml.etree.fromstring(rendered)))
-        open(os.path.join(dir, entry.id.encode('utf8') + '.html'), 'w').write(transformed)
+        constance.output(os.path.join(dir, entry.id.encode('utf8') + '.html'), transformed)
     
     # index
     rendered = template_loader.load('index.html').generate(items=entries).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
-    open(os.path.join(dir, 'index.html'), 'w').write(transformed)
+    constance.output(os.path.join(dir, 'index.html'), transformed)
 
     # feed
     rendered = template_loader.load('index.atom').generate(items=entries).render('xml')
-    open(os.path.join(dir, 'index.atom'), 'w').write(rendered)
+    constance.output(os.path.join(dir, 'index.atom'), rendered)
 
     return entries
diff --git a/constance.py b/constance.py
@@ -18,6 +18,16 @@ import reading
 import tags
 import homepage
 
+def output(filename, content):
+    assert isinstance(content, str)
+    if os.path.exists(filename):
+        existing = open(filename, 'r').read()
+        if content == existing:
+            print 'Skipped %s' % filename
+            return
+    open(filename, 'w').write(content)
+    print 'Wrote %s' % filename
+
 def main():
     parser = optparse.OptionParser()
     parser.add_option('--blog-dir', metavar='DIR')
diff --git a/homepage.py b/homepage.py
@@ -6,6 +6,7 @@ from itertools import chain
 import genshi.template
 import lxml.etree
 
+import constance
 import viewutils
 
 template_loader = genshi.template.TemplateLoader(
@@ -17,8 +18,8 @@ def generate(dir, xslt, blog_entries, reading_entries):
     template = template_loader.load('index.html')
     rendered = template.generate(blog_entries=blog_entries).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
-    open(os.path.join(dir, 'index.html'), 'w').write(transformed)
+    constance.output(os.path.join(dir, 'index.html'), transformed)
 
     # firehose
     rendered = template_loader.load('firehose.atom').generate(items=chain(blog_entries, reading_entries)).render('xml')
-    open(os.path.join(dir, 'firehose.atom'), 'w').write(rendered)
+    constance.output(os.path.join(dir, 'firehose.atom'), rendered)
diff --git a/reading.py b/reading.py
@@ -6,6 +6,7 @@ import genshi.template
 import yaml
 import lxml.etree
 
+import constance
 import viewutils
 
 template_loader = genshi.template.TemplateLoader(
@@ -43,10 +44,10 @@ def generate(filename, xslt):
 
     rendered = template_loader.load('reading.html').generate(items=entries).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
-    open(os.path.join(os.path.dirname(filename), 'reading.html'), 'w').write(transformed)
+    constance.output(os.path.join(os.path.dirname(filename), 'reading.html'), transformed)
 
     # feed
     rendered = template_loader.load('reading.atom').generate(items=entries).render('xml')
-    open(os.path.join(os.path.dirname(filename), 'reading.atom'), 'w').write(rendered)
+    constance.output(os.path.join(os.path.dirname(filename), 'reading.atom'), rendered)
 
     return entries
diff --git a/tags.py b/tags.py
@@ -5,6 +5,7 @@ import os
 import genshi.template
 import lxml.etree
 
+import constance
 import viewutils
 
 template_loader = genshi.template.TemplateLoader(
@@ -21,8 +22,8 @@ def generate(dir, xslt, blog_entries):
         tagged_entries = [e for e in blog_entries if tag in e.tags]
         rendered = template_loader.load('tag.html').generate(tag=tag, items=tagged_entries).render('xhtml')
         transformed = str(xslt(lxml.etree.fromstring(rendered)))
-        open(os.path.join(dir, tag.encode('utf8') + '.html'), 'w').write(transformed)
+        constance.output(os.path.join(dir, tag.encode('utf8') + '.html'), transformed)
 
     rendered = template_loader.load('index.html').generate(tag_freqs=tag_freqs).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
-    open(os.path.join(dir, 'index.html'), 'w').write(transformed)
+    constance.output(os.path.join(dir, 'index.html'), transformed)