From b52aedc76701b3e42acdb34ad2513b119dbaf7e0 Mon Sep 17 00:00:00 2001
From: IkiWiki <ikiwiki.info>
Date: Mon, 2 Sep 2019 12:54:51 -0400
Subject: work towards referencing bsv

---
 backend/resilient/__init__.py    | 30 +++++++++++++++++++--------
 backend/resilient/bsv_metanet.py | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 8 deletions(-)
 create mode 100644 backend/resilient/bsv_metanet.py

(limited to 'backend/resilient')

diff --git a/backend/resilient/__init__.py b/backend/resilient/__init__.py
index dfab073..73e375c 100644
--- a/backend/resilient/__init__.py
+++ b/backend/resilient/__init__.py
@@ -4,7 +4,6 @@
 	# git saves history
 	# ikiwiki connects git to online presentation and collaboration
 
-import fileinput
 import os
 import shlex
 import shutil
@@ -14,7 +13,7 @@ import tempfile
 import time
 import urllib
 
-__all__ = ["freenet"]
+__all__ = ["freenet", "bsv_metanet"]
 
 backend_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
 root_path = os.path.normpath(os.path.join(backend_path, '..'))
@@ -44,11 +43,14 @@ class git:
   found = (os.system("git status --porcelain") == 0)
   if not found:
     print("ERROR: git not found")
-    # TODO; download git, place in dep_path, and run from there
+    # TODO: download git, place in dep_path, and run from there
     exit(1)
 
+  orig_dir = os.path.abspath(".")
+  os.chdir(root_path)
   os.system("git submodule init")
   os.system("git submodule update")
+  os.chdir(orig_dir)
 
   # http://stackoverflow.com/questions/1964470/whats-the-equivalent-of-use-commit-times-for-git
   # sets mtimes to last commit time, for ikiwiki 'last edited by' time
@@ -100,10 +102,18 @@ class git:
 
 
 class ikiwiki:
-  found = (os.system("ikiwiki --version") == 0)
+  binary = "ikiwiki"
+  found = (os.system("%s --version" % binary) == 0)
   if not found:
-    print("WARNING: ikiwiki not found.  please add code to fetch and build the backend/dep/ikiwiki submodule.")
-  
+    binary = "%s/ikiwiki/ikiwiki.out" % dep_path
+    os.environ['PERL5LIB'] += os.pathsep + os.path.join(dep_path, 'ikiwiki')
+    found = (os.system("%s --version" % binary) == 0)
+  if not found:
+    print("ikiwiki not found: attempting to build")
+    os.system("%s/build_ikiwiki.sh" % dep_path)
+    found = (os.system("%s --version" % binary) == 0)
+  if not found:
+    print("WARNING: ikiwiki not found.  did build fail on this platform?")
   synced = False
 
   def push(self):
@@ -116,13 +126,17 @@ class ikiwiki:
     shutil.rmtree(html_path)
     os.system("git checkout -- '%s'" % html_path)
 
