File Coverage

blib/lib/Debian/Dependencies.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Debian::Dependencies;
2              
3 9     9   713946 use strict;
  9         21  
  9         260  
4 9     9   65 use warnings;
  9         18  
  9         537  
5              
6             our $VERSION = '0.67';
7              
8 9     9   4215 use AptPkg::Config;
  0            
  0            
9             use Debian::Dependency;
10              
11             use overload '""' => \&_stringify,
12             '+' => \&_add,
13             'eq' => \&_eq;
14              
15             =head1 NAME
16              
17             Debian::Dependencies - a list of Debian::Dependency objects
18              
19             =head1 SYNOPSIS
20              
21             my $dl = Debian::Dependencies->new('perl, libfoo-perl (>= 3.4)');
22             print $dl->[1]->ver; # 3.4
23             print $dl->[1]; # libfoo-perl (>= 3.4)
24             print $dl; # perl, libfoo-perl (>= 3.4)
25              
26             $dl += 'libbar-perl';
27             print $dl; # perl, libfoo-perl (>= 3.4), libbar-perl
28              
29             print Debian::Dependencies->new('perl') + 'libfoo-bar-perl';
30             # simple 'sum'
31              
32             print Debian::Dependencies->new('perl')
33             + Debian::Dependencies->new('libfoo, libbar');
34             # add (concatenate) two lists
35              
36             print Debian::Dependencies->new('perl')
37             + Debian::Dependency->new('foo');
38             # add dependency to a list
39              
40             =head1 DESCRIPTION
41              
42             Debian::Dependencies a list of Debian::Dependency objects, with automatic
43             construction and stringification.
44              
45             Objects of this class are blessed array references. You can safely treat them
46             as arrayrefs, as long as the elements you put in them are instances of the
47             L class.
48              
49             When used in string context, Debian::Dependencies converts itself into a
50             comma-delimited list of dependencies, suitable for dependency fields of
51             F files.
52              
53             =head2 CLASS METHODS
54              
55             =over 4
56              
57             =item new(dependency-string)
58              
59             Constructs a new L object. Accepts one scalar argument,
60             which is parsed and turned into an arrayref of L objects.
61             Each dependency should be delimited by a comma and optional space. The exact
62             regular expression is C.
63              
64             =cut
65              
66             sub new {
67             my ( $class, $val ) = @_;
68              
69             my $self = bless [], ref($class)||$class;
70              
71             if ( defined($val) ) {
72             $self->add( Debian::Dependency->new($_) )
73             for split( /\s*,\s*/s, $val );
74             }
75              
76             return $self;
77             }
78              
79             sub _stringify {
80             my $self = shift;
81              
82             return join( ', ', @$self );
83             }
84              
85             sub _add_dependency {
86             my( $self, @deps ) = @_;
87              
88             DEP:
89             for my $dep(@deps) {
90             # see if the new dependency is already satisfied by some of the
91             # dependencies we have
92             for(@$self) {
93             next DEP if $_->satisfies($dep);
94             }
95              
96             # see if the new dependency is broader than (satisfies) some of the old
97             for(@$self) {
98             if( $dep->satisfies($_) ) {
99             $_ = $dep;
100             next DEP;
101             }
102             }
103              
104             # OK, the new dependency doesn't overlap with any of the old, add it
105             push @$self, $dep;
106             }
107             }
108              
109             sub _add {
110             my $left = shift;
111             my $right = shift;
112             my $mode = shift;
113              
114             $right = $left->new($right) unless ref($right);
115             $right = [ $right ] if $right->isa('Debian::Dependency');
116              
117             if ( defined $mode ) { # $a + $b
118             my $result = bless [ @$left ], ref($left);
119             $result->_add_dependency(@$right);
120             return $result;
121             }
122             else { # $a += $b;
123             $left->_add_dependency(@$right);
124             return $left;
125             }
126             }
127              
128             sub _eq {
129             my( $left, $right ) = @_;
130              
131             # force stringification
132             return "$left" eq "$right";
133             }
134              
135             =back
136              
137             =head2 OBJECT METHODS
138              
139             =over 4
140              
141             =item add( I[, ... ] )
142              
143             Adds I (or a list of) to the list of dependencies. If the new
144             dependency is a subset of or overlaps some of the old dependencies, it is not
145             duplicated.
146              
147             my $d = Debian::Dependencies('foo, bar (<=4)');
148             $d->add('foo (>= 4), bar');
149             print "$d"; # foo (>= 4), bar (>= 4)
150              
151             I can be either a L object, a
152             L object, or a string (in which case it is converted to an
153             instance of the L class).
154              
155             =cut
156              
157             sub add {
158             my $self = shift;
159              
160             while ( defined(my $dep = shift) ) {
161             $dep = Debian::Dependencies->new($dep)
162             unless ref($dep);
163              
164             $self += $dep;
165             }
166             }
167              
168             =item remove( I, ... )
169             =item remove( I, ... )
170              
171             Removes a dependency from the list of dependencies. Instances of
172             L and L classes are supported as
173             arguments.
174              
175             Any non-reference arguments are coerced to instances of L
176             class.
177              
178             Only dependencies that are subset of the given dependencies are removed:
179              
180             my $deps = Debian::Dependencies->new('foo (>= 1.2), bar');
181             $deps->remove('foo, bar (>= 2.0)');
182             print $deps; # bar
183              
184             Returns the list of the dependencies removed.
185              
186             =cut
187              
188             sub remove {
189             my( $self, @deps ) = @_;
190              
191             my @removed;
192              
193             for my $deps(@deps) {
194             $deps = Debian::Dependencies->new($deps)
195             unless ref($deps);
196              
197             for my $dep(@$deps) {
198             my @kept;
199              
200             for( @$self ) {
201             if( $_->satisfies($dep) ) {
202             push @removed, $_;
203             }
204             else {
205             push @kept, $_;
206             }
207             }
208              
209             @$self = @kept;
210             }
211             }
212              
213             return @removed;
214             }
215              
216             =item has( I )
217              
218             Return true if the dependency list contains given dependency. In other words,
219             this returns true if the list of dependencies guarantees that the given
220             dependency will be satisfied. For example, C satisfies C, but
221             not C<< foo (>= 5) >>.
222              
223             =cut
224              
225             sub has {
226             my( $self, $dep ) = @_;
227              
228             $dep = Debian::Dependency->new($dep)
229             unless eval { $dep->isa('Debian::Dependency') };
230              
231             for( @$self ) {
232             return 1
233             if $_->satisfies($dep);
234             }
235              
236             return 0;
237             }
238              
239             =item prune()
240              
241             This method is deprecated. If you want to sort the dependency list, either call L or use normal perl sorting stuff on the dereferenced array.
242              
243             =cut
244              
245             sub prune {
246             my $self = shift;
247              
248             use Carp ();
249             Carp::croak("prune() is deprecated and does nothing");
250             }
251              
252             =item sort()
253              
254             Sorts the dependency list by package name, version and relation.
255              
256             =cut
257              
258             sub sort {
259             my( $self ) = @_;
260              
261             @$self = sort { $a <=> $b } @$self;
262             }
263              
264             =back
265              
266             =cut
267              
268             1;
269              
270             =head1 SEE ALSO
271              
272             L
273              
274             =head1 AUTHOR
275              
276             =over 4
277              
278             =item Damyan Ivanov
279              
280             =back
281              
282             =head1 COPYRIGHT & LICENSE
283              
284             =over 4
285              
286             =item Copyright (C) 2008, 2009, 2010 Damyan Ivanov
287              
288             =item Copyright (C) 2009 gregor herrmann
289              
290             =back
291              
292             This program is free software; you can redistribute it and/or modify it under
293             the terms of the GNU General Public License version 2 as published by the Free
294             Software Foundation.
295              
296             This program is distributed in the hope that it will be useful, but WITHOUT ANY
297             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
298             PARTICULAR PURPOSE. See the GNU General Public License for more details.
299              
300             You should have received a copy of the GNU General Public License along with
301             this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
302             Street, Fifth Floor, Boston, MA 02110-1301 USA.
303              
304             =cut