summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Debian/Control.pm
blob: cd99302bd96ef2a0ac7c7ff53ac5a83363ff9f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- perl -*-
# Lintian::Debian::Control -- object for fields in d/control

# Copyright (C) 2008 Russ Allbery
# Copyright (C) 2009 Raphael Geissert
# Copyright (C) 2020 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

package Lintian::Debian::Control;

use v5.20;
use warnings;
use utf8;

use Path::Tiny;
use Syntax::Keyword::Try;
use Unicode::UTF8 qw(valid_utf8 decode_utf8 encode_utf8);

use Lintian::Deb822;
use Lintian::Deb822::Section;
use Lintian::Util qw($PKGNAME_REGEX);

use Moo;
use namespace::clean;

=head1 NAME

Lintian::Debian::Control - Lintian interface to d/control fields

=head1 SYNOPSIS

    use Lintian::Debian::Control;

=head1 DESCRIPTION

Lintian::Debian::Control provides access to fields in d/control.

=head1 INSTANCE METHODS

=over 4

=item item
=item source_fields
=item installable_fields_by_name

=cut

has item => (is => 'rw');

has source_fields => (
    is => 'rw',
    default => sub { return Lintian::Deb822::Section->new; },
    coerce => sub {
        my ($blessedref) = @_;
        return ($blessedref // Lintian::Deb822::Section->new);
    },
);

has installable_fields_by_name => (
    is => 'rw',
    default => sub { {} },
    coerce => sub { my ($hashref) = @_; return ($hashref // {}); },
);

=item load

=cut

sub load {
    my ($self, $item) = @_;

    return
      unless defined $item;

    $self->item($item);

    return
      unless -r $item->unpacked_path;

    my $deb822 = Lintian::Deb822->new;

    my @sections;
    try {
        @sections = $deb822->read_file($item->unpacked_path);

    } catch {
        # If it is a syntax error, ignore it (we emit
        # syntax-error-in-control-file in this case via
        # control-file).
        die map { encode_utf8($_) } $@
          unless $@ =~ /syntax error/;

        return;
    }

    # in theory, one could craft a package in which d/control is empty
    my $source = shift @sections;
    $self->source_fields($source);

    my @named
      = grep { $_->value('Package') =~ m{\A $PKGNAME_REGEX \Z}x }@sections;

    my %by_name = map { $_->value('Package') => $_ } @named;

    $self->installable_fields_by_name(\%by_name);

    return;
}

=item installables

Returns a list of the binary and udeb packages listed in the
F<debian/control>.

=cut

sub installables {
    my ($self) = @_;

    return keys %{$self->installable_fields_by_name};
}

=item installable_package_type (NAME)

Returns package type based on value of the Package-Type (or if absent,
X-Package-Type) field.  If the field is omitted, the default value
"deb" is used.

If NAME is not an installable listed in the source packages
F<debian/control> file, this method return C<undef>.

=cut

sub installable_package_type {
    my ($self, $name) = @_;

    my $type;

    my $fields = $self->installable_fields_by_name->{$name};

    $type = $fields->value('Package-Type') || $fields->value('XC-Package-Type')
      if defined $fields;

    $type ||= 'deb';

    return lc $type;
}

=item installable_fields (PACKAGE)

Returns the Deb822::Section object for the installable. Returns an
empty object if the installable does not exist.

=cut

sub installable_fields {
    my ($self, $package) = @_;

    my $per_package;

    $per_package = $self->installable_fields_by_name->{$package}
      if length $package;

    return ($per_package // Lintian::Deb822::Section->new);
}

=back

=head1 AUTHOR

Originally written by Russ Allbery <rra@debian.org> for Lintian.
Amended by Felix Lechner <felix.lechner@lease-up.com> for Lintian.

=head1 SEE ALSO

lintian(1)

=cut

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et