installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / ga-sumsize
blob59301cec15b767d935176866556d0ef1338f06d2
1 #!/usr/bin/env perl
3 #=======================================================================
4 # ga-sumsize
5 # File ID: dc4436de-5619-11e5-9330-000df06acc56
7 # Read git-annex keys from stdin or files and output total size.
9 # Character set: UTF-8
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
18 use Number::Bytes::Human qw(format_bytes);
20 local $| = 1;
22 our %Opt = (
24 'display' => 0,
25 'help' => 0,
26 'quiet' => 0,
27 'verbose' => 0,
28 'version' => 0,
32 our $progname = $0;
33 $progname =~ s/^.*\/(.*?)$/$1/;
34 our $VERSION = '0.2.0';
36 Getopt::Long::Configure('bundling');
37 GetOptions(
39 'display|d' => \$Opt{'display'},
40 'help|h' => \$Opt{'help'},
41 'quiet|q+' => \$Opt{'quiet'},
42 'verbose|v+' => \$Opt{'verbose'},
43 'version' => \$Opt{'version'},
45 ) || die("$progname: Option error. Use -h for help.\n");
47 $Opt{'verbose'} -= $Opt{'quiet'};
48 $Opt{'help'} && usage(0);
49 if ($Opt{'version'}) {
50 print_version();
51 exit(0);
54 exit(main());
56 sub main {
57 # {{{
58 my $Retval = 0;
59 my $total_size = 0;
61 while (<>) {
62 $total_size += file_size($_);
63 $Opt{'display'} && print("$total_size ");
64 print;
67 printf("\n%s: Total size of keys: %u (%s)\n",
68 $progname,
69 $total_size,
70 format_bytes($total_size, bs => 1000),
73 return $Retval;
74 # }}}
75 } # main()
77 sub file_size {
78 # Read file size from git-annex key {{{
79 my $key = shift;
80 my $retval = 0;
81 if ($key =~ /[A-Z0-9_]+-s(\d+)-/) {
82 $retval = $1;
84 return($retval)
85 # }}}
86 } # file_size()
88 sub print_version {
89 # Print program version {{{
90 print("$progname $VERSION\n");
91 return;
92 # }}}
93 } # print_version()
95 sub usage {
96 # Send the help message to stdout {{{
97 my $Retval = shift;
99 if ($Opt{'verbose'}) {
100 print("\n");
101 print_version();
103 print(<<"END");
105 Read git-annex keys from stdin or files and output total size.
107 For example:
109 git-annex unused >unused.txt
110 $progname unused.txt
112 Usage: $progname [options] [file [files [...]]]
114 Options:
116 -d, --display
117 Prefix each line with the current count.
118 -h, --help
119 Show this help.
120 -q, --quiet
121 Be more quiet. Can be repeated to increase silence.
122 -v, --verbose
123 Increase level of verbosity. Can be repeated.
124 --version
125 Print version information.
128 exit($Retval);
129 # }}}
130 } # usage()
132 sub msg {
133 # Print a status message to stderr based on verbosity level {{{
134 my ($verbose_level, $Txt) = @_;
136 if ($Opt{'verbose'} >= $verbose_level) {
137 print(STDERR "$progname: $Txt\n");
139 return;
140 # }}}
141 } # msg()
143 __END__
145 # This program is free software; you can redistribute it and/or modify
146 # it under the terms of the GNU General Public License as published by
147 # the Free Software Foundation; either version 2 of the License, or (at
148 # your option) any later version.
150 # This program is distributed in the hope that it will be useful, but
151 # WITHOUT ANY WARRANTY; without even the implied warranty of
152 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
153 # See the GNU General Public License for more details.
155 # You should have received a copy of the GNU General Public License
156 # along with this program.
157 # If not, see L<http://www.gnu.org/licenses/>.
159 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :