import fileinput import os import shutil import subprocess import tempfile import time import urllib __all__ = ["freenet"] ## Set to change default title 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") for auth_subpath, auth_subpaths, auth_subfiles in os.walk(auth_path): for auth_subfile in auth_subfiles: auth_subfile = os.path.join(auth_subpath, auth_subfile) if os.path.isfile(auth_subfile): os.chmod(auth_subfile, 0600) 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 = ""): self.remote = remote if push_remote == "": 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 shutil.rmtree(html_path) os.mkdir(html_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') html_git_path = os.path.join(html_path, '.git') os.mkdir(html_git_path) for path in ['objects', 'refs', 'HEAD', 'info', 'packed-refs']: git_subpath = os.path.join(git_path, path) html_git_subpath = os.path.join(html_git_path, path) if os.path.isdir(git_subpath): shutil.copytree(git_subpath, html_git_subpath) else: shutil.copy2(git_subpath, html_git_subpath) 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 or not zeronet.found: return False zitedir = os.path.join(zeronet.zn_datadir, self.addr) shutil.rmtree(zitedir, True) shutil.copytree(html_path, zitedir) status = os.system('%s siteSign %s %s' % (zeronet.zn_script, self.addr, self.key)) return status == 0 class ipfs: found = (os.system('ipfs version') == 0) if not found: print("WARNING: ipfs not found, ipfs content won't be updated") synced = False def __init__(self, configdir): self.configdir = os.path.join(auth_path, configdir) subprocess.Popen(['ipfs', '-c', self.configdir, 'daemon']) while self._config('Identity.PeerID') == None: time.sleep(0.5) self.peerid = self._config('Identity.PeerID') self.gateway = self._config('Addresses.Gateway').replace('/ip4','http:/').replace('/tcp/',':') def _config(self, key): proc = subprocess.Popen(['ipfs', '-c', self.configdir, 'config', key], stdout=subprocess.PIPE) for line in proc.stdout: return line[0:-1] def push(self): if not ikiwiki.synced or not ipfs.found: return False # add files to ipfs if not ipfs.synced: proc = subprocess.Popen(['ipfs', '-c', self.configdir, 'add', '-rH', html_path], stdout=subprocess.PIPE) for line in proc.stdout: print line[0:-1] ipfs.hash = line[6:52] ipfs.synced = True # publish hash to private key proc = subprocess.Popen(['ipfs', '-c', self.configdir, 'name', 'publish', ipfs.hash], stdout=subprocess.PIPE) for line in proc.stdout: print line[0:-1] self.peerid = line[13:59] status = proc.wait() return status == 0