szaydel
4/8/2016 - 8:51 PM

BrickstorOS snippets for obtaining various filesystem information.

BrickstorOS snippets for obtaining various filesystem information.

#!/usr/bin/env python
# Copyright 2016 RackTop Systems Inc. and/or its affiliates.
# http://www.racktopsystems.com
#
# The methods and techniques utilized herein are considered TRADE SECRETS
# and/or CONFIDENTIAL unless otherwise noted. REPRODUCTION or DISTRIBUTION
# is FORBIDDEN, in whole and/or in part, except by express written permission
# of RackTop Systems.
import sys, os
import json

if len(sys.argv) < 2:  # Must give starting point, i.e. /storage/p01/foo/bar
    sys.stderr.write("Path to root of tree to scan is required!\n")
    sys.exit(1)

obj = {}

def stat_to_json(f):
    fi = os.stat(f)
    return \
    {
        "path": f,
        "mode": fi.st_mode,
        "inonde": fi.st_ino,
        "gid": fi.st_gid,
        "uid": fi.st_uid,
        "size": fi.st_size,
        "atime": fi.st_atime,
        "mtime": fi.st_mtime,
        "ctime": fi.st_ctime,
    }

for root, dirs, files in os.walk(sys.argv[1], topdown=True):
    f = [stat_to_json("%s/%s" % (root, _)) for _ in files]
    obj.update(
        {
            root: f,
        }
    )
print(json.dumps(obj))