Created
June 27, 2023 09:05
-
-
Save JaySon-Huang/e3d489377b3631863ba325125ee3b1fb to your computer and use it in GitHub Desktop.
Collect the inode usage of given directory
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
#!/usr/bin/env python | |
from __future__ import print_function | |
import os | |
import sys | |
class UsageInfo(object): | |
def __init__(self, inode_used, bytes_used): | |
self.inode = inode_used | |
self.bytes = bytes_used | |
def merge(self, rhs): | |
self.inode += rhs.inode | |
self.bytes += rhs.bytes | |
def __repr__(self): | |
return '{{"inode": {}, "bytes": {}}}'.format(self.inode, self.bytes) | |
def collect(root_path): | |
info = {} | |
for p in os.listdir(root_path): | |
full_sub_path = os.path.join(root_path, p) | |
if os.path.isdir(full_sub_path): | |
sub_path_infos = collect(full_sub_path) | |
sub_dir_info = UsageInfo(1, 0) # directory itself takes 1 inode | |
for p in sub_path_infos: | |
sub_dir_info.merge(sub_path_infos[p]) | |
info[full_sub_path] = sub_dir_info | |
print('{{"path": {}, "info": {}}}'.format(full_sub_path, info[full_sub_path])) | |
continue | |
sub_path_info = UsageInfo(1, os.path.getsize(full_sub_path)) | |
info[full_sub_path] = sub_path_info | |
# print(full_sub_path, info[full_sub_path]) | |
return info | |
def main(root_path): | |
sub_path_infos = collect(root_path) | |
sub_dir_info = UsageInfo(1, 0) # directory itself takes 1 inode | |
for p in sub_path_infos: | |
sub_dir_info.merge(sub_path_infos[p]) | |
print('{{"path": {}, "info": {}}}'.format(root_path, sub_dir_info)) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print('Usage: {} DIRECTORY'.format(sys.argv[0])) | |
sys.exit(1) | |
root_path = sys.argv[1] | |
main(root_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment