File: apt-show-source.in

package info (click to toggle)
apt-show-source 0.10%2Bnmu5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 68 kB
  • sloc: perl: 178; makefile: 48
file content (328 lines) | stat: -rw-r--r-- 9,499 bytes parent folder | download | duplicates (2)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#! /usr/bin/perl -w

# apt-show-source - Lists source-packages.

# This program parses the APT lists for source packages and the dpkg
# status file and then lists every package with an different version
# number than the installed.

# Copyright (C) 2000 Dennis Schoen

# Author: Dennis Schoen <dennis@debian.org>
# Maintainer: Dennis Schoen <dennis@debian.org>
# Version: @VERSION@

# This file 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, or (at your option) any
# later version.

# This file 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.

# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.


use strict;
use Getopt::Long;

my $VERSION;
$VERSION='@VERSION@';

# process command line parameters
my %opts;
unless (GetOptions (\%opts,'status-file|stf=s','list-dir|ld=s',
					'package|p=s','help|h','all|a','verbose|v',
                    'version-only')) {
	exit 1;
}

if (exists $opts{'help'}) {
	print <<EOF;
Apt-Show-Source v.$VERSION (c) Dennis Schoen

Usage:
 apt-show-source            show source packages with higher version numbers
                            than the installed packages.

Options:
 -stf|--status-file=<file>  Use <file> as the dpkg status file instead
                            of /var/lib/dpkg/status
 -ld|list-dir=<directory>   Use <directory> as path to apt's list files instead
                            of /var/lib/apt/lists/
 -p|--package=<package>     Print source version for <package>.
 --version-only             Prints version only if used together
                            with --package.
 -a|--all                   Print out all available source packages.
 -v|--verbose				Verbose messages.
 -h|--help                  Print this help.
EOF
	exit;
}


# Path to apt's list files
my $list_dir;
if ($opts{'list-dir'}) {
	$list_dir = $opts{'list-dir'};
}
# New APT 5.0 path
elsif (-d "/var/lib/apt/lists/") {
	$list_dir = "/var/lib/apt/lists/";
}
# Old APT path
elsif (-d "/var/state/apt/lists/") {
	$list_dir = "/var/state/apt/lists/";
}
else {die "Can't find a valid lists directory\n"}

# Path to dpkg status file
my $status_file = $opts{'status-file'} || "/var/lib/dpkg/status";

opendir(DIR, $list_dir) or die "Can't opendir $list_dir: $!";
my @files = map { $list_dir . $_} grep /Sources$/, readdir(DIR);
unless (scalar @files > 0) {die "Error: No information about source packages! (Maybe no deb-src entries?)\n"};
closedir DIR ;

# This is the hash ref that stores our Sources Package information
my $source_packages;

# This is the hash ref that stores our _installed_ Package information
my $packages = parse_file ($status_file, 1);

foreach (@files) {
	my $href = &parse_file ($_);
	foreach (keys %$href) {
		$source_packages->{$_} = $href->{$_};
	}
}

if (exists $opts{'package'}) {
	my $key = $opts{'package'};
	my $skey = $packages->{$key}->{'Source'} || $key;

	unless ($packages->{$key}->{'Package'} || $packages->{$key}->{'Version'} ||
        $source_packages->{$skey}->{'Package'} || $source_packages->{$skey}->{'Version'}) {
		print "Can't find information about $opts{'package'} !\n";
		exit 1;
	}
	unless (cmp_version(($packages->{$key}->{'Version'} || 0),($source_packages->{$skey}->{'Version'} || 0))) {
		print "Sorry, no newer source package available for $opts{'package'}\n";
		exit 1;
	}
	my $package = $packages->{$key}->{'Package'} || "not installed.";
	my $version = $packages->{$key}->{'Version'} || "not available.";
	my $spackage = $source_packages->{$skey}->{'Package'} || "not available.";
	my $sversion = $source_packages->{$skey}->{'Version'} || "not available.";
	if ($opts{'version-only'}) {
		print "$sversion\n";
	} else {
		print_package ($package, $version, $spackage, $sversion);
	}
	exit;
}

if (exists $opts{'all'}) {
	foreach my $key (keys %$source_packages) {
		my $skey = $packages->{$key}->{'Source'} || $key;

		my $package = $packages->{$key}->{'Package'} || "not installed.";
		my $version = $packages->{$key}->{'Version'} || "not available.";
		my $spackage = $source_packages->{$skey}->{'Package'};
		my $sversion = $source_packages->{$skey}->{'Version'};

		print_package ($package, $version, $spackage, $sversion);
	}
	exit;
}

foreach my $key (keys %$packages) {
	my $skey = $packages->{$key}->{'Source'} || $key;

	my $package = $packages->{$key}->{'Package'};
	my $version = $packages->{$key}->{'Version'};
	my $sversion = $source_packages->{$skey}->{'Version'} || "not available.";
	my $spackage = $source_packages->{$skey}->{'Package'} || "not available.";

	if (cmp_version($sversion,($version || 0))) {
		print_package ($package, $version, $spackage, $sversion);
	}
}


