update.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import os
  2. import sys
  3. import json
  4. import re
  5. import shutil
  6. def update():
  7. print('please update zeronet-conservancy via git. usually it can be done via single commnad')
  8. print(' git pull')
  9. print('although it depends on your branches setup')
  10. print('updating through 1update site is not considered safe at the moment')
  11. print('if you really want to use it, edit this file')
  12. return False
  13. from Config import config
  14. config.parse(silent=True)
  15. if getattr(sys, 'source_update_dir', False):
  16. if not os.path.isdir(sys.source_update_dir):
  17. os.makedirs(sys.source_update_dir)
  18. source_path = sys.source_update_dir.rstrip("/")
  19. else:
  20. source_path = os.getcwd().rstrip("/")
  21. if config.dist_type.startswith("bundle_linux"):
  22. runtime_path = os.path.normpath(os.path.dirname(sys.executable) + "/../..")
  23. else:
  24. runtime_path = os.path.dirname(sys.executable)
  25. updatesite_path = config.data_dir + "/" + config.updatesite
  26. sites_json = json.load(open(config.data_dir + "/sites.json"))
  27. updatesite_bad_files = sites_json.get(config.updatesite, {}).get("cache", {}).get("bad_files", {})
  28. print(
  29. "Update site path: %s, bad_files: %s, source path: %s, runtime path: %s, dist type: %s" %
  30. (updatesite_path, len(updatesite_bad_files), source_path, runtime_path, config.dist_type)
  31. )
  32. updatesite_content_json = json.load(open(updatesite_path + "/content.json"))
  33. inner_paths = list(updatesite_content_json.get("files", {}).keys())
  34. inner_paths += list(updatesite_content_json.get("files_optional", {}).keys())
  35. # Keep file only in ZeroNet directory
  36. inner_paths = [inner_path for inner_path in inner_paths if re.match("^(core|bundle)", inner_path)]
  37. # Checking plugins
  38. plugins_enabled = []
  39. plugins_disabled = []
  40. if os.path.isdir("%s/plugins" % source_path):
  41. for dir in os.listdir("%s/plugins" % source_path):
  42. if dir.startswith("disabled-"):
  43. plugins_disabled.append(dir.replace("disabled-", ""))
  44. else:
  45. plugins_enabled.append(dir)
  46. print("Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled)
  47. update_paths = {}
  48. for inner_path in inner_paths:
  49. if ".." in inner_path:
  50. continue
  51. inner_path = inner_path.replace("\\", "/").strip("/") # Make sure we have unix path
  52. print(".", end=" ")
  53. if inner_path.startswith("core"):
  54. dest_path = source_path + "/" + re.sub("^core/", "", inner_path)
  55. elif inner_path.startswith(config.dist_type):
  56. dest_path = runtime_path + "/" + re.sub("^bundle[^/]+/", "", inner_path)
  57. else:
  58. continue
  59. if not dest_path:
  60. continue
  61. # Keep plugin disabled/enabled status
  62. match = re.match(re.escape(source_path) + "/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", end=" ")
  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. update_paths[updatesite_path + "/" + inner_path] = dest_path
  75. num_ok = 0
  76. num_rename = 0
  77. num_error = 0
  78. for path_from, path_to in update_paths.items():
  79. print("-", path_from, "->", path_to)
  80. if not os.path.isfile(path_from):
  81. print("Missing file")
  82. continue
  83. data = open(path_from, "rb").read()
  84. try:
  85. open(path_to, 'wb').write(data)
  86. num_ok += 1
  87. except Exception as err:
  88. try:
  89. print("Error writing: %s. Renaming old file as workaround..." % err)
  90. path_to_tmp = path_to + "-old"
  91. if os.path.isfile(path_to_tmp):
  92. os.unlink(path_to_tmp)
  93. os.rename(path_to, path_to_tmp)
  94. num_rename += 1
  95. open(path_to, 'wb').write(data)
  96. shutil.copymode(path_to_tmp, path_to) # Copy permissions
  97. print("Write done after rename!")
  98. num_ok += 1
  99. except Exception as err:
  100. print("Write error after rename: %s" % err)
  101. num_error += 1
  102. print("* Updated files: %s, renamed: %s, error: %s" % (num_ok, num_rename, num_error))
  103. if __name__ == "__main__":
  104. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) # Imports relative to src
  105. update()