update.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import urllib
  2. import zipfile
  3. import os
  4. import ssl
  5. import httplib
  6. import socket
  7. import re
  8. import cStringIO as StringIO
  9. from gevent import monkey
  10. monkey.patch_all()
  11. def update():
  12. from src.util import helper
  13. urls = [
  14. "https://github.com/HelloZeroNet/ZeroNet/archive/master.zip",
  15. "https://gitlab.com/HelloZeroNet/ZeroNet/repository/archive.zip?ref=master",
  16. "https://try.gogs.io/ZeroNet/ZeroNet/archive/master.zip"
  17. ]
  18. zipdata = None
  19. for url in urls:
  20. print "Downloading from:", url,
  21. try:
  22. req = helper.httpRequest(url)
  23. data = StringIO.StringIO()
  24. while True:
  25. buff = req.read(1024 * 16)
  26. if not buff:
  27. break
  28. data.write(buff)
  29. print ".",
  30. try:
  31. zipdata = zipfile.ZipFile(data)
  32. break # Success
  33. except Exception, err:
  34. data.seek(0)
  35. print "Unpack error", err, data.read(256)
  36. except Exception, err:
  37. print "Error downloading update from %s: %s" % (url, err)
  38. if not zipdata:
  39. raise err
  40. print "Downloaded."
  41. # Checking plugins
  42. plugins_enabled = []
  43. plugins_disabled = []
  44. if os.path.isdir("plugins"):
  45. for dir in os.listdir("plugins"):
  46. if dir.startswith("disabled-"):
  47. plugins_disabled.append(dir.replace("disabled-", ""))
  48. else:
  49. plugins_enabled.append(dir)
  50. print "Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled
  51. print "Extracting...",
  52. for inner_path in zipdata.namelist():
  53. if ".." in inner_path:
  54. continue
  55. inner_path = inner_path.replace("\\", "/") # Make sure we have unix path
  56. print ".",
  57. dest_path = re.sub("^[^/]*-master.*?/", "", inner_path) # Skip root zeronet-master-... like directories
  58. dest_path = dest_path.lstrip("/")
  59. if not dest_path:
  60. continue
  61. # Keep plugin disabled/enabled status
  62. match = re.match("plugins/([^/]+)", dest_path)
  63. if match:
  64. plugin_name = match.group(1).replace("disabled-", "")
  65. if plugin_name in plugins_enabled: # Plugin was enabled
  66. dest_path = dest_path.replace("plugins/disabled-" + plugin_name, "plugins/" + plugin_name)
  67. elif plugin_name in plugins_disabled: # Plugin was disabled
  68. dest_path = dest_path.replace("plugins/" + plugin_name, "plugins/disabled-" + plugin_name)
  69. print "P",
  70. dest_dir = os.path.dirname(dest_path)
  71. if dest_dir and not os.path.isdir(dest_dir):
  72. os.makedirs(dest_dir)
  73. if dest_dir != dest_path.strip("/"):
  74. data = zipdata.read(inner_path)
  75. try:
  76. open(dest_path, 'wb').write(data)
  77. except Exception, err:
  78. print dest_path, err
  79. print "Done."
  80. return True
  81. if __name__ == "__main__":
  82. # Fix broken gevent SSL
  83. import sys
  84. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) # Imports relative to src
  85. from Config import config
  86. config.parse()
  87. from src.util import SslPatch
  88. try:
  89. update()
  90. except Exception, err:
  91. print "Update error: %s" % err
  92. raw_input("Press enter to exit")