update.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from gevent import monkey; monkey.patch_all()
  2. import urllib, zipfile, os, ssl, httplib, socket, re
  3. import cStringIO as StringIO
  4. def update():
  5. # Gevent https bug workaround (https://github.com/gevent/gevent/issues/477)
  6. reload(socket)
  7. reload(httplib)
  8. reload(ssl)
  9. print "Downloading.",
  10. file = urllib.urlopen("https://github.com/HelloZeroNet/ZeroNet/archive/master.zip")
  11. data = StringIO.StringIO()
  12. while True:
  13. buff = file.read(1024*16)
  14. if not buff: break
  15. data.write(buff)
  16. print ".",
  17. print "Downloaded."
  18. # Checking plugins
  19. plugins_enabled = []
  20. plugins_disabled = []
  21. if os.path.isdir("plugins"):
  22. for dir in os.listdir("plugins"):
  23. if dir.startswith("disabled-"):
  24. plugins_disabled.append(dir.replace("disabled-", ""))
  25. else:
  26. plugins_enabled.append(dir)
  27. print "Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled
  28. print "Extracting...",
  29. zip = zipfile.ZipFile(data)
  30. for inner_path in zip.namelist():
  31. inner_path = inner_path.replace("\\", "/") # Make sure we have unix path
  32. print ".",
  33. dest_path = inner_path.replace("ZeroNet-master/", "")
  34. if not dest_path: continue
  35. # Keep plugin disabled/enabled status
  36. match = re.match("plugins/([^/]+)", dest_path)
  37. if match:
  38. plugin_name = match.group(1).replace("disabled-","")
  39. if plugin_name in plugins_enabled: # Plugin was enabled
  40. dest_path = dest_path.replace("plugins/disabled-"+plugin_name, "plugins/"+plugin_name)
  41. elif plugin_name in plugins_disabled: # Plugin was disabled
  42. dest_path = dest_path.replace("plugins/"+plugin_name, "plugins/disabled-"+plugin_name)
  43. print "P",
  44. dest_dir = os.path.dirname(dest_path)
  45. if dest_dir and not os.path.isdir(dest_dir):
  46. os.makedirs(dest_dir)
  47. if dest_dir != dest_path.strip("/"):
  48. data = zip.read(inner_path)
  49. try:
  50. open(dest_path, 'wb').write(data)
  51. except Exception, err:
  52. print dest_path, err
  53. print "Done."
  54. if __name__ == "__main__":
  55. try:
  56. update()
  57. except Exception, err:
  58. print "Update error: %s" % err
  59. raw_input("Press enter to exit")