format STDOUT_TOP =

Inst. Package (Version)                | Newest Source Package (Version)
-------------------------------------------------------------------------------
.

sub print_package {
	my ($package, $version, $spackage, $sversion) = @_;
	my $left = $package . " (" . $version . ")";
	my $right = $spackage . " (" . $sversion . ")";
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$left, $right
.

	write;
}

#-------------------------------------------------------
# FUNCTION: cmp_version (STRING, STRING)
#
# Compares complete version numbers ala upstream-debian
# and returns true if the first version string is greater
# than the second.
#--------------------------------------------------------

sub cmp_version {
	my ($first, $last)  = @_;

	return 0 if ($first eq $last);

	my ($firstup, $firstdeb) = split /-/, $first;
	my ($lastup, $lastdeb) = split /-/, $last;

	my @upstream = ($firstup,$lastup);
	my @debian = ($firstdeb,$lastdeb);
	
	my @index = sort {
		$upstream[$a] cmp $upstream[$b] or
		$debian[$a] cmp $debian[$b]
	} 0 .. $#upstream;

	if ($debian[0]) {
		if ("$upstream[0]-$debian[0]" eq $first) {
			return 1;
		} else {
			return 0;
		}
	} else {
		if ($upstream[0] eq $first) {
			return 1;
		} else {
			return 0;
		}
	}
}


# ------------------------------------------------------
# FUNCTION: HASHREF = parse_file FILE (STATUS)
#
# Parses FILE into an HASHREF of Hashes
# Set STATUS when the file should be parsed just for
# installed packages (here the dpkg status file)
# Returns HASHREF.
# ------------------------------------------------------

sub parse_file {
	my ($file, $status) = @_;
	my ($key, $value, $package, $packages);

	open FILE, $file or die "Can't open file $file: $!";
	if ($opts{'verbose'}) {print "Parsing $file...";};
	while (<FILE>) {
		if (/^$/){
			unless (defined $package) {next};
			if (defined $source_packages->{$package->{'Package'}}) {
				if ($source_packages->{$package->{'Package'}}->{'Version'} gt $package->{'Version'}) {
					undef $package;
					next;
				}
			}
			elsif (defined $packages->{$package->{'Package'}}) {
				if ($packages->{$package->{'Package'}}->{'Version'} gt $package->{'Version'}) {
					undef $package;
					next;
				}
			}

			if ($status) { # Are we parsing the status file?
				unless ($package->{'Status'} =~ /not-installed/ || $package->{'Status'} =~ /config-files/) {
					$packages->{ $package->{'Package'}} = $package;
				}
			}
			else {
				$packages->{$package->{'Package'}} = $package;
			}
			undef $package;
			next;
		}
		unless ((/^Package/) || (/^Version/) || (/^Status/) || (/^Source/)) {next};
		($key, $value) = split /: /, $_;
		$value =~ s/\n//;
		$value =~ s/\s\(.*\)$//; # Remove any Version information in ()
		$package->{$key} = $value;
	}
	if ($opts{'verbose'}) {print " completed.\n"};
	close FILE;
	return $packages;
}


# script documentation (POD style)

=head1 NAME

apt-show-source - Lists source-packages.

=head1 DESCRIPTION

This program parses the APT lists for source packages and the dpkg status file and then lists
every package with a higher version number than the one installed.

It may prove very useful if the "deb" entries in your APT sources.list point to
stable and the "deb-src" entries point to unstable. With this program you are
easily able to find out if there is a newer version of eg. Program XXXX
in unstable.

=head1 COMMAND LINE PARAMETERS

Optional command line parameters are the DPKG Status file to use, the path to APT's list files and a package name.
There are also options to display: all source-packages, verbose messages, version-only and command-line help.

=head1 OPTIONS

=head2 B<-stf> I<FILE>, B<--status-file>=I<FILE>

Reads installed packages from I<FILE> instead of /var/lib/dpkg/status.

=head2 B<-ld> I<DIRECTORY>, B<--list-dir>=I<DIRECTORY>

I<DIRECTORY> specifies the path to APT's list files, defaults to /var/lib/apt/lists/.

=head2 B<-p> I<PACKAGE>, B<--package>=I<PACKAGE>

Prints out the installed-package/source-package version Information for I<PACKAGE>.

=head2 B<--version-only>

Prints version only if used together with B<--package>.

=head2 B<-a>, B<--all>

Prints out all available source-packages with version.

=head2 B<-v>, B<--verbose>

Prints out verbose messages.

=head2 B<-h>, B<--help>

Prints out command-line help.

=head1 AUTHOR

Dennis Schoen, dennis@debian.org

=head1 SEE ALSO

apt(1), dpkg(1)

=cut