summaryrefslogtreecommitdiff
path: root/backend/resilient/__init__.py
blob: 21f2d85c1faa4b57b3869c0fb87dd7f640408ffc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import fileinput
import os
import shutil
import subprocess
import tempfile
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")
  else:
    found = (os.system('ipns-pub --version') == 0)
    if not found:
      print ("WARNING: ipns-pub not found, ipfs content won't be updated")

  synced = False

  def __init__(self, keyfile):
    self.keyfile = keyfile
  
  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', '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(['ipns-pub', '-key=%s' % self.keyfile, ipfs.hash])
    status = proc.wait()
    if status != 0:
      print 'NOTE: ipns-pub failed to run under python as is common, try running by hand.'
      print 'CMDLINE: ipns-pub -key=%s %s' % (self.keyfile, ipfs.hash)
    return status == 0