You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.6 KiB

  1. #!/usr/bin/python
  2. import nilmdb.server
  3. import argparse
  4. import os
  5. import socket
  6. def main():
  7. """Main entry point for the 'nilmdb-server' command line script"""
  8. parser = argparse.ArgumentParser(
  9. description = 'Run the NilmDB server',
  10. formatter_class = argparse.ArgumentDefaultsHelpFormatter)
  11. parser.add_argument("-V", "--version", action="version",
  12. version = nilmdb.__version__)
  13. group = parser.add_argument_group("Standard options")
  14. group.add_argument('-a', '--address',
  15. help = 'Only listen on the given address',
  16. default = '0.0.0.0')
  17. group.add_argument('-p', '--port', help = 'Listen on the given port',
  18. type = int, default = 12380)
  19. group.add_argument('-d', '--database', help = 'Database directory',
  20. default = os.path.join(os.getcwd(), "db"))
  21. group.add_argument('-q', '--quiet', help = 'Silence output',
  22. action = 'store_true')
  23. group = parser.add_argument_group("Debug options")
  24. group.add_argument('-y', '--yappi', help = 'Run under yappi profiler and '
  25. 'invoke interactive shell afterwards',
  26. action = 'store_true')
  27. args = parser.parse_args()
  28. # Create database object. Needs to be serialized before passing
  29. # to the Server.
  30. db = nilmdb.utils.serializer_proxy(NilmDB, args.database)
  31. # Configure the server
  32. if args.quiet:
  33. embedded = True
  34. else:
  35. embedded = False
  36. server = nilmdb.server.Server(db,
  37. host = args.address,
  38. port = args.port,
  39. embedded = embedded)
  40. # Print info
  41. if not args.quiet:
  42. print "Database: %s" % (os.path.realpath(args.database))
  43. if args.address == '0.0.0.0' or args.address == '::':
  44. host = socket.getfqdn()
  45. else:
  46. host = args.address
  47. print "Server URL: http://%s:%d/" % ( host, args.port)
  48. print "----"
  49. # Run it
  50. if args.yappi:
  51. print "Running in yappi"
  52. try:
  53. import yappi
  54. yappi.start()
  55. server.start(blocking = True)
  56. finally:
  57. yappi.stop()
  58. yappi.print_stats(sort_type = yappi.SORTTYPE_TTOT, limit = 50)
  59. from IPython import embed
  60. embed(header = "Use the yappi object to explore further, "
  61. "quit to exit")
  62. else:
  63. server.start(blocking = True)
  64. # Clean up
  65. if not args.quiet:
  66. print "Closing database"
  67. db.close()
  68. if __name__ == "__main__":
  69. main()