/
ipfs-mirror.py
executable file
·408 lines (310 loc) · 10.3 KB
/
ipfs-mirror.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python3
from argh import ArghParser, arg, dispatch, wrap_errors
import plyvel
import subprocess
import json
import sys
import os
class Cache(object):
def __init__(self, path=None, progress=None):
self.path = path
self.db = self.open('cache.db')
self.stat_db = self.open('stat.db')
self._goipfs_2938_db = self.open('_goipfs_2938.db')
self.progress = progress
self.filter = list(self.load_filter())
self.silent = False
def ensure_exists(self):
os.makedirs(self.path, exist_ok=True)
def open(self, name='cache.db'):
if self.path:
self.ensure_exists()
path = os.path.join(self.path, name)
return LevelDBStore(path)
else:
return NullStore(self.path)
def load_filter(self):
if self.path:
self.ensure_exists()
path = os.path.join(self.path, 'cacheignore')
try:
with open(path) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
continue
yield line
except FileNotFoundError:
pass
def add(self, path, **kwargs):
multihash = self.try_cache(path, lambda path: ipfs_add(path), **kwargs)
return multihash
def try_cache(self, path, func, root=None):
self.progress.log_n('[+] %-100r ... ' % path)
if self.skips_cache(root, path):
self.progress.log_n('NOCACHE ... ')
self.progress.log_size(path)
multihash = func(path)
else:
multihash = self.db.get(path)
if multihash:
if self.silent:
self.progress.buffer = ''
return multihash
else:
self.progress.log_n('HIT ... ')
else:
self.progress.log_n('MISS ... ')
self.progress.log_size(path)
multihash = func(path)
self.db.put(path, multihash)
self.progress.increase()
self.progress.log('%s' % multihash)
return multihash
def skips_cache(self, root, path):
if root:
relative = path[len(root):]
if any(relative.startswith(x) for x in self.filter):
return True
return os.path.basename(path) in self.filter
def close(self):
self.db.close()
def get(self, key):
return self.db.get(key)
def put(self, key, value):
return self.db.put(key, value)
class FolderWalker(object):
def __init__(self, root, cache=None, progress=None):
self.root = root
if not cache:
cache = Cache(cache)
self.cache = cache
self.progress = progress
def add(self, path):
return self.cache.add(path, root=self.root)
def traverse(self):
tree = {}
self.progress.total = sum(
len(files)
for _, _, files in
os.walk(self.root))
for root, subs, files in os.walk(self.root):
subs.sort()
files.sort()
folder_content = self._process_folder(root, files)
obj = {
'folders': subs,
'files': folder_content
}
tree[root] = obj
return tree
def _process_folder(self, root, files):
def process(root, files):
for name in files:
path = os.path.join(root, name)
multihash = self.add(path)
yield name, multihash
return {name: multihash for name, multihash in process(root, files)}
class NullStore(object):
def __init__(self, path):
pass
def close(self):
pass
def get(self, key):
pass
def put(self, key, value):
pass
class LevelDBStore(NullStore):
def __init__(self, path):
self.db = plyvel.DB(path, create_if_missing=True)
def close(self):
return self.db.close()
def get(self, key):
key = bytes(key, 'utf8')
value = self.db.get(key)
if value:
value = str(value, 'utf8')
return value
def put(self, key, value):
key = bytes(key, 'utf8')
value = bytes(value, 'utf8')
return self.db.put(key, value)
class Progress(object):
text_only = False
def __init__(self, total=-1):
self.progress = 0
self.total = total
self.silent = False
self.dirty = False
self.buffer = ''
def log(self, line):
self.reset()
log(self.buffer + line)
self.buffer = ''
self.update()
def log_n(self, chunk):
self.reset()
self.buffer += chunk
if not Progress.text_only:
log_n(self.buffer)
self.update()
def log_size(self, path):
self.reset()
size = os.path.getsize(path)
size = human_size(size)
self.log_n('%s ... ' % size)
self.update()
def update(self):
if self.silent or Progress.text_only:
return
if not self.dirty:
log('')
self.dirty = True
if self.total < 0:
log_n('\r[%%] %d / ??' % (self.progress))
else:
log_n('\r[%%] %d / %d' % (self.progress, self.total))
def finish(self):
if Progress.text_only:
return
self.reset()
self.dirty = True
self.update()
log('')
self.reset_line()
self.silent = True
def reset(self):
if self.silent or Progress.text_only:
return
if self.dirty:
log_n('\033[1A\r')
self.dirty = False
self.reset_line()
log_n('\033[2K\r')
def reset_line(self):
if not Progress.text_only:
log_n('\033[2K\r')
def increase(self, num=1):
self.progress += num
def log(line):
print(line, file=sys.stderr)
def log_n(chunk):
print(chunk, end='', flush=True, file=sys.stderr)
def human_size(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return '%5.1f %3s%s' % (num, unit, suffix)
num /= 1024.0
return '%.1f %s%s' % (num, 'Yi', suffix)
def ipfs(cmd):
output = subprocess.check_output(['ipfs'] + cmd)
output = str(output, 'ascii').strip()
return output
@wrap_errors([KeyboardInterrupt])
def empty():
'Create empty folder'
return ipfs(['object', 'new', 'unixfs-dir'])
def ipfs_add(path):
return ipfs(['add', '-q', '--progress=false', '--', path])
@arg('--cache', metavar='path', help='cache location')
@wrap_errors([KeyboardInterrupt])
def add(path, cache=None):
'Get ipfs path for file'
cache = Cache(cache)
multihash = cache.add(path)
cache.close()
return multihash
@wrap_errors([KeyboardInterrupt])
def merge(root, name, multihash):
'Merge folder into another folder'
return ipfs(['object', 'patch', root, 'add-link', name, multihash])
def ipfs_patch_dir(content, progress=None, _goipfs_2938_db=None):
folder = empty()
for name, multihash in sorted(content.items()):
_key = '%r/%r/%r' % (folder, multihash, name)
_value = None
if _goipfs_2938_db:
_value = _goipfs_2938_db.get(_key)
if _value:
folder = _value
else:
folder = merge(folder, name, multihash)
if _goipfs_2938_db:
_goipfs_2938_db.put(_key, folder)
if progress:
progress.increase()
progress.update()
return folder
def stat(multihash, db=None):
def parse(output):
for line in output.split('\n'):
yield line.split(': ')
if db:
cached = db.get(multihash)
if cached:
return json.loads(cached)
output = ipfs(['object', 'stat', '--', multihash])
obj = {key: value for key, value in parse(output)}
if db:
db.put(multihash, json.dumps(obj))
return obj
def put(obj):
p = subprocess.Popen(['ipfs', 'object', 'put'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
data = json.dumps(obj).encode('utf-8')
stdout, _ = p.communicate(data)
output = str(stdout, 'utf-8').strip()
return output.split(' ')[1] # TODO: find a cleaner way
def files2obj(files, stat_db=None, progress=None):
ipfs_obj = {'Links':[],'Data':'\u0008\u0001'}
for name, multihash in files.items():
x = stat(multihash, db=stat_db)
y = {
'Name': name,
'Hash': multihash,
'Size': int(x['CumulativeSize'])
}
ipfs_obj['Links'].append(y)
if progress:
progress.increase()
progress.update()
return ipfs_obj
def resolve(root, tree, stat_db=None, _goipfs_2938_db=None):
obj = tree[root]
for folder in obj['folders']:
path = os.path.join(root, folder)
obj['files'][folder] = resolve(path, tree, stat_db=stat_db, _goipfs_2938_db=_goipfs_2938_db)
total = len(obj['files'].items())
progress = Progress(total)
progress.log_n('[*] resolving %r ... ' % root)
ipfs_obj = files2obj(obj['files'], stat_db=stat_db, progress=progress)
resolved = put(ipfs_obj)
#resolved = ipfs_patch_dir(obj['files'], _goipfs_2938_db=_goipfs_2938_db, progress=progress) # TODO
progress.log(resolved)
progress.finish()
return resolved
@arg('--cache', metavar='path', help='cache location')
@wrap_errors([KeyboardInterrupt])
def mirror(folder, cache=None, text_only=False, silent=False):
'Mirror a folder'
Progress.text_only = text_only
progress = Progress()
cache = Cache(cache, progress=progress)
cache.silent = silent
walker = FolderWalker(folder, cache, progress=progress)
tree = walker.traverse()
progress.finish()
cache.close()
return resolve(folder, tree, stat_db=cache.stat_db, _goipfs_2938_db=cache._goipfs_2938_db)
@wrap_errors([KeyboardInterrupt])
def init(path):
'Initialize cache'
cache = Cache(path)
ignore = os.path.join(path, 'cacheignore')
with open(ignore, 'w'):
pass
def main():
parser = ArghParser(description='todo')
parser.add_commands([mirror, add, empty, merge, init])
dispatch(parser)
if __name__ == '__main__':
main()