summaryrefslogtreecommitdiff
path: root/rpmduplicates.pl
blob: 840575ecb8048ae36c4ac739412b05fb31432403 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/perl
###############################################################################
#
#    Copyright (C) 2006 by Eric Gerbier
#    Bug reports to: gerbier@users.sourceforge.net
#    $Id: rpmorphan-1.11 | rpmduplicates.pl | Mon Jul 4 12:47:55 2011 +0000 | gerbier  $
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
###############################################################################
# search for packages with several versions installed
###############################################################################
use strict;
use warnings;

use English '-no_match_vars';

use Getopt::Long;    # arg analysis
use Pod::Usage;      # man page
use Data::Dumper;    # debug

# library
use File::Basename;
my $dirname = dirname($PROGRAM_NAME);

# not very usefull, but warning, debug ...
require $dirname . '/rpmorphan-lib.pl'
  or die "problem to load rpmorphan-lib: $ERRNO\n";

my $VERSION = '0.2';

my $opt_help;
my $opt_man;
my $opt_version;
my $opt_verbose;

my %opt = (
	'help'    => \$opt_help,
	'man'     => \$opt_man,
	'verbose' => \$opt_verbose,
	'version' => \$opt_version,
);

Getopt::Long::Configure('no_ignore_case');
GetOptions( \%opt, 'help|?', 'man', 'verbose', 'version|V' )
  or pod2usage(2);

init_debug($opt_verbose);

if ($opt_help) {
	pod2usage(1);
}
elsif ($opt_man) {
	pod2usage( -verbose => 2 );
}
elsif ($opt_version) {
	print_version($VERSION);
	exit;
}

#  we cannot use common cache as duplicate package are not clean taken
#  by the common code
## no critic (RequireInterpolationOfMetachars)
my $cmd =
  'rpm -qa --queryformat "%{NAME} %{VERSION}-%{RELEASE} %{BUILDTIME}\n" ';
## use critic

my $fh;
## no critic (ProhibitTwoArgOpen,RequireBriefOpen)
if ( !open $fh, "$cmd |" ) {
	die "can not open $cmd : $ERRNO\n";
}
## use critic

my %h_ver;          # versions
my %h_buildtime;    # buildtime

my $nb    = 0;
my $SPACE = q{ };

while ( my $line = <$fh> ) {
	chomp $line;
	my ( $soft, $version, $build ) = split $SPACE, $line;

	next if ( $soft eq 'gpg-pubkey' );

	if ( exists $h_ver{$soft} ) {

		# we already have found a package with same name
		my $old_ver  = $h_ver{$soft};
		my $old_date = $h_buildtime{$soft};

		my $old;
		if ( $old_date < $build ) {

			# current package was build after the stored one
			# keep the latest
			$h_buildtime{$soft} = $build;

			# delete the older
			$old = $old_ver;
		}
		else {

			# current package was build before the stored one
			# delete the older
			$old = $version;
		}
		$old_date = localtime $old_date;
		$build    = localtime $build;
		print "duplicate $soft : $old_ver ($old_date) / $version ($build)\n";
		print "suggest : rpm -e $soft-$old\n";
		$nb++;
	}
	else {
		$h_ver{$soft}       = $version;
		$h_buildtime{$soft} = $build;
	}
}
close $fh or warning("can not close $cmd : $ERRNO");

if ( $nb == 0 ) {
	print "no duplicate found !\n";
}

exit $nb;
__END__

=head1 NAME

rpmduplicates - find program with several version installed

=head1 DESCRIPTION

sometimes, the upgrade of a system will install new packages, and "forget" to remove
the older version. Rpmorphan do not work with this kind of unused packages. Rpmduplicates 
is the answer. I recommend to run it after each distribution upgrade 
(for example from fedora core 7 to 8).

=head1 SYNOPSIS

rpmduplicates.pl  [options]

options:

   -help                brief help message
   -man                 full documentation
   -V, --version        print version

   -verbose             verbose

=head1 REQUIRED ARGUMENTS

nothing

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Print the full manual page and exits.

=item B<-version>

Print the program release and exit.

=item B<-verbose>

The program works and print debugging messages.

=back

=head1 USAGE

rpmduplicates.pl 

=head1 FILES

nothing

=head1 DIAGNOSTICS

it will show the duplicates with their version and build date

and suggest to remove the older one

example :

duplicate kernel-devel : 2.6.24.4-64.fc8 (Sat Mar 29 15:36:41 2008) / 2.6.24.3-50.fc8 (Thu Mar 20 20:43:47 2008)

suggest : rpm -e kernel-devel-2.6.24.3-50.fc8

=head1 EXIT STATUS

the number of duplicates packages found

=head1 CONFIGURATION

nothing

=head1 DEPENDENCIES

rpmorphan library

=head1 INCOMPATIBILITIES

not known

=head1 BUGS AND LIMITATIONS

not known

=head1 NOTES

this program can be used as "normal" user

the gpg-pubkey packages are excluded from the result

=head1 SEE ALSO

=for man
\fIrpm\fR\|(1) for rpm call
.PP
\fIrpmorphan\fR\|(1)
.PP
\fIrpmusage\fR\|(1)
.PP
\fIrpmdep\fR\|(1)
.PP
\fIrpmextra\fR\|(1)

=for html
<a href="rpmorphan.1.html">rpmorphan(1)</a><br />
<a href="rpmusage.1.html">rpmusage(1)</a><br />
<a href="rpmdep.1.html">rpmdep(1)</a><br />
<a href="rpmextra.1.html">rpmextra(1)</a><br />

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2008 by Eric Gerbier
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=head1 AUTHOR

Eric Gerbier

you can report any bug or suggest to gerbier@users.sourceforge.net

=cut