-    status = os.system("ikiwiki --setup '%s' '%s' '%s'" % (ikiwiki_setup_path, wiki_src_path, html_path))
+    status = os.system("%s --setup '%s' '%s' '%s'" % (ikiwiki.binary, ikiwiki_setup_path, 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')
+
+      # /.git folder access can be seen as security access problem by web hosts, so alias to /git
+      with open(os.path.join(html_path, ".git"), "w") as f: f.write("gitdir: ./git")
+
+      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)
diff --git a/backend/resilient/bsv_metanet.py b/backend/resilient/bsv_metanet.py
new file mode 100644
index 0000000..3d21b3e
--- /dev/null
+++ b/backend/resilient/bsv_metanet.py
@@ -0,0 +1,44 @@
+from resilient import html_path, auth_path, dep_path, git, ikiwiki
+import os
+import subprocess
+import time
+
+# TODO: need to connect to a local node, not a web service
+
+class bsvup():
+  binary = "bsvup"
+  found = (os.system("%s" % binary) == 0)
+  # TODO: check dep_path for alternate location for binary
+  if not found:
+    print("WARNING: bsvup not found.  bsv metanet won't be updated")
+
+  def __init__(self, keyfile):
+    self.keyfile = os.path.join(auth_path, keyfile)
+    with open(self.keyfile) as f: self.privkey = f.read()
+    self.configdir = self.keyfile + '.bsv'
+    if bsvup.found:
+      if not os.path.isdir(self.configdir):
+        os.mkdir(self.configdir)
+        os.symlink(".", os.path.join(self.configdir, ".bsv"))
+        proc = subprocess.Popen([bsvup.binary, 'init'], stdin=subprocess.PIPE, cwd=self.configdir)
+        proc.stdin.write(self.privkey + "\n")
+        time.sleep(1)
+        proc.communicate("\n")
+        open(os.path.join(self.configdir, "info", ".keepme"), "a").close()
+        open(os.path.join(self.configdir, "objects", ".keepme"), "a").close()
+        open(os.path.join(self.configdir, "tx", ".keepme"), "a").close()
+      else: # resume tx if needed
+        proc = subprocess.Popen([bsvup.binary], stdin=subprocess.PIPE, cwd=self.configdir)
+        proc.communicate("Y\n")
+
+  def push(self):
+    if not ikiwiki.synced or not bsvup.found:
+      return False
+
+    # add files to bsv metanet
+    orig_dir = os.path.abspath(".")
+    proc = subprocess.Popen([bsvup.binary, '-f', html_path, 'upload'], stdin=subprocess.PIPE, cwd=self.configdir)
+    proc.stdin.write("\n")
+    time.sleep(1)
+    proc.stdin.write("Y\n")
+    return proc.wait() == 0
-- 
cgit v1.2.3


From 23f867762d8482ac16fc351b1529f45880a740b9 Mon Sep 17 00:00:00 2001
From: IkiWiki <ikiwiki.info>
Date: Mon, 2 Sep 2019 13:24:22 -0400
Subject: update git packing

---
 backend/resilient/__init__.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

(limited to 'backend/resilient')

diff --git a/backend/resilient/__init__.py b/backend/resilient/__init__.py
index 73e375c..ead6576 100644
--- a/backend/resilient/__init__.py
+++ b/backend/resilient/__init__.py
@@ -130,7 +130,8 @@ class ikiwiki:
 
     ikiwiki.synced = (status == 0)
     if ikiwiki.synced:
-      os.system('git gc')
+      os.system('git repack -d')
+      os.system('git prune-packed')
       os.system('git update-server-info')
 
       # /.git folder access can be seen as security access problem by web hosts, so alias to /git
@@ -138,7 +139,7 @@ class ikiwiki:
 
       html_git_path = os.path.join(html_path, 'git')
       os.mkdir(html_git_path)
-      for path in ['objects', 'refs', 'HEAD', 'info', 'packed-refs']:
+      for path in ['objects/info', 'objects/packs', '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):
-- 
cgit v1.2.3


From 1e6e19dad98f517152aeba4e4f60a87a7731d648 Mon Sep 17 00:00:00 2001
From: IkiWiki <ikiwiki.info>
Date: Mon, 2 Sep 2019 13:26:55 -0400
Subject: fix typo

---
 backend/resilient/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'backend/resilient')

diff --git a/backend/resilient/__init__.py b/backend/resilient/__init__.py
index ead6576..d44e914 100644
--- a/backend/resilient/__init__.py
+++ b/backend/resilient/__init__.py
@@ -139,7 +139,7 @@ class ikiwiki:
 
       html_git_path = os.path.join(html_path, 'git')
       os.mkdir(html_git_path)
-      for path in ['objects/info', 'objects/packs', 'refs', 'HEAD', 'info', 'packed-refs']:
+      for path in ['objects/info', 'objects/pack', '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):
-- 
cgit v1.2.3


From 258fc3d7f7ba7983dcd3dac1429936227233eaf2 Mon Sep 17 00:00:00 2001
From: IkiWiki <ikiwiki.info>
Date: Mon, 2 Sep 2019 13:51:05 -0400
Subject: fix ssh behavior on old ssh versions

---
 backend/dep/build_ikiwiki.sh  |  2 +-
 backend/resilient/__init__.py | 14 +++++---------
 backend/wrapped_ssh.sh        |  6 ++++++
 3 files changed, 12 insertions(+), 10 deletions(-)
 create mode 100755 backend/wrapped_ssh.sh

(limited to 'backend/resilient')

diff --git a/backend/dep/build_ikiwiki.sh b/backend/dep/build_ikiwiki.sh
index 38d6692..3635e40 100755
--- a/backend/dep/build_ikiwiki.sh
+++ b/backend/dep/build_ikiwiki.sh
@@ -1,6 +1,6 @@
 #!/bin/env bash
 
-SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+SCRIPTDIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
 
 cd "$SCRIPTDIR"/ikiwiki &&
 { PERL5LIB=`pwd` PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'CPAN::Shell->notest("install", "Bundle::IkiWiki")'; } &&
diff --git a/backend/resilient/__init__.py b/backend/resilient/__init__.py
index d44e914..e2b40c7 100644
--- a/backend/resilient/__init__.py
+++ b/backend/resilient/__init__.py
@@ -25,7 +25,7 @@ html_path = os.path.normpath(os.path.join(root_path, 'html'))
 wiki_src_path = os.path.normpath(os.path.join(root_path, 'wiki'))
 git_path = os.path.normpath(os.path.join(root_path, '.git'))
 
-ssh_config_path = os.path.join(auth_path, "ssh-config")
+ssh_wrap_path = os.path.join(backend_path, "wrapped_ssh.sh")
 
 for auth_subpath, auth_subpaths, auth_subfiles in os.walk(auth_path):
   for auth_subfile in auth_subfiles:
@@ -83,21 +83,17 @@ class git:
   def pull(self):
     if self.remote == None:
       return False
-    # TODO: GIT_SSH_COMMAND needs new version of git
-    # to make it work on old versions, path ssh_config_path on environment
-    # and use a wrapper script that calls ssh -F
-    # then place path to wrapper script in os.environ["GIT_SSH"]
-    os.environ["GIT_SSH_COMMAND"] = "ssh -F %s" % ssh_config_path
+    os.environ["GIT_SSH"] = ssh_wrap_path
     status = os.system("git pull %s master" % self.remote)
-    del os.environ["GIT_SSH_COMMAND"]
+    del os.environ["GIT_SSH"]
     return status == 0
 
   def push(self):
     if self.push_remote == None:
       return False
-    os.environ["GIT_SSH_COMMAND"] = "ssh -F %s" % ssh_config_path
+    os.environ["GIT_SSH"] = ssh_wrap_path
     status = os.system("git push %s master" % self.push_remote)
-    del os.environ["GIT_SSH_COMMAND"]
+    del os.environ["GIT_SSH"]
     return status == 0
 
 
diff --git a/backend/wrapped_ssh.sh b/backend/wrapped_ssh.sh
new file mode 100755
index 0000000..8281e9d
--- /dev/null
+++ b/backend/wrapped_ssh.sh
@@ -0,0 +1,6 @@
+#!/bin/env sh
+
+SCRIPTDIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
+
+ssh -F "$SCRIPTDIR"/auth/ssh-config "$@"
+
-- 
cgit v1.2.3