File: Package.pm

package info (click to toggle)
apt-cross 0.12.0
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 180 kB
  • ctags: 65
  • sloc: perl: 1,760; xml: 610; sh: 52; makefile: 12
file content (285 lines) | stat: -rw-r--r-- 8,048 bytes parent folder | download
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
package Cache::Apt::Package;
use Class::Struct;
use Exporter;
use Data::Dumper;
use strict;
use warnings;
use vars qw(@ISA @EXPORT $verbose );
@ISA=qw(Exporter);
@EXPORT=qw( lookup_pkg output_pkg );

=pod

=head1 Name

Cache::Apt::Package - Package handler for apt-cross and related tools

=head2 Description

Turns the awkward libapt-pkg-perl API into an Object-oriented
set of structs that can be passed around between scripts.
Uses an extensively modified portion of NorthernCross
to provide the data contained in the structs. Together with
Cache and Config, Package will form a new Debian package:
libcache-apt-perl

The structures are designed to be discrete and do not contain
self-referencing variables as are found in apt-pkg-perl. All instances
can be safely used with Data::Dumper to inspect the contents - most
functions support a $verbose setting that can dump the contents of the
current package to the console using Data::Dumper.

=head1 Copyright and Licence

 Copyright (C) 2007  Neil Williams <codehelp@debian.org>

 This package 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 3 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.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

=head1 Bugs

Please report bugs via the Debian Bug Tracking System.

=head1 Support

All enquiries to the C<<debian-embedded@lists.debian.org>> mailing list.

=cut

=head1 AptCrossPackage

The struct that is used to describe a package.

Note that Depends is an array - it needs to be an array of
AptCrossDependency structs.

 struct (AptCrossPackage => {
	"Package" =>             '$',
	"Version" =>             '$',
	"Source" =>              '$',
	"Distribution" =>        '$',
	"Architecture" =>        '$',
	"Maintainer" =>          '$',
	"Size" =>                '$',
	"Installed" =>           '$',
	"Section" =>             '$',
	"Priority" =>            '$',
	"Build_Depends" =>       '$',
	"Build_Depends_Indep" => '$',
	"Depends" =>             '@',
	"Description" =>         '$',
	"Summary" =>             '$',
	"Recommends" =>          '$',
	"Suggests" =>            '$',
	"Filename" =>            '$',
	"Provides" =>            '$',
	"FileMD5Hash" =>         '$',
 });

=cut

struct (AptCrossPackage => {
	"Package" =>             '$',
	"Version" =>             '$',
	"Source" =>              '$',
	"Distribution" =>        '$',
	"Architecture" =>        '$',
	"Cross_Architecture" =>  '$',
	"Maintainer" =>          '$',
	"Size" =>                '$',
	"Installed" =>           '$',
	"Section" =>             '$',
	"Priority" =>            '$',
	"Build_Depends" =>       '$',
	"Build_Depends_Indep" => '$',
	"Depends" =>             '@',
	"Description" =>         '$',
	"Summary" =>             '$',
	"Recommends" =>          '$',
	"Suggests" =>            '$',
	"Filename" =>            '$',
	"Provides" =>            '$',
	"FileMD5Hash" =>         '$',
});

=head1 AptCrossDependency

AptCrossPackage->Depends is an array of
AptCrossDependency instances describing the
dependency.

VersionLimit is the version indicator for this dependency and the
version string itself - but not the parentheses.

 e.g.
 >= 1.2.3-1
 << 0.56-2
 >> 1.3~r345-1

 struct (AptCrossDependency => {
	"Package" => '$',
	"VersionLimit" => '$',
	"Installed" => '$',
	"Type" => '$',
 });

=cut

struct (AptCrossDependency => {
	"Package" => '$',
	"VersionLimit" => '$',
	"Installed" => '$',
	"Type" => '$',
});

sub populate_depends
{
	my ($depends, $pkg) = @_;
	my @list=();
	foreach my $name (keys %$depends)
	{
		my $dep = new AptCrossDependency;
		$dep->Package($name);
		$dep->VersionLimit($$depends{$name}[0]);
		$dep->Installed($$depends{$name}[1]);
		$dep->Type($$depends{$name}[2]);
		push @list, $dep;
	}
	$$pkg->Depends(\@list);
}

=head1 lookup_pkg

Populate an AptCrossPackage with data from the cache.

Use this function to remove some of the pain of working directly
with the apt-pkg-perl constructs or even AptCross::Cache functions.

Note that this lookup operates on the binary package name.

=cut

