update.py 2.4 KB

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