1 # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2009, 2012-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
17 package Dpkg::Control::Info;
22 our $VERSION = '1.01';
25 use Dpkg::ErrorHandling;
28 use parent qw(Dpkg::Interface::Storable);
31 '@{}' => sub { return [ $_[0]->{source}, @{$_[0]->{packages}} ] };
37 Dpkg::Control::Info - parse files like debian/control
41 It provides an object to access data of files that follow the same
42 syntax as F<debian/control>.
48 =item $c = Dpkg::Control::Info->new(%opts)
50 Create a new Dpkg::Control::Info object. Loads the file from the filename
51 option, if no option is specified filename defaults to F<debian/control>.
52 If a scalar is passed instead, it will be used as the filename. If filename
53 is "-", it parses the standard input. If filename is undef no loading will
59 my ($this, @args) = @_;
60 my $class = ref($this) || $this;
68 if (scalar @args == 0) {
69 $opts{filename} = 'debian/control';
70 } elsif (scalar @args == 1) {
71 $opts{filename} = $args[0];
76 $self->load($opts{filename}) if $opts{filename};
89 $self->{source} = undef;
90 $self->{packages} = [];
95 Load the content of $file. Exits in case of errors. If file is "-", it
96 loads from the standard input.
98 =item $c->parse($fh, $description)
100 Parse a control file from the given filehandle. Exits in case of errors.
101 $description is used to describe the filehandle, ideally it's a filename
102 or a description of where the data comes from. It is used in error messages.
103 The data in the object is reset before parsing new control files.
108 my ($self, $fh, $desc) = @_;
110 my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC);
111 return if not $cdata->parse($fh, $desc);
112 $self->{source} = $cdata;
113 unless (exists $cdata->{Source}) {
114 $cdata->parse_error($desc, g_('first block lacks a Source field'));
117 $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
118 last if not $cdata->parse($fh, $desc);
119 push @{$self->{packages}}, $cdata;
120 unless (exists $cdata->{Package}) {
121 $cdata->parse_error($desc, g_("block lacks the '%s' field"),
124 unless (exists $cdata->{Architecture}) {
125 $cdata->parse_error($desc, g_("block lacks the '%s' field"),
134 =item $c->get_source()
136 Returns a Dpkg::Control object containing the fields concerning the
143 return $self->{source};
146 =item $c->get_pkg_by_idx($idx)
148 Returns a Dpkg::Control object containing the fields concerning the binary
149 package numbered $idx (starting at 1).
154 my ($self, $idx) = @_;
155 return $self->{packages}[--$idx];
158 =item $c->get_pkg_by_name($name)
160 Returns a Dpkg::Control object containing the fields concerning the binary
165 sub get_pkg_by_name {
166 my ($self, $name) = @_;
167 foreach my $pkg (@{$self->{packages}}) {
168 return $pkg if ($pkg->{Package} eq $name);
174 =item $c->get_packages()
176 Returns a list containing the Dpkg::Control objects for all binary packages.
182 return @{$self->{packages}};
185 =item $c->output($filehandle)
187 Dump the content into a filehandle.
192 my ($self, $fh) = @_;
194 $str .= $self->{source}->output($fh);
195 foreach my $pkg (@{$self->{packages}}) {
196 print { $fh } "\n" if defined $fh;
197 $str .= "\n" . $pkg->output($fh);
204 Return a string representation of the content.
208 Return a list of Dpkg::Control objects, the first one is corresponding to
209 source information and the following ones are the binary packages
216 =head2 Version 1.01 (dpkg 1.18.0)
218 New argument: The $c->new() constructor accepts an %opts argument.
220 =head2 Version 1.00 (dpkg 1.15.6)
222 Mark the module as public.