Personal wiki notebook (not under development)

convert_sessions.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Basic script to convert file-based CherryPy sessions to PostgreSQL-backed sessions.
  2. import os
  3. import cPickle as pickle
  4. from controller.Database import Database
  5. from controller.Session_storage import Session_storage
  6. def main( args ):
  7. import cherrypy
  8. from config import Common
  9. cherrypy.config.update( Common.settings )
  10. if args and "-d" in args:
  11. from config import Development
  12. settings = Development.settings
  13. else:
  14. from config import Production
  15. settings = Production.settings
  16. cherrypy.config.update( settings )
  17. database = Database(
  18. host = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_host" ),
  19. ssl_mode = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_ssl_mode" ),
  20. data_dir = ".",
  21. )
  22. class Stub_root( object ):
  23. def __init__( self, database ):
  24. self.database = database
  25. cherrypy.root = Stub_root( database )
  26. sessions = Session_storage()
  27. session_count = 0
  28. for session_filename in os.listdir( u"session/" ):
  29. pickled_data = file( u"session/%s" % session_filename ).read()
  30. ( data, expiration_time ) = pickle.loads( pickled_data )
  31. session_id = data[ u"_id" ]
  32. sessions.save( session_id, data, expiration_time )
  33. session_count += 1
  34. print "converted %d sessions" % session_count
  35. if __name__ == "__main__":
  36. import sys
  37. main( sys.argv[ 1: ] )