Last active
December 26, 2015 07:49
-
-
Save pdonadeo/7117456 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 | |
# -*- coding: utf-8 -*- | |
import sys, os, re, glob | |
import subprocess | |
from subprocess import * | |
def check_output(*popenargs, **kwargs): | |
# WORKS WITH PYTHON 2.6.x | |
if 'stdout' in kwargs: | |
raise ValueError('stdout argument not allowed, it will be overridden.') | |
process = Popen(stdout=PIPE, *popenargs, **kwargs) | |
output, unused_err = process.communicate() | |
retcode = process.poll() | |
if retcode: | |
cmd = kwargs.get("args") | |
if cmd is None: | |
cmd = popenargs[0] | |
exc = CalledProcessError(retcode, cmd) | |
exc.output = output | |
raise exc | |
return output | |
def show_help(): | |
def p(x): | |
print >> sys.stderr, x | |
p("Usage %s [options] <dir and file list>"%sys.argv[0]) | |
p("""Options: | |
--help: Show this help | |
-h: Show this help | |
-a: All files in current working directory | |
-n: Sort by name | |
-s: Sort by size | |
-r: reverse list (even if not sorted) | |
""") | |
sys.exit(1) | |
def sconosciuto(cmd): | |
print >> sys.stderr, "Unknown option '%s'"%cmd | |
show_help() | |
flist = [] | |
options = {'sort_name': False, | |
'sort_size': True, | |
'reverse': False, | |
'all': False, | |
} | |
keyok = True | |
for x in sys.argv[1:]: | |
if keyok: | |
if x=='--': | |
keyok = False | |
continue | |
elif x[0:1] == '--': | |
# opzioni lunghe | |
if x == "--help": | |
show_help() | |
sconosciuto(x) | |
continue | |
elif x[0] == '-': | |
# opzioni corte | |
for o in x[1:]: | |
if o=='h': | |
show_help() | |
elif o=='s': | |
options['sort_size'] = True | |
continue | |
elif o=='n': | |
options['sort_name'] = True | |
continue | |
elif o=='r': | |
options['reverse'] = True | |
continue | |
elif o=='a': | |
options['all'] = True | |
continue | |
else: | |
sconosciuto(o) | |
continue | |
flist.append(x) | |
def flatten(l): | |
return [ item for sublist in l for item in sublist ] | |
if len(flist)==0: | |
flist = ['.'] | |
if options['all']: | |
flist = [ [os.path.join(f, '*'), os.path.join(f, '.*')] for f in flist ] | |
flist = flatten(flist) | |
flist = [ glob.glob(pattern) for pattern in flist ] | |
flist = flatten(flist) | |
cmd = [ "du", "-sk" ] + flist | |
try: | |
out = check_output(cmd) | |
except CalledProcessError,e: | |
print >> sys.stderr, "Some errors found while executing command. Output can be inaccurate" | |
out = e.output | |
dirs = [ x.split("\t") for x in out.split("\n") if len(x)>0 ] | |
total_k = sum([ int(a) for a,b in dirs ]) | |
max_k = max([ int(a) for a,b in dirs ]) | |
def sorting(by_name, by_size, data): | |
def cmp_name(a,b): | |
if a[1]<b[1]: | |
return -1 | |
elif a[1]==b[1]: | |
return 0 | |
else: | |
return 1 | |
def cmp_size(a,b): | |
ka = int(a[0]) | |
kb = int(b[0]) | |
return ka - kb | |
def cmp_combined(a,b): | |
r = cmp_size(a,b) | |
if r == 0: | |
r = cmp_name(a,b) | |
return r | |
if by_name and by_size: | |
return sorted(data,cmp_combined) | |
elif by_name: | |
return sorted(data,cmp_name) | |
elif by_size: | |
return sorted(data,cmp_size) | |
else: | |
return data | |
def identity(a): | |
return a | |
rev = { True: reversed, False: identity}[options['reverse']] | |
data = [ (int(k),n, (float(k)*100./total_k), (float(k)*100./max_k)) for k,n in rev(sorting(options['sort_name'],options['sort_size'],dirs)) ] | |
def hr(k): | |
if k<1024: | |
return "%d kB"%k | |
k/=1024 | |
if k<1024: | |
return "%.1f MB"%k | |
k/=1024. | |
if k<1024: | |
return "%.1f GB"%k | |
k/=1024. | |
return "%.1f TB"%k | |
def ralign(t,nch): | |
d = nch - len(t) | |
if d > 0: | |
t = " "*d + t | |
return t | |
def bar(p, nmax): | |
n1 = int(round(p/100.*nmax)) | |
n0 = nmax - n1 | |
return "*"*n1 + "."*n0 | |
for k,n,p,b in data: | |
print "%s %s %s %s"%(ralign(hr(k),10), ralign("%.2f%%"%p, 7), bar(b,20), n) | |
print "%s %s %s %s"%(ralign(hr(total_k),10), ralign("100%", 7), ralign("",20), "Total") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment