| File: | Dpkg/Control/Info.pm |
| Coverage: | 86.0% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org> | ||||||
| 2 | # Copyright © 2009, 2012-2015 Guillem Jover <guillem@debian.org> | ||||||
| 3 | # | ||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||
| 7 | # (at your option) any later version. | ||||||
| 8 | # | ||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
| 12 | # GNU General Public License for more details. | ||||||
| 13 | # | ||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 16 | |||||||
| 17 | =encoding utf8 | ||||||
| 18 | |||||||
| 19 - 28 | =head1 NAME Dpkg::Control::Info - parse files like debian/control =head1 DESCRIPTION It provides a class to access data of files that follow the same syntax as F<debian/control>. =cut | ||||||
| 29 | |||||||
| 30 | package Dpkg::Control::Info 1.01; | ||||||
| 31 | |||||||
| 32 | 6 6 6 | 19 5 141 | use strict; | ||||
| 33 | 6 6 6 | 14 5 160 | use warnings; | ||||
| 34 | |||||||
| 35 | 6 6 6 | 13 4 279 | use Dpkg::Control; | ||||
| 36 | 6 6 6 | 13 10 244 | use Dpkg::ErrorHandling; | ||||
| 37 | 6 6 6 | 14 8 206 | use Dpkg::Gettext; | ||||
| 38 | |||||||
| 39 | 6 6 6 | 13 2 19 | use parent qw(Dpkg::Interface::Storable); | ||||
| 40 | |||||||
| 41 | use overload | ||||||
| 42 | 6 6 6 6 6 | 389 6 31 9 16 | '@{}' => sub { return [ $_[0]->{source}, @{$_[0]->{packages}} ] }; | ||||
| 43 | |||||||
| 44 - 56 | =head1 METHODS =over 4 =item $c = Dpkg::Control::Info->new(%opts) Create a new Dpkg::Control::Info object. Loads the file from the filename option, if no option is specified filename defaults to F<debian/control>. If a scalar is passed instead, it will be used as the filename. If filename is "-", it parses the standard input. If filename is undef no loading will be performed. =cut | ||||||
| 57 | |||||||
| 58 | sub new { | ||||||
| 59 | 24 | 1 | 57 | my ($this, @args) = @_; | |||
| 60 | 24 | 145 | my $class = ref($this) || $this; | ||||
| 61 | 24 | 62 | my $self = { | ||||
| 62 | source => undef, | ||||||
| 63 | packages => [], | ||||||
| 64 | }; | ||||||
| 65 | 24 | 35 | bless $self, $class; | ||||
| 66 | |||||||
| 67 | 24 | 26 | my %opts; | ||||
| 68 | 24 | 61 | if (scalar @args == 0) { | ||||
| 69 | 0 | 0 | $opts{filename} = 'debian/control'; | ||||
| 70 | } elsif (scalar @args == 1) { | ||||||
| 71 | 3 | 5 | $opts{filename} = $args[0]; | ||||
| 72 | } else { | ||||||
| 73 | 21 | 41 | %opts = @args; | ||||
| 74 | } | ||||||
| 75 | |||||||
| 76 | 24 | 74 | $self->load($opts{filename}) if $opts{filename}; | ||||
| 77 | |||||||
| 78 | 24 | 67 | return $self; | ||||
| 79 | } | ||||||
| 80 | |||||||
| 81 - 85 | =item $c->reset() Resets what got read. =cut | ||||||
| 86 | |||||||
| 87 | sub reset { | ||||||
| 88 | 24 | 1 | 19 | my $self = shift; | |||
| 89 | 24 | 167 | $self->{source} = undef; | ||||
| 90 | 24 | 29 | $self->{packages} = []; | ||||
| 91 | } | ||||||
| 92 | |||||||
| 93 - 100 | =item $c->parse($fh, $description) Parse a control file from the given filehandle. Exits in case of errors. $description is used to describe the filehandle, ideally it's a filename or a description of where the data comes from. It is used in error messages. The data in the object is reset before parsing new control files. =cut | ||||||
| 101 | |||||||
| 102 | sub parse { | ||||||
| 103 | 24 | 1 | 39 | my ($self, $fh, $desc) = @_; | |||
| 104 | 24 | 42 | $self->reset(); | ||||
| 105 | 24 | 95 | my $cdata = Dpkg::Control->new(type => CTRL_TMPL_SRC); | ||||
| 106 | 24 | 51 | return if not $cdata->parse($fh, $desc); | ||||
| 107 | 24 | 37 | $self->{source} = $cdata; | ||||
| 108 | 24 | 24 | unless (exists $cdata->{Source}) { | ||||
| 109 | 0 | 0 | $cdata->parse_error($desc, g_("first stanza lacks a '%s' field"), | ||||
| 110 | 'Source'); | ||||||
| 111 | } | ||||||
| 112 | 24 | 29 | while (1) { | ||||
| 113 | 54 | 86 | $cdata = Dpkg::Control->new(type => CTRL_TMPL_PKG); | ||||
| 114 | 54 | 68 | last if not $cdata->parse($fh, $desc); | ||||
| 115 | 30 30 | 26 35 | push @{$self->{packages}}, $cdata; | ||||
| 116 | 30 | 39 | unless (exists $cdata->{Package}) { | ||||
| 117 | 0 | 0 | $cdata->parse_error($desc, g_("stanza lacks the '%s' field"), | ||||
| 118 | 'Package'); | ||||||
| 119 | } | ||||||
| 120 | 30 | 32 | unless (exists $cdata->{Architecture}) { | ||||
| 121 | 0 | 0 | $cdata->parse_error($desc, g_("stanza lacks the '%s' field"), | ||||
| 122 | 'Architecture'); | ||||||
| 123 | } | ||||||
| 124 | } | ||||||
| 125 | } | ||||||
| 126 | |||||||
| 127 - 139 | =item $c->load($file) Load the content of $file. Exits in case of errors. If file is "-", it loads from the standard input. =item $c->[0] =item $c->get_source() Returns a L<Dpkg::Control> object containing the fields concerning the source package. =cut | ||||||
| 140 | |||||||
| 141 | sub get_source { | ||||||
| 142 | 24 | 1 | 19 | my $self = shift; | |||
| 143 | 24 | 32 | return $self->{source}; | ||||
| 144 | } | ||||||
| 145 | |||||||
| 146 - 151 | =item $c->get_pkg_by_idx($idx) Returns a L<Dpkg::Control> object containing the fields concerning the binary package numbered $idx (starting at 1). =cut | ||||||
| 152 | |||||||
| 153 | sub get_pkg_by_idx { | ||||||
| 154 | 6 | 1 | 8 | my ($self, $idx) = @_; | |||
| 155 | 6 | 11 | return $self->{packages}[--$idx]; | ||||
| 156 | } | ||||||
| 157 | |||||||
| 158 - 163 | =item $c->get_pkg_by_name($name) Returns a L<Dpkg::Control> object containing the fields concerning the binary package named $name. =cut | ||||||
| 164 | |||||||
| 165 | sub get_pkg_by_name { | ||||||
| 166 | 3 | 1 | 4 | my ($self, $name) = @_; | |||
| 167 | 3 3 | 3 4 | foreach my $pkg (@{$self->{packages}}) { | ||||
| 168 | 9 | 9 | return $pkg if ($pkg->{Package} eq $name); | ||||
| 169 | } | ||||||
| 170 | 0 | 0 | return; | ||||
| 171 | } | ||||||
| 172 | |||||||
| 173 | |||||||
| 174 - 178 | =item $c->get_packages() Returns a list containing the L<Dpkg::Control> objects for all binary packages. =cut | ||||||
| 179 | |||||||
| 180 | sub get_packages { | ||||||
| 181 | 0 | 1 | 0 | my $self = shift; | |||
| 182 | 0 0 | 0 0 | return @{$self->{packages}}; | ||||
| 183 | } | ||||||
| 184 | |||||||
| 185 - 190 | =item $str = $c->output([$fh]) Return the content info into a string. If $fh is specified print it into the filehandle. =cut | ||||||
| 191 | |||||||
| 192 | sub output { | ||||||
| 193 | 3 | 1 | 6 | my ($self, $fh) = @_; | |||
| 194 | 3 | 3 | my $str; | ||||
| 195 | 3 | 35 | $str .= $self->{source}->output($fh); | ||||
| 196 | 3 3 | 6 30 | foreach my $pkg (@{$self->{packages}}) { | ||||
| 197 | 9 9 | 16 10 | print { $fh } "\n" if defined $fh; | ||||
| 198 | 9 | 22 | $str .= "\n" . $pkg->output($fh); | ||||
| 199 | } | ||||||
| 200 | 3 | 4 | return $str; | ||||
| 201 | } | ||||||
| 202 | |||||||
| 203 - 225 | =item "$c"
Return a string representation of the content.
=item @{$c}
Return a list of L<Dpkg::Control> objects, the first one is corresponding to
source information and the following ones are the binary packages
information.
=back
=head1 CHANGES
=head2 Version 1.01 (dpkg 1.18.0)
New argument: The $c->new() constructor accepts an %opts argument.
=head2 Version 1.00 (dpkg 1.15.6)
Mark the module as public.
=cut | ||||||
| 226 | |||||||
| 227 | 1; | ||||||