#!/usr/local/bin/python """ mkoldlinks - Given a tree with an "old-style" spunk distribution and a database of new locations remove the datafiles and replaces them with links to the new files. """ import sys import os import dbm import regex import string DIR="anarchy_texts" DATABASE="indexdb" MATCHPROG=regex.compile("Spunk\([0-9][0-9][0-9]\)\..*") IGNORE=[".", "..", "Index.txt"] def main(): dict = dbm.open(DATABASE, 'r') # Collect dictionaries os.path.walk(DIR, walker, dict) def walker(dict, dirname, names): depth = string.count(dirname, '/') prefix = '../'*(depth+1) for name in names: fullname = os.path.join(dirname, name) if name in IGNORE or os.path.isdir(fullname): continue if MATCHPROG.match(name) == len(name): id = MATCHPROG.group(1) id = string.atoi(id) id = '%06.6d'%id if dict.has_key(id): os.unlink(fullname) os.symlink(prefix+dict[id], fullname) else: print 'Missing:', fullname main()