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/>.
21 Dpkg::Control::Info - parse files like debian/control
25 It provides a class to access data of files that follow the same
26 syntax as F<debian/control>.
30 package Dpkg
::Control
::Info
1.01;
36 use Dpkg
::ErrorHandling
;
39 use parent
qw(Dpkg::Interface::Storable);
42 '@{}' => sub { return [ $_[0]->{source
}, @
{$_[0]->{packages
}} ] };
48 =item $c = Dpkg::Control::Info->new(%opts)
50 Create a new Dpkg::Control::Info object. Loads F<debian/control> by default.
51 If a single scalar is passed, it will be used as the filename to load
52 instead of the default. If filename is "-", it parses the standard input.
53 If filename is undef no loading will be performed.
61 Loads the file from the filename, instead of F<debian/control>.
68 my ($this, @args) = @_;
69 my $class = ref($this) || $this;
77 if (scalar @args == 0) {
78 $opts{filename
} = 'debian/control';
79 } elsif (scalar @args == 1) {
80 $opts{filename
} = $args[0];
85 $self->load($opts{filename
}) if $opts{filename
};
98 $self->{source
} = undef;
99 $self->{packages
} = [];
102 =item $c->parse($fh, $description)
104 Parse a control file from the given filehandle. Exits in case of errors.
105 $description is used to describe the filehandle, ideally it's a filename
106 or a description of where the data comes from. It is used in error messages.
107 The data in the object is reset before parsing new control files.
112 my ($self, $fh, $desc) = @_;
114 my $cdata = Dpkg
::Control
->new(type
=> CTRL_TMPL_SRC
);
115 return if not $cdata->parse($fh, $desc);
116 $self->{source
} = $cdata;
117 unless (exists $cdata->{Source
}) {
118 $cdata->parse_error($desc, g_
("first stanza lacks a '%s' field"),
122 $cdata = Dpkg
::Control
->new(type
=> CTRL_TMPL_PKG
);
123 last if not $cdata->parse($fh, $desc);
124 push @
{$self->{packages
}}, $cdata;
125 unless (exists $cdata->{Package
}) {
126 $cdata->parse_error($desc, g_
("stanza lacks the '%s' field"),
129 unless (exists $cdata->{Architecture
}) {
130 $cdata->parse_error($desc, g_
("stanza lacks the '%s' field"),
136 =item $c->load($file)
138 Load the content of $file. Exits in case of errors. If file is "-", it
139 loads from the standard input.
143 =item $c->get_source()
145 Returns a L<Dpkg::Control> object containing the fields concerning the
152 return $self->{source
};
155 =item $c->get_pkg_by_idx($idx)
157 Returns a L<Dpkg::Control> object containing the fields concerning the binary
158 package numbered $idx (starting at 1).
163 my ($self, $idx) = @_;
164 return $self->{packages
}[--$idx];
167 =item $c->get_pkg_by_name($name)
169 Returns a L<Dpkg::Control> object containing the fields concerning the binary
174 sub get_pkg_by_name
{
175 my ($self, $name) = @_;
176 foreach my $pkg (@
{$self->{packages
}}) {
177 return $pkg if ($pkg->{Package
} eq $name);
183 =item $c->get_packages()
185 Returns a list containing the L<Dpkg::Control> objects for all binary packages.
191 return @
{$self->{packages
}};
194 =item $str = $c->output([$fh])
196 Return the content info into a string. If $fh is specified print it into
202 my ($self, $fh) = @_;
204 $str .= $self->{source
}->output($fh);
205 foreach my $pkg (@
{$self->{packages
}}) {
206 print { $fh } "\n" if defined $fh;
207 $str .= "\n" . $pkg->output($fh);
214 Return a string representation of the content.
218 Return a list of L<Dpkg::Control> objects, the first one is corresponding to
219 source information and the following ones are the binary packages
226 =head2 Version 1.01 (dpkg 1.18.0)
228 New argument: The $c->new() constructor accepts an %opts argument.
230 =head2 Version 1.00 (dpkg 1.15.6)
232 Mark the module as public.