File Coverage

File:Dpkg/Vendor.pm
Coverage:87.9%

linestmtbrancondsubpodtimecode
1# Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
2# Copyright © 2008-2009, 2012-2017, 2022 Guillem Jover <guillem@debian.org>
3#
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.
8#
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.
13#
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/>.
16
17=encoding utf8
18
19 - 43
=head1 NAME

Dpkg::Vendor - get access to some vendor specific information

=head1 DESCRIPTION

The files in $Dpkg::CONFDIR/origins/ can provide information about various
vendors who are providing Debian packages. Currently those files look like
this:

  Vendor: Debian
  Vendor-URL: https://www.debian.org/
  Bugs: debbugs://bugs.debian.org

If the vendor derives from another vendor, the file should document
the relationship by listing the base distribution in the Parent field:

  Parent: Debian

The file should be named according to the vendor name. The usual convention
is to name the vendor file using the vendor name in all lowercase, but some
variation is permitted. Namely, spaces are mapped to dashes ('-'), and the
file can have the same casing as the Vendor field, or it can be capitalized.

=cut
44
45package Dpkg::Vendor 1.02;
46
47
138
138
138
400
100
2356
use strict;
48
138
138
138
237
99
3563
use warnings;
49
138
138
138
274
96
7581
use feature qw(state);
50
51our @EXPORT_OK = qw(
52    get_current_vendor
53    get_vendor_info
54    get_vendor_file
55    get_vendor_dir
56    get_vendor_object
57    run_vendor_hook
58);
59
60
138
138
138
299
71
1983
use Exporter qw(import);
61
138
138
138
225
574
4484
use List::Util qw(uniq);
62
63
138
138
138
748
83
1258
use Dpkg ();
64
138
138
138
794
130
4700
use Dpkg::ErrorHandling;
65
138
138
138
282
87
3526
use Dpkg::Gettext;
66
138
138
138
14141
104
2596
use Dpkg::BuildEnv;
67
138
138
138
26062
221
88452
use Dpkg::Control::HashCore;
68
69my $origins = "$Dpkg::CONFDIR/origins";
70$origins = $ENV{DPKG_ORIGINS_DIR} if $ENV{DPKG_ORIGINS_DIR};
71
72 - 81
=head1 FUNCTIONS

=over 4

=item $dir = get_vendor_dir()

Returns the current dpkg origins directory name, where the vendor files
are stored.

=cut
82
83sub get_vendor_dir {
84
3
1
8
    return $origins;
85}
86
87 - 95
=item $fields = get_vendor_info($name)

Returns a L<Dpkg::Control> object with the information parsed from the
corresponding vendor file in $Dpkg::CONFDIR/origins/. If $name is omitted,
it will use $Dpkg::CONFDIR/origins/default which is supposed to be a symlink
to the vendor of the currently installed operating system. Returns undef
if there's no file for the given vendor.

=cut
96
97my $vendor_sep_regex = qr{[^A-Za-z0-9]+};
98
99sub get_vendor_info(;$) {
100
1662
1
2767
    my $vendor = shift || 'default';
101
1662
3829
    my $vendor_key = lc $vendor =~ s{$vendor_sep_regex}{}gr;
102
1662
1033
    state %VENDOR_CACHE;
103
1662
2582
    return $VENDOR_CACHE{$vendor_key} if exists $VENDOR_CACHE{$vendor_key};
104
105
69
95
    my $file = get_vendor_file($vendor);
106
69
86
    return unless $file;
107
69
236
    my $fields = Dpkg::Control::HashCore->new();
108
69
205
    $fields->load($file, compression => 0) or error(g_('%s is empty'), $file);
109
69
92
    $VENDOR_CACHE{$vendor_key} = $fields;
110
69
76
    return $fields;
111}
112
113 - 134
=item $name = get_vendor_file($name)

Check if there's a file for the given vendor and returns its
name.

The vendor filename will be derived from the vendor name, by replacing any
number of non-alphanumeric characters (that is B<[^A-Za-z0-9]>) into "B<->",
then the resulting name will be tried in sequence by lower-casing it,
keeping it as is, lower-casing then capitalizing it, and capitalizing it.

In addition, for historical and backwards compatibility, the name will
be tried keeping it as is without non-alphanumeric characters remapping,
then the resulting name will be tried in sequence by lower-casing it,
keeping it as is, lower-casing then capitalizing it, and capitalizing it.
And finally the name will be tried by replacing only spaces to "B<->",
then the resulting name will be tried in sequence by lower-casing it,
keeping it as is, lower-casing then capitalizing it, and capitalizing it.

But these backwards compatible name lookups will be removed during
the dpkg 1.22.x release cycle.

=cut
135
136sub get_vendor_file(;$) {
137
69
1
105
    my $vendor = shift || 'default';
138
139
69
50
    my @names;
140
69
95
    my $vendor_sep = $vendor =~ s{$vendor_sep_regex}{-}gr;
141
69
165
    push @names, lc $vendor_sep, $vendor_sep, ucfirst lc $vendor_sep, ucfirst $vendor_sep;
142
143    # XXX: Backwards compatibility, remove on 1.22.x.
144
69
276
94
346
    my %name_seen = map { $_ => 1 } @names;
145    my @obsolete_names = uniq grep {
146
69
276
211
181
        my $seen = exists $name_seen{$_};
147
276
192
        $name_seen{$_} = 1;
148
276
316
        not $seen;
149    } (
150        (lc $vendor, $vendor, ucfirst lc $vendor, ucfirst $vendor),
151        ($vendor =~ s{\s+}{-}g) ?
152        (lc $vendor, $vendor, ucfirst lc $vendor, ucfirst $vendor) : ()
153    );
154
69
0
88
0
    my %obsolete_name = map { $_ => 1 } @obsolete_names;
155
69
52
    push @names, @obsolete_names;
156
157
69
179
    foreach my $name (uniq @names) {
158
69
589
        next unless -e "$origins/$name";
159
69
135
        if (exists $obsolete_name{$name}) {
160
0
0
            warning(g_('%s origin filename is deprecated; ' .
161                       'it should have only alphanumeric or dash characters'),
162                    $name);
163        }
164
69
232
        return "$origins/$name";
165    }
166
0
0
    return;
167}
168
169 - 175
=item $name = get_current_vendor()

