diff options
Diffstat (limited to 'backend/resilient/__init__.py')
-rw-r--r-- | backend/resilient/__init__.py | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/backend/resilient/__init__.py b/backend/resilient/__init__.py new file mode 100644 index 0000000..490ff3f --- /dev/null +++ b/backend/resilient/__init__.py @@ -0,0 +1,107 @@ +import os, urllib + +__all__ = ["freenet"] + +name = 'A Wiki for Targeted Individuals' + +backend_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..')) + +auth_path = os.path.join(backend_path, 'auth') +html_path = os.path.normpath(os.path.join(backend_path, '../html')) +wiki_src_path = os.path.normpath(os.path.join(backend_path, '../wiki')) +git_path = os.path.normpath(os.path.join(backend_path, '../.git')) + + +ssh_config_path = os.path.join(auth_path, "ssh-config") + +def url200(url): + try: + return urllib.urlopen(url).getcode() == 200 + except IOError: + return False + +class git: + found = (os.system("git status --porcelain") == 0) + if not found: + print("ERROR: git not found") + exit(1) + + def __init__(self, remote, push_remote = None): + self.remote = remote + if push_remote == None: + push_remote = remote + self.push_remote = push_remote + + def pull(self): + if self.remote == None: + return False + os.environ["GIT_SSH_COMMAND"] = "ssh -F %s" % ssh_config_path + status = os.system("git pull %s master" % self.remote) + del os.environ["GIT_SSH_COMMAND"] + return status == 0 + + def push(self): + if self.push_remote == None: + return False + os.environ["GIT_SSH_COMMAND"] = "ssh -F %s" % ssh_config_path + status = os.system("git push %s master" % self.push_remote) + del os.environ["GIT_SSH_COMMAND"] + return status == 0 + + +class ikiwiki: + found = (os.system("ikiwiki --version") == 0) + if not found: + print("WARNING: ikiwiki not found, pages will not be built") + + synced = False + + def push(self): + if not ikiwiki.found: + return False + + if ikiwiki.synced: + return True + + html_git_path = os.path.join(html_path, '.git') + os.system("rm -rf '%s'" % html_path) + os.system("mkdir -p '%s'" % html_git_path) + + status = os.system("ikiwiki --wikiname '%s' --rebuild '%s' '%s'" % (name.replace("'","'\"'\"'"), wiki_src_path, html_path)) + + ikiwiki.synced = status == 0 + if ikiwiki.synced: + os.system('git gc') + os.system('git update-server-info') + for path in ['objects', 'refs', 'HEAD', 'info', 'packed-refs']: + os.system("cp -r '%s' '%s'" % (os.path.join(git_path, path), html_git_path)) + return ikiwiki.synced + + +class zeronet(): + running = url200('http://127.0.0.1:43110/1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D') + if not running: + print("WARNING: ZeroNet not running, won't be used") + + found = 'ZERONETDIR' in os.environ + if found: + zn_datadir = os.path.join(os.environ['ZERONETDIR'], 'ZeroNet/data') + zn_script = os.path.join(os.environ['ZERONETDIR'], 'ZeroNet.sh') + found = (os.system("'%s' --version" % zn_script) == 0) + if not found: + print("WARNING: ZeroNet bundle not found in ZERONETDIR, public zeronet site won't be updated") + + def __init__(self, addr, key): + self.addr = addr + self.key = key + + def push(self): + if not ikiwiki.synced: + return False + zitedir = os.path.join(zeronet.zn_datadir, self.addr) + status = os.system('cp -r %s %s' % (os.path.join(html_path, '.'), os.path.join(zitedir, '.'))) + if status != 0: + return False + status = os.system('%s siteSign %s %s' % (zeronet.zn_script, self.addr, self.key)) + return status == 0 + |