#!/usr/bin/env python

import sys
import json
import math
import os

def convert_size(size_bytes):
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    p = math.pow(1024, i)
    s = round(size_bytes / p, 2)
    return "%s %s" % (s, size_name[i])

fileName = sys.argv[1]

json_data=open(fileName).read()

data = json.loads(json_data)

for item in data:
    if item[u'IsDir']:
        # print 'mkdir ' + item[u'Path']
        os.makedirs(item[u'Path'])

    if not item[u'IsDir']:
        print 'touch ' + item[u'Path']
        print convert_size(item[u'Size'])
        file = open(item[u'Path'] + ".txt", "w")
        file.write(convert_size(item[u'Size']))
        file.close()
