summaryrefslogtreecommitdiff
path: root/backend/resilient/bsv_metanet.py
blob: 3d21b3ebd2f1a100728e5751d60568e79a4ba805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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