Created
August 27, 2015 03:21
-
-
Save garbas/453de961cf37131a7dec to your computer and use it in GitHub Desktop.
nix-store --tree to ncdu format
This file contains hidden or 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 sys | |
import os | |
import json | |
import time | |
import subprocess | |
def set_item(tree, items, info): | |
item = items[0] | |
if item not in tree: | |
tree[item] = dict() | |
if len(items) == 1: | |
tree[item]['__info__'] = info | |
else: | |
set_item(tree[item], items[1:], info) | |
def get_size(folder): | |
return int(subprocess.check_output( | |
['du','-s', '-B 1', path]).split()[0].decode('utf-8')) | |
if __name__ == '__main__': | |
tree, items, prev_level = dict(), [], 0 | |
for line in sys.stdin.read().split('\n'): | |
line = line.strip() | |
if not line: | |
continue | |
level = line.find('/') / 4 | |
path = line[level * 4:] | |
info = os.lstat(path) | |
if level == 0: | |
items = [ path ] | |
elif level > prev_level: | |
items.append(path) | |
elif level < prev_level: | |
items = items[:-(prev_level - level + 1)] + [ path ] | |
set_item(tree, items, dict( | |
name=path[44:], | |
dsize=get_size(path), | |
asize=info.st_size, | |
dev=info.st_dev, | |
ino=info.st_ino, | |
)) | |
prev_level = level | |
data = [1, 0, { | |
"progname" : "ncdu", | |
"progver" : "1.9", | |
"timestamp" : int(time.time()) | |
}] | |
def tree2json(data, tree): | |
for path in filter(lambda x: x != '__info__', tree.keys()): | |
tmp = [tree[path]['__info__']] | |
if len(tree[path].keys()) > 1: | |
tree2json(tmp, tree[path]) | |
data.append(tmp) | |
tree2json(data, tree) | |
print json.dumps(data, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment