summaryrefslogtreecommitdiff
path: root/backend/resilient/__init__.py
blob: 490ff3fb80e88ea3b49d770befd8519109180e89 (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
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