Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick and dirty script to create a Bikini 2.x database file from previous 1.x /pages/* files. Copy this into your Bikini 2.x installation directory and run "python upgrade.py" command. The script will look for a "pages" directory and will load the most recent revision of every page found in there.
'''
Quick and dirty script to create a Bikini 2.x database file from previous 1.x /pages/* files.
'''
import hashlib, re, codecs, glob, sqlite3
DATABASE_NAME = 'bikini.db'
ENCODING = 'utf-8'
FILENAME_MASK = u'./pages/%s%s.%s'
from bikini import storage
def make_hash(s):
return hashlib.md5(s).hexdigest()
def encode(value):
return value.encode(ENCODING, 'replace')
re_filename = re.compile(r'^\./pages/(\w+)\.(\d+)\.\w+$', re.U)
def get_page_info(filename):
page_id, revision_id = re_filename.match(filename).groups()
return page_id, int(revision_id)
def make_filename(page_id, revision_id=u'', filetype=u'txt'):
if revision_id:
return FILENAME_MASK % (page_id, u'.%02d' % int(revision_id), filetype)
else:
return FILENAME_MASK % (page_id, u'', filetype)
def run():
conn = storage.connect()
storage.setup_db(conn)
filenames = glob.glob(FILENAME_MASK % ('*', '[0-9]', 'txt'))
filenames.sort()
filenames.reverse()
inserted_names = {}
for filename in filenames:
name, revision_id = get_page_info(filename)
# For each page keep the highest revision found
if name in inserted_names:
continue
inserted_names[name] = name
content = codecs.open(filename, 'r', encoding=ENCODING).read()
content_html = codecs.open(make_filename(name, filetype='html'), 'r', encoding=ENCODING).read()
content_hash = make_hash(encode(content))
with conn:
storage.add_page(conn, name, name, content_html, content_hash)
storage.add_revision(conn, name, revision_id, content, 'AnonymousCoward', 'localhost', message='Initial import.')
print 'Added revision %d for page %s.' % (revision_id, name)
print '%d pages stored into bikini.db file.' % len(inserted_names)
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment