zeronet.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import atexit
  20. # Try cleanup openssl
  21. try:
  22. if "lib.opensslVerify" in sys.modules:
  23. sys.modules["lib.opensslVerify"].opensslVerify.closeLibrary()
  24. except Exception, err:
  25. print "Error closing opensslVerify lib", err
  26. try:
  27. if "lib.pyelliptic" in sys.modules:
  28. sys.modules["lib.pyelliptic"].openssl.closeLibrary()
  29. except Exception, err:
  30. print "Error closing pyelliptic lib", err
  31. # Close lock file
  32. sys.modules["main"].lock.close()
  33. # Update
  34. try:
  35. update.update()
  36. except Exception, err:
  37. print "Update error: %s" % err
  38. # Close log files
  39. logger = sys.modules["main"].logging.getLogger()
  40. for handler in logger.handlers[:]:
  41. handler.flush()
  42. handler.close()
  43. logger.removeHandler(handler)
  44. atexit._run_exitfuncs()
  45. except Exception, err: # Prevent closing
  46. import traceback
  47. try:
  48. import logging
  49. logging.exception("Unhandled exception: %s" % err)
  50. except Exception, log_err:
  51. print "Failed to log error:", log_err
  52. traceback.print_exc()
  53. from src.Config import config
  54. traceback.print_exc(file=open(config.log_dir + "/error.log", "a"))
  55. if main and main.update_after_shutdown: # Updater
  56. # Restart
  57. gc.collect() # Garbage collect
  58. print "Restarting..."
  59. import time
  60. time.sleep(1) # Wait files to close
  61. args = sys.argv[:]
  62. sys.executable = sys.executable.replace(".pkg", "") # Frozen mac fix
  63. if not getattr(sys, 'frozen', False):
  64. args.insert(0, sys.executable)
  65. if sys.platform == 'win32':
  66. args = ['"%s"' % arg for arg in args]
  67. try:
  68. print "Executing %s %s" % (sys.executable, args)
  69. os.execv(sys.executable, args)
  70. except Exception, err:
  71. print "Execv error: %s" % err
  72. print "Bye."
  73. if __name__ == '__main__':
  74. main()