sub lookup_pkg
{
	my $pkg = shift;
	$verbose = Cache::Apt::Lookup::get_verbose();
	my $rec = Cache::Apt::Lookup::binlookup($pkg->Package);
	print Dumper ($rec) if ($verbose >= 4);
	$pkg->Version($rec->{VerStr});
	# all dpkg-cross packages are Architecture: all
	if ($pkg->Package =~ /-([^\-]+)-cross$/)
	{
		$pkg->Cross_Architecture($1);
		$pkg->Architecture("all");
	}
	(defined $rec->{SourcePkg}) ? $pkg->Source($rec->{SourcePkg}) : $pkg->Source($pkg->Package);
	#$pkg->Size();
	#$pkg->Installed();
	$pkg->Section($rec->{Section});
	$pkg->Priority($rec->{Priority});
	$pkg->Maintainer($rec->{Maintainer});
	$pkg->Build_Depends();
	$pkg->Build_Depends_Indep();
	$pkg->Depends();
	$pkg->Description($rec->{LongDesc});
	$pkg->Summary($rec->{ShortDesc});
	(defined $rec->{Recommends}) ? $pkg->Recommends($rec->{Recommends}) : undef;
	(defined $rec->{Suggests}) ? $pkg->Suggests($rec->{Suggests}) : undef;
	(defined $rec->{FileName}) ? $pkg->Filename($rec->{FileName}) : undef;
	my $str = Cache::Apt::Lookup::get_provides($pkg->Package);
	(defined $str) ? $pkg->Provides($str) : undef;
	if (defined($pkg->Filename) && ($pkg->Filename =~ /_([^_]*)\.deb$/))
	{
		$pkg->Architecture($1);
	}
	# reset Architecture: all packages
	if (defined($pkg->Filename) && ($pkg->Filename =~ /_all\.deb$/))
	{
		$pkg->Architecture("all");
	}
	$pkg->FileMD5Hash($rec->{MD5Hash});
	&get_depends_list ($pkg);
	return \$pkg;
}

=head1 output_pkg

Creates output on STDOUT to imitate dpkg-query -s or apt-cache show

Certain values are not available via the current perl bindings.

=cut

sub output_pkg {
	my $emp = shift;
	print "Package: " . $$emp->Package . "\n" if ($$emp->Package);
	print "Version: " . $$emp->Version . "\n" if ($$emp->Version);
	print "Architecture: " . $$emp->Architecture . "\n" if ($$emp->Architecture);
	print "Cross-Architecture: " . $$emp->Cross_Architecture . "\n"
		if ($$emp->Cross_Architecture);
	print "Maintainer: " . $$emp->Maintainer . "\n" if ($$emp->Maintainer);
	print "Source: " . $$emp->Source . "\n" if ($$emp->Source);
	my $dep = $$emp->Depends;
	my $deps;
	my $line = "";
	my @a=();
	foreach my $d (@$dep)
	{
		$deps = $$d->Type . ":";
		$line .= $$d->Package . " (" . $$d->VersionLimit . ")" if ($$d->VersionLimit);
		$line =~ s/\s\s+/ /g;
		push @a, $line if ($line ne "");
		undef ($deps) if ($line eq "");
		$line="";
	}
	print $deps . join(", ",@a) . "\n" if (defined($deps));
	print "Suggests: ".$$emp->Suggests."\n" if ($$emp->Suggests);
	print "Recommends: ".$$emp->Recommends."\n" if ($$emp->Recommends);
	print "Priority: " . $$emp->Priority . "\n" if ($$emp->Priority);
	print "Section: " . $$emp->Section . "\n" if ($$emp->Section);
	print "Filename: " . $$emp->Filename . "\n" if ($$emp->Filename);
	print "Provides: " . $$emp->Provides . "\n" if ($$emp->Provides);
	print "Size: " . $emp->Size . "\n" if ($$emp->Size);
	print "MD5sum: " . $$emp->FileMD5Hash . "\n" if ($$emp->FileMD5Hash);
	print "Description: " . $$emp->Description . "\n" if ($$emp->Description);
}

sub get_depends_list
{
	my %sorter = ();
	my @list=();
	my $emp = shift;
	my $dephash = Cache::Apt::Lookup::deplookup(\$emp);
	foreach my $href (@$dephash)
	{
		foreach my $hh (keys %$href)
		{
			# TODO: bring the subroutine inline.
			&populate_depends($$href{$hh}, \$emp);
			my $nested = $$href{$hh};
			while (my ($n, $g) = each (%$nested))
			{
				$g = $$nested{$n};
				my $dep = new AptCrossDependency;
				$dep->Package($n);
				$dep->VersionLimit($$g[0]);
				$dep->Installed($$g[1]);
				$dep->Type($$g[2]);
				push @list, \$dep;
			}
		}
	}
	$emp->Depends(\@list);
}

1;