-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdu_class.py
169 lines (155 loc) · 5.27 KB
/
du_class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#/usr/bin/python
# Note that the main step to transfer from functional type to class type is to add 'self.' in front of each variable
# that needs to be transferred into other functions.
from optparse import OptionParser
import sys,os
global count
count = 1
class Process(object):
def __init__(self):
self.dirlist = [] ## subdirs list
self.list = [] ## files list
self.size = [] ## str type
self.sizecal = [] ## int type
self.total = 0 ## calculate total
def _option(self):
parser = OptionParser()
parser.add_option("-H","--humanreadable",
dest="humanreadable",
action="store_true",
default=False,
help="human readable",)
self.options, self.args = parser.parse_args()
def _tracedownDir(self,name=''):
global count
if count == 1:
name = self.fn
count += 1
isdir, isfile, join = os.path.isdir, os.path.isfile, os.path.join
Isdir = os.listdir(name)
dirs = [i for i in Isdir if isdir(join(name,i))]
files = [i for i in Isdir if isfile(join(name,i))]
if dirs:
for d in dirs:
# print join(name,d)
self.dirlist.append(join(name,d))
self._tracedownDir(join(name,d))
if files:
for f in files:
# print join(name,f)
self.list.append(join(name,f))
self.dir = name
def _getSize(self):
for path in self.list:
i = 1
tempsize = os.path.getsize(path)
while tempsize > 4096*i:
i += 1
if not self.options.humanreadable:
self.size.append(str(4096*i))
self.sizecal.append(4096*i)
else:
self.sizecal.append(4096*i)
if tempsize < 1024**2:
self.size.append(str(4096*i/1024.0) + 'K')
elif tempsize >= 1024**2 and tempsize < 1024**3:
self.size.append(str(40964*i/1024.0/1024.0) + 'M')
elif tempsize >= 1024**3 and tempsize < 1024**4:
self.size.append(str(4096*i/1024.0/1024.0/1024.0) + 'G')
elif tempsize >= 1024**4 and tempsize < 1024**5:
self.size.append(str(4096*i/1024.0/1024.0/1024.0/1024.0) + 'T')
self.total = sum(self.sizecal)
#self.total = reduce(lambda x, y: x+y, self.sizecal)
def _printResult(self):
if self.options.humanreadable:
if self.total < 1024**2:
self.total = (str(self.total/1024.0) + 'K')
elif self.total >= 1024**2 and self.total < 1024**3:
self.total = (str(self.total/1024.0/1024.0) + 'M')
elif self.total >= 1024**3 and self.total < 1024**4:
self.total = (str(self.total/1024.0/1024.0/1024.0) + 'G')
elif self.total >= 1024**4 and self.total < 1024**5:
self.total = (str(self.total/1024.0/1024.0/1024.0/1024.0) + 'T')
filedic = dict(zip(self.list,self.size))
filedic.update({self.dir:self.total})
for k,v in filedic.items():
print "%s : %s" %(v,k)
def _tracedownSubdir(self):
lists = self.dirlist
for d in lists:
self.dirlist = [] ## clear the subdirs list
self.list = [] ## clear the files list
self.size = []
self._tracedownDir(d)
self._getSizeSubdir()
self._printResultSubdir()
def _getSizeSubdir(self):
for path in self.list:
i = 1
tempsize = os.path.getsize(path)
while tempsize > 4096*i:
i += 1
self.size.append(4096*i)
if not self.size:
self.size = [4096] ## Still occupied 4K for empty dir
self.total = sum(self.size)
def _printResultSubdir(self):
if self.options.humanreadable:
if self.total < 1024**2:
self.total = (str(self.total/1024.0) + 'K')
elif self.total >= 1024**2 and self.total < 1024**3:
self.total = (str(self.total/1024.0/1024.0) + 'M')
elif self.total >= 1024**3 and self.total < 1024**4:
self.total = (str(self.total/1024.0/1024.0/1024.0) + 'G')
elif self.total >= 1024**4 and self.total < 1024**5:
self.total = (str(self.total/1024.0/1024.0/1024.0/1024.0) + 'T')
filedic = {self.dir:self.total}
for k,v in filedic.items():
print "%s : %s" %(v,k)
def _getSizeFile(self):
i = 1
self.total = os.path.getsize(self.fn)
self.dir = self.fn
while self.total > 4096*i:
i += 1
if not self.options.humanreadable:
self.total = str(4096*i)
else:
self.total = 4096*i
if self.options.humanreadable:
if self.total < 1024**2:
self.total = (str(self.total/1024.0) + 'K')
elif self.total >= 1024**2 and self.total < 1024**3:
self.total = (str(self.total/1024.0/1024.0) + 'M')
elif self.total >= 1024**3 and self.total < 1024**4:
self.total = (str(self.total/1024.0/1024.0/1024.0) + 'G')
elif self.total >= 1024**4 and self.total < 1024**5:
self.total = (str(self.total/1024.0/1024.0/1024.0/1024.0) + 'T')
filedic = {self.dir:self.total}
for k,v in filedic.items():
print "%s : %s" %(v,k)
def _control(self):
if self.args:
for self.fn in self.args:
if not os.path.exists(self.fn):
print >> sys.stderr,"%s: cannot access %s: No such file or directory" %(__file__,self.fn) ## 2> /dev/null
continue
if os.path.isfile(self.fn):
self._getSizeFile()
elif os.path.isdir(self.fn):
self._tracedownDir()
self._getSize()
self._printResult()
self._tracedownSubdir()
else:
self.fn = os.getcwd()
self._tracedownDir()
self._getSize()
self._printResult()
self._tracedownSubdir()
def main():
pm = Process()
pm._option()
pm._control()
if __name__ == '__main__':
main()