1 # Copyright(c) 2009, Gentoo Foundation
3 # Licensed under the GNU General Public License, v2
7 """Print total size of files contained in a given package"""
9 from __future__ import print_function
11 __docformat__ = 'epytext'
18 from getopt import gnu_getopt, GetoptError
20 import gentoolkit.pprinter as pp
21 from gentoolkit.equery import format_options, mod_usage, CONFIG
22 from gentoolkit.query import Query
32 "include_masked": True,
34 "show_progress": False,
35 "size_in_bytes": False
42 def print_help(with_description=True):
43 """Print description, usage and a detailed help message.
45 @type with_description: bool
46 @param with_description: if true, print module's __doc__ string
50 print(__doc__.strip())
53 # Deprecation warning added by djanderson, 12/2008
55 "Default action for this module has changed in Gentoolkit 0.3.",
56 "Use globbing to simulate the old behavior (see man equery).",
57 "Use '*' to check all installed packages.",
58 "Use 'foo-bar/*' to filter by category."
60 for line in depwarning:
61 sys.stderr.write(pp.warn(line))
64 print(mod_usage(mod_name="size"))
66 print(pp.command("options"))
67 print(format_options((
68 (" -h, --help", "display this help message"),
69 (" -b, --bytes", "report size in bytes"),
70 (" -f, --full-regex", "query is a regular expression")
74 def display_size(match_set):
75 """Display the total size of all accessible files owned by packages.
78 @param match_set: package cat/pkg-ver strings
82 size, files, uncounted = pkg.size()
85 print(" * %s" % pp.cpv(str(pkg.cpv)))
86 print("Total files : %s".rjust(25) % pp.number(str(files)))
89 print(("Inaccessible files : %s".rjust(25) %
90 pp.number(str(uncounted))))
92 if QUERY_OPTS["size_in_bytes"]:
93 size_str = pp.number(str(size))
95 size_str = "%s %s" % format_bytes(size)
97 print("Total size : %s".rjust(25) % size_str)
99 info = "%s: total(%d), inaccessible(%d), size(%s)"
100 print(info % (str(pkg.cpv), files, uncounted, size))
103 def format_bytes(bytes_, precision=2):
104 """Format bytes into human-readable format (IEC naming standard).
106 @see: http://mail.python.org/pipermail/python-list/2008-August/503423.html
108 @return: (str(num), str(label))
120 return (pp.number('0'), 'bytes')
122 return (pp.number('1'), 'byte')
124 for factor, label in labels:
125 if not bytes_ >= factor:
128 float_split = str(bytes_/float(factor)).split('.')
129 integer = float_split[0]
130 decimal = float_split[1]
131 if int(decimal[0:precision]):
132 float_string = '.'.join([integer, decimal[0:precision]])
134 float_string = integer
136 return (pp.number(float_string), label)
139 def parse_module_options(module_opts):
140 """Parse module options and update QUERY_OPTS"""
142 opts = (x[0] for x in module_opts)
144 if opt in ('-h', '--help'):
147 elif opt in ('-b', '--bytes'):
148 QUERY_OPTS["size_in_bytes"] = True
149 elif opt in ('-e', '--exact-name'):
150 sys.stderr.write(pp.warn("-e, --exact-name is now default."))
151 warning = pp.warn("Use globbing to simulate the old behavior.")
152 sys.stderr.write(warning)
154 elif opt in ('-f', '--full-regex'):
155 QUERY_OPTS['is_regex'] = True
158 def main(input_args):
159 """Parse input and run the program"""
161 # -e, --exact-name is no longer needed. Kept for compatibility.
164 long_opts = ('help', 'bytes', 'full-regex', 'exact-name')
167 module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
168 except GetoptError as err:
169 sys.stderr.write(pp.error("Module %s" % err))
171 print_help(with_description=False)
174 parse_module_options(module_opts)
181 for query in (Query(x, QUERY_OPTS['is_regex']) for x in queries):
185 matches = query.smart_find(**QUERY_OPTS)
188 sys.stderr.write(pp.error("No package found matching %s" % query))
192 display_size(matches)
196 # vim: set ts=4 sw=4 tw=79: