Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import os
2import sys
3import json
4import re
5import shutil
6
7
8def update():
9 from Config import config
10 config.parse(silent=True)
11
12 if getattr(sys, 'source_update_dir', False):
13 if not os.path.isdir(sys.source_update_dir):
14 os.makedirs(sys.source_update_dir)
15 source_path = sys.source_update_dir.rstrip("/")
16 else:
17 source_path = os.getcwd().rstrip("/")
18
19 if config.dist_type.startswith("bundle_linux"):
20 runtime_path = os.path.normpath(os.path.dirname(sys.executable) + "/../..")
21 else:
22 runtime_path = os.path.dirname(sys.executable)
23
24 updatesite_path = config.data_dir + "/" + config.updatesite
25
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
33 updatesite_content_json = json.load(open(updatesite_path + "/content.json"))
34 inner_paths = list(updatesite_content_json.get("files", {}).keys())
35 inner_paths += list(updatesite_content_json.get("files_optional", {}).keys())
36
37 # Keep file only in ZeroNet directory
38 inner_paths = [inner_path for inner_path in inner_paths if re.match("^(core|bundle)", inner_path)]
39
40 # Checking plugins
41 plugins_enabled = []
42 plugins_disabled = []
43 if os.path.isdir("%s/plugins" % source_path):
44 for dir in os.listdir("%s/plugins" % source_path):
45 if dir.startswith("disabled-"):
46 plugins_disabled.append(dir.replace("disabled-", ""))
47 else:
48 plugins_enabled.append(dir)
49 print("Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled)
50
51 update_paths = {}
52
53 for inner_path in inner_paths:
54 if ".." in inner_path:
55 continue
56 inner_path = inner_path.replace("\\", "/").strip("/") # Make sure we have unix path
57 print(".", end=" ")
58 if inner_path.startswith("core"):
59 dest_path = source_path + "/" + re.sub("^core/", "", inner_path)
60 elif inner_path.startswith(config.dist_type):
61 dest_path = runtime_path + "/" + re.sub("^bundle[^/]+/", "", inner_path)
62 else:
63 continue
64
65 if not dest_path:
66 continue
67
68 # Keep plugin disabled/enabled status
69 match = re.match(re.escape(source_path) + "/plugins/([^/]+)", dest_path)
70 if match:
71 plugin_name = match.group(1).replace("disabled-", "")
72 if plugin_name in plugins_enabled: # Plugin was enabled
73 dest_path = dest_path.replace("plugins/disabled-" + plugin_name, "plugins/" + plugin_name)
74 elif plugin_name in plugins_disabled: # Plugin was disabled
75 dest_path = dest_path.replace("plugins/" + plugin_name, "plugins/disabled-" + plugin_name)
76 print("P", end=" ")
77
78 dest_dir = os.path.dirname(dest_path)
79 if dest_dir and not os.path.isdir(dest_dir):
80 os.makedirs(dest_dir)
81
82 if dest_dir != dest_path.strip("/"):
83 update_paths[updatesite_path + "/" + inner_path] = dest_path
84
85 num_ok = 0
86 num_rename = 0
87 num_error = 0
88 for path_from, path_to in update_paths.items():
89 print("-", path_from, "->", path_to)
90 if not os.path.isfile(path_from):
91 print("Missing file")
92 continue
93
94 data = open(path_from, "rb").read()
95
96 try:
97 open(path_to, 'wb').write(data)
98 num_ok += 1
99 except Exception as err:
100 try:
101 print("Error writing: %s. Renaming old file as workaround..." % err)
102 path_to_tmp = path_to + "-old"
103 if os.path.isfile(path_to_tmp):
104 os.unlink(path_to_tmp)
105 os.rename(path_to, path_to_tmp)
106 num_rename += 1
107 open(path_to, 'wb').write(data)
108 shutil.copymode(path_to_tmp, path_to) # Copy permissions
109 print("Write done after rename!")
110 num_ok += 1
111 except Exception as err:
112 print("Write error after rename: %s" % err)
113 num_error += 1
114 print("* Updated files: %s, renamed: %s, error: %s" % (num_ok, num_rename, num_error))
115
116
117if __name__ == "__main__":
118 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) # Imports relative to src
119
120 update()