From 804aa45fbbaf034b09b0f63ebe75630f02b32052 Mon Sep 17 00:00:00 2001 From: Karl Semich <0xloem@gmail.com> Date: Thu, 9 Jun 2016 18:08:08 -0400 Subject: Convert update.py to use more python --- backend/resilient/__init__.py | 107 ++++++++++++++++++++++++++++++++++++++++++ backend/resilient/freenet.py | 30 ++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 backend/resilient/__init__.py create mode 100644 backend/resilient/freenet.py (limited to 'backend/resilient') 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 + diff --git a/backend/resilient/freenet.py b/backend/resilient/freenet.py new file mode 100644 index 0000000..ea72760 --- /dev/null +++ b/backend/resilient/freenet.py @@ -0,0 +1,30 @@ +from resilient import auth_path, git, ikiwiki, url200 +import os + +running = url200('http://127.0.0.1:8888/') +if not running: + print("WARNING: freenet not running, changes won't be synced with freenet") + +class gitocalypse(git): + found = (os.system("git-remote-freenet") == 256) + if not found: + print("WARNING: gitocalypse not found, changes won't be synced with freenet") + + def __init__(self, pull_remote, push_remote = None): + self.remote = pull_remote + self.push_remote = push_remote + +class freesitemgr(): + found = (os.system("freesitemgr --version") == 0) + if not found: + print("WARNING: freesitemgr/pyFreenet not found, public freesite won't be updated") + + def __init__(self): + pass + + def push(self): + if not ikiwiki.synced: + return False + config_path = os.path.join(auth_path, 'freesitemgr') + status = os.system("freesitemgr -c '%s' -v -r 1 update" % config_path) + return status == 0 -- cgit v1.2.3