#!/usr/local/bin/python """ mkdatabase - Recursively descend the texts and images directories and build a dbm database mapping spunk-numbers to filenames. Also check that all numbers exist. """ import sys import os import dbm import regex import string DIRS=["texts", "images"] DATABASE="indexdb" MATCHPROG=regex.compile("sp\([0-9][0-9][0-9][0-9][0-9][0-9]\)\..*") IGNORE=[".", "..", "index.txt"] def main(): dict = {} # Collect dictionaries for d in DIRS: os.path.walk(d, walker, dict) # Ckech that all keys exist keys = dict.keys() keys.sort() lastkey = string.atoi(keys[-1]) for i in range(1, lastkey+1): thiskey = '%06.6d'%i if not dict.has_key(thiskey): print 'Missing: ', thiskey # Copy dict to dbm database dbmdb = dbm.open(DATABASE, 'n') for k in keys: dbmdb[k] = dict[k] def walker(dict, dirname, names): 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) if dict.has_key(id): print 'Duplicate:', dict[id] print ' ', fullname else: dict[id] = fullname else: print 'Unexpected:', fullname main()