Returns the name of the current vendor. If DEB_VENDOR is set, it uses
that first, otherwise it falls back to parsing $Dpkg::CONFDIR/origins/default.
If that file doesn't exist, it returns undef.

=cut
176
177sub get_current_vendor() {
178
1659
1
1084
    my $f;
179
1659
2280
    if (Dpkg::BuildEnv::has('DEB_VENDOR')) {
180
771
729
        $f = get_vendor_info(Dpkg::BuildEnv::get('DEB_VENDOR'));
181
771
1169
        return $f->{'Vendor'} if defined $f;
182    }
183
888
1172
    $f = get_vendor_info();
184
888
1850
    return $f->{'Vendor'} if defined $f;
185
0
0
    return;
186}
187
188 - 206
=item $object = get_vendor_object($name)

Return the Dpkg::Vendor::* object of the corresponding vendor.
If $name is omitted, return the object of the current vendor.
If no vendor can be identified, then return the L<Dpkg::Vendor::Default>
object.

The module name will be derived from the vendor name, by splitting parts
around groups of non alphanumeric character (that is B<[^A-Za-z0-9]>)
separators, by either capitalizing or lower-casing and capitalizing each part
and then joining them without the separators. So the expected casing is based
on the one from the B<Vendor> field in the F<origins> file.

In addition, for historical and backwards compatibility, the module name
will also be looked up without non-alphanumeric character stripping, by
capitalizing, lower-casing then capitalizing, as-is or lower-casing.
But these name lookups will be removed during the 1.22.x release cycle.

=cut
207
208sub get_vendor_object {
209
1656
1
3112
    my $vendor = shift || get_current_vendor() || 'Default';
210
1656
2533
    my $vendor_key = lc $vendor =~ s{$vendor_sep_regex}{}gr;
211
1656
952
    state %OBJECT_CACHE;
212
1656
2392
    return $OBJECT_CACHE{$vendor_key} if exists $OBJECT_CACHE{$vendor_key};
213
214
69
52
    my ($obj, @names);
215
216
69
127
    my @vendor_parts = split m{$vendor_sep_regex}, $vendor;
217
69
69
75
163
    push @names, join q{}, map { ucfirst } @vendor_parts;
218
69
69
66
87
    push @names, join q{}, map { ucfirst lc } @vendor_parts;
219
220    # XXX: Backwards compatibility, remove on 1.22.x.
221
69
138
54
211
    my %name_seen = map { $_ => 1 } @names;
222    my @obsolete_names = uniq grep {
223
69
276
109
175
        my $seen = exists $name_seen{$_};
224
276
168
        $name_seen{$_} = 1;
225
276
297
        not $seen;
226    } (ucfirst $vendor, ucfirst lc $vendor, $vendor, lc $vendor);
227
69
72
71
76
    my %obsolete_name = map { $_ => 1 } @obsolete_names;
228
69
70
    push @names, @obsolete_names;
229
230
69
126
    foreach my $name (uniq @names) {
231
78
3763
        eval qq{
232            require Dpkg::Vendor::$name;
233            \$obj = Dpkg::Vendor::$name->new();
234        };
235
78
312
        unless ($@) {
236
66
96
            $OBJECT_CACHE{$vendor_key} = $obj;
237
66
142
            if (exists $obsolete_name{$name}) {
238
0
0
                warning(g_('%s module name is deprecated; ' .
239                           'it should be capitalized with only alphanumeric characters'),
240                        "Dpkg::Vendor::$name");
241            }
242
66
200
            return $obj;
243        }
244    }
245
246
3
6
    my $info = get_vendor_info($vendor);
247
3
8
    if (defined $info and defined $info->{'Parent'}) {
248
3
2
        return get_vendor_object($info->{'Parent'});
249    } else {
250
0
0
        return get_vendor_object('Default');
251    }
252}
253
254 - 258
=item run_vendor_hook($hookid, @params)

Run a hook implemented by the current vendor object.

=cut
259
260sub run_vendor_hook {
261
1641
1
1820
    my @args = @_;
262
1641
1724
    my $vendor_obj = get_vendor_object();
263
264
1641
2754
    $vendor_obj->run_hook(@args);
265}
266
267=back
268
269 - 289
=head1 CHANGES

=head2 Version 1.02 (dpkg 1.21.10)

Deprecated behavior: get_vendor_file() loading vendor files with no special
characters remapping. get_vendor_object() loading vendor module names with
no special character stripping.

=head2 Version 1.01 (dpkg 1.17.0)

New function: get_vendor_dir().

=head2 Version 1.00 (dpkg 1.16.1)

Mark the module as public.

=head1 SEE ALSO

L<deb-origin(5)>.

=cut
290
2911;