Rev1761, Update ZeroNet from ZeroNet network

This commit is contained in:
shortcutme 2016-12-04 18:57:08 +01:00
parent c69f70bdb0
commit b66317fa7d
2 changed files with 33 additions and 6 deletions

View file

@ -5,13 +5,13 @@ import ssl
import httplib
import socket
import re
import json
import cStringIO as StringIO
from gevent import monkey
monkey.patch_all()
def update():
def download():
from src.util import helper
urls = [
@ -46,6 +46,27 @@ def update():
print "Downloaded."
return zipdata
def update():
from Config import config
updatesite_path = config.data_dir + "/" + config.updatesite
sites_json = json.load(open(config.data_dir + "/sites.json"))
updatesite_bad_files = sites_json.get(config.updatesite, {}).get("cache", {}).get("bad_files", {})
print "Update site path: %s, bad_files: %s" % (updatesite_path, len(updatesite_bad_files))
if os.path.isfile(updatesite_path + "/content.json") and len(updatesite_bad_files) == 0 and sites_json.get(config.updatesite, {}).get("serving"):
# Update site exists and no broken file
print "Updating using site %s" % config.updatesite
zipdata = False
inner_paths = json.load(open(updatesite_path + "/content.json"))["files"].keys()
# Keep file only in ZeroNet directory
inner_paths = [inner_path for inner_path in inner_paths if inner_path.startswith("ZeroNet/")]
else:
# Fallback to download
zipdata = download()
inner_paths = zipdata.namelist()
# Checking plugins
plugins_enabled = []
plugins_disabled = []
@ -58,12 +79,12 @@ def update():
print "Plugins enabled:", plugins_enabled, "disabled:", plugins_disabled
print "Extracting...",
for inner_path in zipdata.namelist():
for inner_path in inner_paths:
if ".." in inner_path:
continue
inner_path = inner_path.replace("\\", "/") # Make sure we have unix path
print ".",
dest_path = re.sub("^[^/]*-master.*?/", "", inner_path) # Skip root zeronet-master-... like directories
dest_path = re.sub("^([^/]*-master.*?|ZeroNet)/", "", inner_path) # Skip root zeronet-master-... like directories
dest_path = dest_path.lstrip("/")
if not dest_path:
continue
@ -84,7 +105,11 @@ def update():
os.makedirs(dest_dir)
if dest_dir != dest_path.strip("/"):
data = zipdata.read(inner_path)
if zipdata:
data = zipdata.read(inner_path)
else:
data = open(updatesite_path + "/" + inner_path).read()
try:
open(dest_path, 'wb').write(data)
except Exception, err: