update.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import urllib
  2. import zipfile
  3. import os
  4. import sys
  5. import ssl
  6. import httplib
  7. import socket
  8. import re
  9. import json
  10. import cStringIO as StringIO
  11. def download():
  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. return zipdata
  42. def update():
  43. from Config import config
  44. if getattr(sys, 'source_update_dir', False):
  45. if not os.path.isdir(sys.source_update_dir):
  46. os.makedirs(sys.source_update_dir)
  47. os.chdir(sys.source_update_dir) # New source code will be stored in different directory
  48. updatesite_path = config.data_dir + "/" + config.updatesite
  49. sites_json = json.load(open(config.data_dir + "/sites.json"))
  50. updatesite_bad_files = sites_json.get(config.updatesite, {}).get("cache", {}).get("bad_files", {})
  51. print "Update site path: %s, bad_files: %s" % (updatesite_path, len(updatesite_bad_files))
  52. if os.path.isfile(updatesite_path + "/content.json") and len(updatesite_bad_files) == 0 and sites_json.get(config.updatesite, {}).get("serving"):
  53. # Update site exists and no broken file
  54. print "Updating using site %s" % config.updatesite
  55. zipdata = False
  56. inner_paths = json.load(open(updatesite_path + "/content.json"))["files"].keys()
  57. # Keep file only in ZeroNet directory
  58. inner_paths = [inner_path for inner_path in inner_paths if inner_path.startswith("ZeroNet/")]
  59. else:
  60. # Fallback to download
  61. zipdata = download()
  62. inner_paths = zipdata.namelist()
  63. # Checking plugins
  64. plugins_enabled = []
  65. plugins_disabled = []
  66. if os.path.isdir("plugins"):
  67. for dir in os.listdir("plugins"):
  68. if dir.startswith("disabled-"):
  69. plugins_disabled.append(dir.replace("disabled-", ""))
  70. else:
  71. plugins_enabled.append(dir)
  72. print "Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled
  73. print "Extracting to %s..." % os.getcwd(),
  74. for inner_path in inner_paths:
  75. if ".." in inner_path:
  76. continue
  77. inner_path = inner_path.replace("\\", "/") # Make sure we have unix path
  78. print ".",
  79. dest_path = re.sub("^([^/]*-master.*?|ZeroNet)/", "", inner_path) # Skip root zeronet-master-... like directories
  80. dest_path = dest_path.lstrip("/")
  81. if not dest_path:
  82. continue
  83. # Keep plugin disabled/enabled status
  84. match = re.match("plugins/([^/]+)", dest_path)
  85. if match:
  86. plugin_name = match.group(1).replace("disabled-", "")
  87. if plugin_name in plugins_enabled: # Plugin was enabled
  88. dest_path = dest_path.replace("plugins/disabled-" + plugin_name, "plugins/" + plugin_name)
  89. elif plugin_name in plugins_disabled: # Plugin was disabled
  90. dest_path = dest_path.replace("plugins/" + plugin_name, "plugins/disabled-" + plugin_name)
  91. print "P",
  92. dest_dir = os.path.dirname(dest_path)
  93. if dest_dir and not os.path.isdir(dest_dir):
  94. os.makedirs(dest_dir)
  95. if dest_dir != dest_path.strip("/"):
  96. if zipdata:
  97. data = zipdata.read(inner_path)
  98. else:
  99. data = open(updatesite_path + "/" + inner_path, "rb").read()
  100. try:
  101. open(dest_path, 'wb').write(data)
  102. except Exception, err:
  103. print dest_path, err
  104. print "Done."
  105. return True
  106. if __name__ == "__main__":
  107. import sys
  108. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) # Imports relative to src
  109. from gevent import monkey
  110. monkey.patch_all()
  111. from Config import config
  112. config.parse(silent=True)
  113. from src.util import SslPatch
  114. try:
  115. update()
  116. except Exception, err:
  117. print "Update error: %s" % err
  118. raw_input("Press enter to exit")