File Coverage

File:Dpkg/Control.pm
Coverage:75.7%

linestmtbrancondsubpodtimecode
1# Copyright © 2007-2009 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
16package Dpkg::Control;
17
18
13
13
13
24
10
135
use strict;
19
13
13
13
18
6
482
use warnings;
20
21our $VERSION = '1.03';
22our @EXPORT = qw(
23    CTRL_UNKNOWN
24    CTRL_INFO_SRC
25    CTRL_INFO_PKG
26    CTRL_INDEX_SRC
27    CTRL_INDEX_PKG
28    CTRL_REPO_RELEASE
29    CTRL_PKG_SRC
30    CTRL_PKG_DEB
31    CTRL_FILE_BUILDINFO
32    CTRL_FILE_CHANGES
33    CTRL_FILE_VENDOR
34    CTRL_FILE_STATUS
35    CTRL_CHANGELOG
36    CTRL_COPYRIGHT_HEADER
37    CTRL_COPYRIGHT_FILES
38    CTRL_COPYRIGHT_LICENSE
39    CTRL_TESTS
40);
41
42
13
13
13
27
4
126
use Exporter qw(import);
43
44
13
13
13
240
11
286
use Dpkg::Gettext;
45
13
13
13
261
7
362
use Dpkg::ErrorHandling;
46
13
13
13
1403
8
379
use Dpkg::Control::Types;
47
13
13
13
1379
12
178
use Dpkg::Control::Hash;
48
13
13
13
25
5
407
use Dpkg::Control::Fields;
49
50
13
13
13
25
8
14
use parent qw(Dpkg::Control::Hash);
51
52=encoding utf8
53
54 - 161
=head1 NAME

Dpkg::Control - parse and manipulate official control-like information

=head1 DESCRIPTION

The Dpkg::Control object is a smart version of Dpkg::Control::Hash.
It associates a type to the control information. That type can be
used to know what fields are allowed and in what order they must be
output.

The types are constants that are exported by default. Here's the full
list:

=over 4

=item CTRL_UNKNOWN

This type is the default type, it indicates that the type of control
information is not yet known.

=item CTRL_INFO_SRC

Corresponds to the first block of information in a F<debian/control> file in
a Debian source package.

=item CTRL_INFO_PKG

Corresponds to subsequent blocks of information in a F<debian/control> file
in a Debian source package.

=item CTRL_REPO_RELEASE

Corresponds to a F<Release> file in a repository.

=item CTRL_INDEX_SRC

Corresponds to an entry in a F<Sources> file of a source package
repository.

=item CTRL_INDEX_PKG

Corresponds to an entry in a F<Packages> file of a binary package
repository.

=item CTRL_PKG_SRC

Corresponds to a .dsc file of a Debian source package.

=item CTRL_PKG_DEB

Corresponds to the F<control> file generated by dpkg-gencontrol
(F<DEBIAN/control>) and to the same file inside .deb packages.

=item CTRL_FILE_BUILDINFO

Corresponds to a .buildinfo file.

=item CTRL_FILE_CHANGES

Corresponds to a .changes file.

=item CTRL_FILE_VENDOR

Corresponds to a vendor file in $Dpkg::CONFDIR/origins/.

=item CTRL_FILE_STATUS

Corresponds to an entry in dpkg's F<status> file ($Dpkg::ADMINDIR/status).

=item CTRL_CHANGELOG

Corresponds to the output of dpkg-parsechangelog.

=item CTRL_COPYRIGHT_HEADER

Corresponds to the header control block in a F<debian/copyright> file in
machine readable format.

=item CTRL_COPYRIGHT_FILES

Corresponds to a files control block in a F<debian/copyright> file in
machine readable format.

=item CTRL_COPYRIGHT_LICENSE

Corresponds to a license control block in a F<debian/copyright> file in
machine readable format.

=item CTRL_TESTS

Corresponds to a package tests control file in F<debian/tests/control>.

=back

=head1 METHODS

All the methods of Dpkg::Control::Hash are available. Those listed below
are either new or overridden with a different behaviour.

=over 4

=item $c = Dpkg::Control->new(%opts)

If the "type" option is given, it's used to setup default values
for other options. See set_options() for more details.

=cut
162
163sub new {
164
848
1
861
    my ($this, %opts) = @_;
165
848
1057
    my $class = ref($this) || $this;
166
167
848
831
    my $self = Dpkg::Control::Hash->new();
168
848
522
    bless $self, $class;
169
848
874
    $self->set_options(%opts);
170
171
848
777
    return $self;
172}
173
174 - 186
=item $c->set_options(%opts)

