Created
November 7, 2016 08:11
-
-
Save maxux/9d8ff88a0908e3a62de8bb93f0fd32b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
class Entry: | |
""" | |
filepath|hash|filesize|uname|gname|permissions|filetype|ctime|mtime|extended | |
""" | |
def __init__(self, path, hash, size, filetype=2, uname='root', gname='root', permissions=0o644, ctime=None, mtime=None, extended=None): | |
self._data = { | |
'path': path, | |
'hash': hash, | |
'size': size, | |
'uname': uname, | |
'gname': gname, | |
'permissions': permissions, | |
'filetype': filetype, | |
'ctime': ctime, | |
'mtime': mtime, | |
'extended': extended, | |
} | |
def __str__(self): | |
return '{path}|{hash}|{size}|{uname}|{gname}|{permissions}|{filetype}|{ctime}|{mtime}|{extended}'.format(self._data**) | |
def generate(root): | |
for root, dirs, files in os.walk(root): | |
if not dirs and not files: | |
# empty directory | |
yield Entry(root, '', 0, 'root', 'root', 0o755, 4, 0, 0, 0) | |
for name in files: | |
path = os.path.join(root, name) | |
cmd = 'brotli --input {} | ipfs add'.format(path) | |
out = os.system(cmd) | |
m = re.match('^added .+ (.+)$', out) | |
if m is None: | |
raise RuntimeError('invalid output from ipfs add: %s' % out) | |
hash = m.group(1) | |
yield Entry(path, hash, size, uname, gname, permisssions, filetype, ctime, mtime, extended) | |
if __name__ == '__main__': | |
with open('plist', 'w') as f: | |
for entry in generate('/root/to/tree'): | |
f.write('%s\n' % entry) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment