-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir-stats-summary.py
More file actions
executable file
·163 lines (137 loc) · 4.5 KB
/
Copy pathdir-stats-summary.py
File metadata and controls
executable file
·163 lines (137 loc) · 4.5 KB
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
#!/usr/bin/python
# vim: set sw=4 sts=4 ts=8 et ft=python fenc=utf8 ff=unix tw=74 :
#
# SYNOPSIS
# ========
# This script analyses an INI file created by dir-stats.py and displays
# directories containing a certain amount of data.
#
# ARGUMENTS
# =========
# Call the script without any parameters to see an unsage message.
#
# OUTPUT
# ======
# The script will print an INI style list of directory names and byte
# counts to stdout.
#
# HISTORY
# =======
# 2008-Jan-22 rbrt-weiler
# * Created the script.
#
import getopt
import os.path
import sys
import time
import ConfigParser
##########################################################################
SCRIPT_VERSION = '1.0.0'
opt_limit = 50000000
opt_style = 'win'
##########################################################################
class MyRawConfigParser(ConfigParser.RawConfigParser):
def optionxform(self, optionstr):
return str(optionstr)
##########################################################################
def main():
global opt_limit, opt_style
try:
opts, args = getopt.getopt(sys.argv[1:], 'hl:s:', [ 'help',
'limit=', 'style=' ])
except getopt.GetoptError:
usage()
sys.exit(1)
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit(1)
if o in ('-l', '--limit'):
opt_limit = int(a)
if o in ('-s', '--style'):
if a in ('win', 'unix'):
opt_style = a
else:
usage()
sys.exit(1)
if 0 == len(args):
usage()
sys.exit(1)
else:
for arg in args:
if not os.path.isfile(arg):
print 'Error: "' + arg + '" is no file.'
sys.exit(2)
summarize(args)
##########################################################################
def summarize(filenames):
if 'win' == opt_style:
cmt_char = ';'
kv_sep = ' = '
else:
cmt_char = '#'
kv_sep = ': '
summary = { }
print cmt_char + ' created ' + time.asctime() + ' by ' \
+ 'dir-stats-summary v' + SCRIPT_VERSION
print cmt_char + ' using a limit of ' + str(opt_limit) + ' bytes'
for filename in filenames:
cfg_parser = MyRawConfigParser()
try:
f_in = open(filename, 'r')
except:
print 'Error: Cannot read file "' + filename + '".'
sys.exit(3)
cfg_parser.readfp(f_in)
f_in.close()
sections = cfg_parser.sections()
for section in sections:
options = cfg_parser.options(section)
for option in options:
try:
size = cfg_parser.getint(section, option)
except ValueError:
size = 0
(basedir, basename) = os.path.split(option)
if summary.has_key(basedir):
summary[basedir] = summary[basedir] + size
else:
summary[basedir] = size
total_dirs = 0
total_size = 0
filename = os.path.basename(filename)
dirs = summary.keys()
dirs.sort()
print
print '[' + filename + ']'
for dir in dirs:
if summary[dir] >= opt_limit:
print dir + kv_sep + str(summary[dir])
total_dirs = total_dirs + 1
total_size = total_size + summary[dir]
print cmt_char + ' ' + filename + ': ' + str(total_dirs) \
+ ' directories with ' + str(total_size) + ' bytes'
cfg_parser = None
summary = { }
##########################################################################
def usage():
print 'dir-stats-summary v' + SCRIPT_VERSION + ' - released ' \
+ 'under the Zlib license'
print 'Usage: ' + os.path.basename(sys.argv[0]) + ' [options] ' \
+ 'filename [...]'
print
print 'Options:'
print ' -h, --help'
print ' Display this usage message and exit.'
print ' -l BYTES, --limit=BYTES'
print ' Set the minimum number of bytes that triggers reporting '
print ' of a directory.'
print ' The default limit is 50000000 bytes.'
print ' -s STYLE, --style=STYLE'
print ' Define the style of the output. Accepted values are ' \
+ '"win" and "unix".'
print ' The default value is "win".'
##########################################################################
if '__main__' == __name__:
main()
sys.exit(0)