Created
September 12, 2011 13:32
-
-
Save polymorphm/1211246 to your computer and use it in GitHub Desktop.
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/env python | |
# -*- mode: python; coding: utf-8 -*- | |
# | |
# Copyright 2011 Andrej A Antonov <[email protected]> | |
from __future__ import absolute_import, print_function | |
assert str is not unicode, "Required python version 2 (not version 3)" | |
import argparse, sys, os, os.path | |
def get_disk_usage(path): | |
''' | |
сколько места на диске занимает файловый объект | |
''' | |
stat_func = os.lstat if os.path.islink(path) else os.stat | |
stat = stat_func(path) | |
uid = stat.st_uid # пользователя номер | |
blocks = stat.st_blocks # столько занято блоков | |
blksize = stat.st_blksize # размер блока | |
du = blocks * blksize | |
return uid, du | |
def look_one_path(user_data, path): | |
''' | |
функция для обработки непосредственно | |
только одного файлового объекта | |
''' | |
uid, du = get_disk_usage(path) | |
if uid not in user_data: | |
user_data[uid] = 0 | |
user_data[uid] += du | |
def look_size(user_data, path): | |
''' | |
подсчёт | |
''' | |
look_one_path(user_data, path) | |
for root, dirs, files in os.walk(path): | |
for one_path in (os.path.join(root, p) for p in dirs + files): | |
try: | |
look_one_path(user_data, one_path) | |
except EnvironmentError: | |
# если ошибка операционной системы -- | |
# то просто показываем её и идём дальше | |
from traceback import print_exc | |
print_exc() | |
def show_results(user_data): | |
''' | |
показываем таблицу результатов | |
''' | |
for uid in user_data: | |
du = user_data[uid] | |
print('uid: {uid!r} -- disk usage: {du!r}'.format(uid=uid, du=du)) | |
def main(): | |
# это главная функция только-лишь проверяет командную строку | |
# и запускает функции: look_size() и show_results() | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('path', nargs='*', | |
help='directory-or-file for looking') | |
args = parser.parse_args() | |
if args.path: | |
look_list = args.path | |
else: | |
look_list = '/', | |
user_data = {} # здесь мы будем хранить сколько занято байт | |
# оносительно каждого uid | |
for path in look_list: | |
print('Begin look path {path!r}...'.format(path=path)) | |
# подсчёт | |
look_size(user_data, path) | |
# вывод результатов | |
print('Results:') | |
show_results(user_data) | |
if __name__ == '__main__': | |
error = main() | |
if error: | |
exit(error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment