terom@12: import struct terom@12: import base64 terom@12: import shelve terom@12: import os.path terom@12: terom@12: from log import index terom@12: terom@12: def int2key (id) : terom@12: """ terom@12: Turn an integer into a short-as-possible url-safe string terom@12: """ terom@12: for type in ('B', 'H', 'I') : terom@12: try : terom@12: return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') terom@12: except struct.error : terom@12: continue terom@12: terom@12: raise Exception("ID overflow: %s" % id) terom@12: terom@12: def updateDB (root) : terom@12: """ terom@12: DeGAL <= 0.2 used a simple key => path mapping, but now we use terom@12: something more structured, key => (type, dirpath, fname), where terom@12: terom@12: type - one of 'img', 'dir' terom@12: dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux' terom@12: fname - the filename, one of '', 'DSC9839.JPG', 'this.png', etc. terom@12: """ terom@12: terom@12: db = shelve.open('shorturls2', 'c', writeback=True) terom@12: terom@12: id = db.get('_id', 1) terom@12: terom@12: dirqueue = [root] terom@12: terom@12: # dict of path -> obj terom@12: paths = {} terom@12: terom@12: index.info("Processing ShortURLs...") terom@12: terom@12: while dirqueue : terom@12: dir = dirqueue.pop(0) terom@12: terom@12: dirqueue.extend(dir.subdirs.itervalues()) terom@12: terom@12: if dir.alive : terom@12: paths[dir.path] = dir terom@12: terom@12: for img in dir.images.itervalues() : terom@12: paths[img.path] = img terom@12: terom@12: for key in db.keys() : terom@12: if key.startswith('_') : terom@12: continue terom@12: terom@12: type, dirpath, fname = db[key] terom@12: terom@12: path = os.path.join(dirpath, fname).rstrip(os.sep) terom@12: terom@12: try : terom@12: paths.pop(path).shorturl_code = key terom@12: index.debug("Code for `%s' is %s", path, key) terom@12: terom@12: except KeyError : terom@12: index.debug("Path `%s' in DB does not exist?", path) terom@12: terom@12: for obj in paths.itervalues() : terom@12: key = int2key(id) terom@12: id += 1 terom@12: terom@12: index.info("Alloc code `%s' for `%s'", key, obj.html_path) terom@12: terom@12: obj.shorturl_code = key terom@12: terom@12: db[key] = obj.getObjInfo() terom@12: terom@12: db['_id'] = id terom@12: db.close()