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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# this is a library to keep a shared repository with many forms of backups
# atm, git and ikiwiki are required, but it should be reasonable to make other approaches
# git saves history
# ikiwiki connects git to online presentation and collaboration
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
import time
import urllib
__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, '..'))
auth_path = os.path.join(backend_path, 'auth')
dep_path = os.path.join(backend_path, 'dep')
ikiwiki_setup_path = os.path.join(backend_path, 'ikiwiki.setup')
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_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:
auth_subfile = os.path.join(auth_subpath, auth_subfile)
if os.path.isfile(auth_subfile):
os.chmod(auth_subfile, 0o600)
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")
# 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
filelist = set()
for root, subdirs, files in os.walk(wiki_src_path):
for file in files:
filelist.add(os.path.relpath(os.path.join(root, file), root_path))
mtime = 0
gitobj = subprocess.Popen(shlex.split('git whatchanged --pretty=%at'), stdout=subprocess.PIPE)
for line in gitobj.stdout:
line = line.strip()
if not line: continue
if line.startswith(':'):
file = line.split('\t')[-1]
if file in filelist:
filelist.remove(file)
#print mtime, file
os.utime(os.path.join(root_path,file), (mtime, mtime))
else:
mtime = long(line)
if not filelist:
break
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"] = ssh_wrap_path
status = os.system("git pull %s master" % self.remote)
del os.environ["GIT_SSH"]
return status == 0
def push(self):
if self.push_remote == None:
return False
os.environ["GIT_SSH"] = ssh_wrap_path
status = os.system("git push %s master" % self.push_remote)
del os.environ["GIT_SSH"]
return status == 0
class ikiwiki:
binary = "ikiwiki"
found = (os.system("%s --version" % binary) == 0)
if not found:
binary = "%s/ikiwiki/ikiwiki.out" % dep_path
if os.environ.has_key('PERL5LIB'):
os.environ['PERL5LIB'] += ':' + os.path.join(dep_path, 'ikiwiki')
else:
os.environ['PERL5LIB'] = 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):
if not ikiwiki.found:
return False
if ikiwiki.synced:
return True
shutil.rmtree(html_path)
os.system("git checkout -- '%s'" % 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 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
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/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):
shutil.copytree(git_subpath, html_git_subpath)
else:
shutil.copy2(git_subpath, html_git_subpath)
return ikiwiki.synced
class zeronet:
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")
running = url200('http://127.0.0.1:43110/1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
if found and not running:
proc = subprocess.Popen([zn_script], env={"DISPLAY":""})
while not running and proc.returncode == None:
time.sleep(0.5)
running = url200('http://127.0.0.1:43110/1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
if not running:
print("WARNING: ZeroNet not running, won't be used")
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
|