zeronet.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python2.7
  2. # Included modules
  3. import os
  4. import sys
  5. def main():
  6. if "--silent" not in sys.argv:
  7. print "- Starting ZeroNet..."
  8. main = None
  9. try:
  10. app_dir = os.path.dirname(os.path.abspath(__file__))
  11. os.chdir(app_dir) # Change working dir to zeronet.py dir
  12. sys.path.insert(0, os.path.join(app_dir, "src/lib")) # External liblary directory
  13. sys.path.insert(0, os.path.join(app_dir, "src")) # Imports relative to src
  14. import main
  15. main.start()
  16. if main.update_after_shutdown: # Updater
  17. import gc
  18. import update
  19. # Try cleanup openssl
  20. try:
  21. if "lib.opensslVerify" in sys.modules:
  22. sys.modules["lib.opensslVerify"].opensslVerify.closeLibrary()
  23. except Exception, err:
  24. print "Error closing opensslVerify lib", err
  25. try:
  26. if "lib.pyelliptic" in sys.modules:
  27. sys.modules["lib.pyelliptic"].openssl.closeLibrary()
  28. except Exception, err:
  29. print "Error closing pyelliptic lib", err
  30. # Close lock file
  31. sys.modules["main"].lock.close()
  32. # Update
  33. try:
  34. update.update()
  35. except Exception, err:
  36. print "Update error: %s" % err
  37. except Exception, err: # Prevent closing
  38. import traceback
  39. try:
  40. import logging
  41. logging.exception("Unhandled exception: %s" % err)
  42. except Exception, log_err:
  43. print "Failed to log error:", log_err
  44. traceback.print_exc()
  45. from Config import config
  46. traceback.print_exc(file=open(config.log_dir + "/error.log", "a"))
  47. if main and (main.update_after_shutdown or main.restart_after_shutdown): # Updater
  48. import atexit
  49. print "Restarting..."
  50. # Close log files
  51. logger = sys.modules["main"].logging.getLogger()
  52. for handler in logger.handlers[:]:
  53. handler.flush()
  54. handler.close()
  55. logger.removeHandler(handler)
  56. atexit._run_exitfuncs()
  57. import time
  58. time.sleep(1) # Wait files to close
  59. args = sys.argv[:]
  60. sys.executable = sys.executable.replace(".pkg", "") # Frozen mac fix
  61. if not getattr(sys, 'frozen', False):
  62. args.insert(0, sys.executable)
  63. if sys.platform == 'win32':
  64. args = ['"%s"' % arg for arg in args]
  65. try:
  66. print "Executing %s %s" % (sys.executable, args)
  67. os.execv(sys.executable, args)
  68. except Exception, err:
  69. print "Execv error: %s" % err
  70. print "Bye."
  71. if __name__ == '__main__':
  72. main()