manage.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python2
  2. # MyServer
  3. import gevent.pywsgi
  4. from gevent import monkey; monkey.patch_all()
  5. import werkzeug.serving
  6. from werkzeug.debug import DebuggedApplication
  7. import os; os.environ.setdefault('FFDNISPDB_SETTINGS', '../settings_dev.py')
  8. import sys
  9. from flask.ext.script import (Shell, Server, Manager, Command, Option,
  10. prompt, prompt_bool, prompt_pass)
  11. import ffdnispdb
  12. class CreateDB(Command):
  13. "Initialize database, create tables"
  14. def handle(self, app, *args, **kwargs):
  15. with app.app_context():
  16. return self.run(*args, **kwargs)
  17. def run(self):
  18. ffdnispdb.db.create_all()
  19. database_manager = Manager(usage=u'Perform database operations')
  20. database_manager.add_command("create", CreateDB)
  21. @database_manager.command
  22. def drop():
  23. "Drop existing tables"
  24. if prompt_bool("Are you sure you wish to destroy existing database"):
  25. ffdnispdb.db.drop_all()
  26. index_manager = Manager(usage=u'Manage the Whoosh index')
  27. @index_manager.command
  28. def rebuild():
  29. "Rebuild the Whoosh index from SQL datatabase"
  30. from ffdnispdb.models import ISP, ISPWhoosh
  31. from whoosh import writing
  32. import shutil
  33. shutil.rmtree(ISPWhoosh.get_index_dir())
  34. idx=ISPWhoosh.get_index()
  35. with idx.writer() as writer:
  36. for isp in ISP.query.all():
  37. ISPWhoosh.update_document(writer, isp)
  38. writer.mergetype = writing.CLEAR
  39. writer.optimize = True
  40. class MyServer(Server):
  41. def handle(self, app, host, port, use_debugger, use_reloader,
  42. threaded, processes, passthrough_errors):
  43. if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
  44. print >>sys.stderr, ' * Running on http://%s:%d/'%(host, port)
  45. if use_debugger:
  46. app=DebuggedApplication(app, evalex=True)
  47. @werkzeug.serving.run_with_reloader
  48. def run():
  49. ws = gevent.pywsgi.WSGIServer((host, port), app)
  50. ws.serve_forever()
  51. def shell_context():
  52. import ffdnispdb
  53. return ffdnispdb.__dict__
  54. class RunTests(Command):
  55. "Run unit-tests"
  56. option_list = (
  57. Option('--verbose', '-v', action='store_true', default=False),
  58. Option('--failfast', '-f', action='store_true', default=False),
  59. )
  60. def run(self, verbose, failfast):
  61. import unittest
  62. import test_ffdnispdb
  63. test=unittest.defaultTestLoader.loadTestsFromModule(test_ffdnispdb)
  64. unittest.TextTestRunner(verbosity=2 if verbose else 1, failfast=bool(failfast)).run(test)
  65. manager = Manager(ffdnispdb.create_app)
  66. manager.add_command("runserver", MyServer())
  67. manager.add_command("shell", Shell(make_context=shell_context))
  68. manager.add_command("db", database_manager)
  69. manager.add_command("index", index_manager)
  70. manager.add_command("test", RunTests())
  71. if __name__ == "__main__":
  72. manager.run()