OLD | NEW |
(Empty) | |
| 1 # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org> |
| 2 # |
| 3 # This program is free software; you can redistribute it and/or modify |
| 4 # it under the terms of the GNU General Public License as published by |
| 5 # the Free Software Foundation; either version 2 of the License, or |
| 6 # (at your option) any later version. |
| 7 # |
| 8 # This program is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 |
| 16 package Dpkg::Control::Info; |
| 17 |
| 18 use strict; |
| 19 use warnings; |
| 20 |
| 21 our $VERSION = '1.00'; |
| 22 |
| 23 use Dpkg::Control; |
| 24 use Dpkg::ErrorHandling; |
| 25 use Dpkg::Gettext; |
| 26 |
| 27 use parent qw(Dpkg::Interface::Storable); |
| 28 |
| 29 use overload |
| 30 '@{}' => sub { return [ $_[0]->{source}, @{$_[0]->{packages}} ] }; |
| 31 |
| 32 =encoding utf8 |
| 33 |
| 34 =head1 NAME |
| 35 |
| 36 Dpkg::Control::Info - parse files like debian/control |
| 37 |
| 38 =head1 DESCRIPTION |
| 39 |
| 40 It provides an object to access data of files that follow the same |
| 41 syntax as F<debian/control>. |
| 42 |
| 43 =head1 FUNCTIONS |
| 44 |
| 45 =over 4 |
| 46 |
| 47 =item $c = Dpkg::Control::Info->new($file) |
| 48 |
| 49 Create a new Dpkg::Control::Info object for $file. If $file is omitted, it |
| 50 loads F<debian/control>. If file is "-", it parses the standard input. |
| 51 |
| 52 =cut |
| 53 |
| 54 sub new { |
| 55 my ($this, $arg) = @_; |
| 56 my $class = ref($this) || $this; |
| 57 my $self = { |
| 58 source => undef, |
| 59 packages => [], |
| 60 }; |
| 61 bless $self, $class; |
| 62 if ($arg) { |
| 63 $self->load($arg); |
| 64 } else { |
| 65 $self->load('debian/control'); |
| 66 } |
| 67 return $self; |
| 68 } |
| 69 |
| 70 =item $c->reset() |
| 71 |
| 72 Resets what got read. |
| 73 |
| 74 =cut |
| 75 |
| 76 sub reset { |
| 77 my $self = shift; |
| 78 $self->{source} = undef; |
| 79 $self->{packages} = []; |
| 80 } |
| 81 |
| 82 =item $c->load($file) |
| 83 |
| 84 Load the content of $file. Exits in case of errors. If file is "-", it |
| 85 loads from the standard input. |
| 86 |
| 87 =item $c->parse($fh, $description) |
| 88 |
| 89 Parse a control file from the given filehandle. Exits in case of errors. |
| 90 $description is used to describe the filehandle, ideally it's a filename |
| 91 or a description of where the data comes from. It's used in error |
| 92 messages. |
| 93 |
| 94 =cut |
| 95 |
| 96 sub parse { |
| 97 my ($self, $fh, $desc) = @_; |
| 98 $self->reset(); |
| 99 my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC); |
| 100 return if not $cdata->parse($fh, $desc); |
| 101 $self->{source} = $cdata; |
| 102 unless (exists $cdata->{Source}) { |
| 103 $cdata->parse_error($desc, _g('first block lacks a source field')); |
| 104 } |
| 105 while (1) { |
| 106 $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG); |
| 107 last if not $cdata->parse($fh, $desc); |
| 108 push @{$self->{packages}}, $cdata; |
| 109 unless (exists $cdata->{Package}) { |
| 110 $cdata->parse_error($desc, _g("block lacks the '%s' field"), |
| 111 'Package'); |
| 112 } |
| 113 unless (exists $cdata->{Architecture}) { |
| 114 $cdata->parse_error($desc, _g("block lacks the '%s' field"), |
| 115 'Architecture'); |
| 116 } |
| 117 |
| 118 } |
| 119 } |
| 120 |
| 121 =item $c->[0] |
| 122 |
| 123 =item $c->get_source() |
| 124 |
| 125 Returns a Dpkg::Control object containing the fields concerning the |
| 126 source package. |
| 127 |
| 128 =cut |
| 129 |
| 130 sub get_source { |
| 131 my $self = shift; |
| 132 return $self->{source}; |
| 133 } |
| 134 |
| 135 =item $c->get_pkg_by_idx($idx) |
| 136 |
| 137 Returns a Dpkg::Control object containing the fields concerning the binary |
| 138 package numbered $idx (starting at 1). |
| 139 |
| 140 =cut |
| 141 |
| 142 sub get_pkg_by_idx { |
| 143 my ($self, $idx) = @_; |
| 144 return $self->{packages}[--$idx]; |
| 145 } |
| 146 |
| 147 =item $c->get_pkg_by_name($name) |
| 148 |
| 149 Returns a Dpkg::Control object containing the fields concerning the binary |
| 150 package named $name. |
| 151 |
| 152 =cut |
| 153 |
| 154 sub get_pkg_by_name { |
| 155 my ($self, $name) = @_; |
| 156 foreach my $pkg (@{$self->{packages}}) { |
| 157 return $pkg if ($pkg->{Package} eq $name); |
| 158 } |
| 159 return; |
| 160 } |
| 161 |
| 162 |
| 163 =item $c->get_packages() |
| 164 |
| 165 Returns a list containing the Dpkg::Control objects for all binary packages. |
| 166 |
| 167 =cut |
| 168 |
| 169 sub get_packages { |
| 170 my $self = shift; |
| 171 return @{$self->{packages}}; |
| 172 } |
| 173 |
| 174 =item $c->output($filehandle) |
| 175 |
| 176 Dump the content into a filehandle. |
| 177 |
| 178 =cut |
| 179 |
| 180 sub output { |
| 181 my ($self, $fh) = @_; |
| 182 my $str; |
| 183 $str .= $self->{source}->output($fh); |
| 184 foreach my $pkg (@{$self->{packages}}) { |
| 185 print { $fh } "\n"; |
| 186 $str .= "\n" . $pkg->output($fh); |
| 187 } |
| 188 return $str; |
| 189 } |
| 190 |
| 191 =item "$c" |
| 192 |
| 193 Return a string representation of the content. |
| 194 |
| 195 =item @{$c} |
| 196 |
| 197 Return a list of Dpkg::Control objects, the first one is corresponding to |
| 198 source information and the following ones are the binary packages |
| 199 information. |
| 200 |
| 201 =back |
| 202 |
| 203 =head1 AUTHOR |
| 204 |
| 205 Raphaël Hertzog <hertzog@debian.org>. |
| 206 |
| 207 =cut |
| 208 |
| 209 1; |
OLD | NEW |