Changes the value of one or more options. If the "type" option is changed,
it is used first to define default values for others options. The option
"allow_pgp" is set to 1 for CTRL_PKG_SRC, CTRL_FILE_CHANGES and
CTRL_REPO_RELEASE and to 0 otherwise. The option "drop_empty" is set to 0
for CTRL_INFO_PKG and CTRL_INFO_SRC and to 1 otherwise. The option "name"
is set to a textual description of the type of control information.

The output order is also set to match the ordered list returned by
Dpkg::Control::Fields::field_ordered_list($type).

=cut
187
188sub set_options {
189
848
1
770
    my ($self, %opts) = @_;
190
848
706
    if (exists $opts{type}) {
191
848
541
        my $t = $opts{type};
192
848
970
        $$self->{allow_pgp} = ($t & (CTRL_PKG_SRC | CTRL_FILE_CHANGES | CTRL_REPO_RELEASE)) ? 1 : 0;
193
848
638
        $$self->{drop_empty} = ($t & (CTRL_INFO_PKG | CTRL_INFO_SRC)) ?  0 : 1;
194
848
777
        if ($t == CTRL_INFO_SRC) {
195
1
2
            $$self->{name} = g_('general section of control info file');
196        } elsif ($t == CTRL_INFO_PKG) {
197
4
3
            $$self->{name} = g_("package's section of control info file");
198        } elsif ($t == CTRL_CHANGELOG) {
199
819
607
            $$self->{name} = g_('parsed version of changelog');
200        } elsif ($t == CTRL_COPYRIGHT_HEADER) {
201
0
0
            $$self->{name} = g_('header stanza of copyright file');
202        } elsif ($t == CTRL_COPYRIGHT_FILES) {
203
0
0
            $$self->{name} = g_('files stanza of copyright file');
204        } elsif ($t == CTRL_COPYRIGHT_HEADER) {
205
0
0
            $$self->{name} = g_('license stanza of copyright file');
206        } elsif ($t == CTRL_TESTS) {
207
12
9
            $$self->{name} = g_("package's tests control file");
208        } elsif ($t == CTRL_REPO_RELEASE) {
209
0
0
            $$self->{name} = sprintf(g_("repository's %s file"), 'Release');
210        } elsif ($t == CTRL_INDEX_SRC) {
211
0
0
            $$self->{name} = sprintf(g_("entry in repository's %s file"), 'Sources');
212        } elsif ($t == CTRL_INDEX_PKG) {
213
0
0
            $$self->{name} = sprintf(g_("entry in repository's %s file"), 'Packages');
214        } elsif ($t == CTRL_PKG_SRC) {
215
11
12
            $$self->{name} = sprintf(g_('%s file'), '.dsc');
216        } elsif ($t == CTRL_PKG_DEB) {
217
1
3
            $$self->{name} = g_('control info of a .deb package');
218        } elsif ($t == CTRL_FILE_BUILDINFO) {
219
0
0
            $$self->{name} = g_('build information file');
220        } elsif ($t == CTRL_FILE_CHANGES) {
221
0
0
            $$self->{name} = sprintf(g_('%s file'), '.changes');
222        } elsif ($t == CTRL_FILE_VENDOR) {
223
0
0
            $$self->{name} = g_('vendor file');
224        } elsif ($t == CTRL_FILE_STATUS) {
225
0
0
            $$self->{name} = g_("entry in dpkg's status file");
226        }
227
848
768
        $self->set_output_order(field_ordered_list($opts{type}));
228    }
229
230    # Options set by the user override default values
231
848
1516
    $$self->{$_} = $opts{$_} foreach keys %opts;
232}
233
234 - 239
=item $c->get_type()

Returns the type of control information stored. See the type parameter
set during new().

=cut
240
241sub get_type {
242
723
1
364
    my $self = shift;
243
723
706
    return $$self->{type};
244}
245
246=back
247
248 - 267
=head1 CHANGES

=head2 Version 1.03 (dpkg 1.18.11)

New type: CTRL_FILE_BUILDINFO.

=head2 Version 1.02 (dpkg 1.18.8)

New type: CTRL_TESTS.

=head2 Version 1.01 (dpkg 1.18.5)

New types: CTRL_REPO_RELEASE, CTRL_COPYRIGHT_HEADER, CTRL_COPYRIGHT_FILES,
CTRL_COPYRIGHT_LICENSE.

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut
268
2691;