Created
June 3, 2017 05:37
-
-
Save bochoven/591fadd4567a5c43c0d90498a5378a0d to your computer and use it in GitHub Desktop.
macOS: Get all local users and calculate the size of the homedirectory
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
#!/usr/bin/python | |
# Get all local users and calculate the size of the homedirectory | |
import os, pwd, plistlib | |
system_accounts = ['root', 'daemon', 'nobody', 'Guest'] | |
account_list = [] | |
def get_size(start_path): | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
for f in filenames: | |
fp = os.path.join(dirpath, f) | |
try: | |
total_size += os.path.getsize(fp) | |
except Exception as e: | |
pass | |
return total_size | |
# Round to .5GB precision (29 bit) | |
def _HalfGB(size): | |
return size >> 29 << 29 | |
for p in pwd.getpwall(): | |
if p[0][:1] == '_': | |
continue | |
if p[0] in system_accounts: | |
continue | |
if p[5] == '/var/empty': | |
continue | |
account_list.append({ | |
'name': p[0], | |
'homedir': p[5], | |
'size': str(_HalfGB(get_size(p[5]))) | |
}) | |
print plistlib.writePlistToString(account_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment