#! /usr/bin/env python # blogtouch # Copyright 2008 by Akkana Peck: feel free to use this under the GPLv2. # Walk my static pyblosxom directory tree searching for all files # containing a pattern. For each matching file found, # set the date on it to be early enough that it will be # regenerated with ./pyblosxom.cgi --static --incremental import os, string, stat def add_tags(path, tags) : print path, "#tags " + ",".join(tags) workpath = path + ".wrk" # Get and save the last-modified time filestat = os.stat(path) # Open the input file try : ifp = open( path, 'r' ) except IOError : print "File:", path, "missing" return False # Open the output file try : ofp = open( workpath, 'w' ) except IOError : print "Can't write file:", workpath return False # Read the first line (the title) and write it line = ifp.readline() ofp.write(line) # Read the old second line and see if it already had tags line = ifp.readline() # A smart program would merge them here, but I don't have # any existing tags except in a couple of test files, # so I'll just use the existing tags if present, # ignoring the directory. if line[0:6] != "#tags " : ofp.write("#tags " + ",".join(tags) + "\n") ofp.write(line) while True : line = ifp.readline() if not line : break ofp.write(line) ofp.close() ifp.close() os.unlink(path) os.rename(workpath, path) # Set the last-mod time back to what it was before os.utime(path, (filestat.st_atime, filestat.st_mtime) ) def check_dir(dummy, dirname, names) : for fil in names : if fil[-4:] == ".blx" : tags = string.split(dirname[1:], "/") tags = tags[4:] # skip the /home/user/blah/ part add_tags(dirname + "/" + fil, tags) os.path.walk(os.environ["HOME"] + "/path_to/blog_files", check_dir, "")