File Coverage

blib/lib/Dpkg/Vendor.pm
Criterion Covered Total %
statement 65 68 95.5
branch 16 22 72.7
condition 5 9 55.5
subroutine 15 15 100.0
pod 6 6 100.0
total 107 120 89.1


line stmt bran cond sub pod time code
1             # Copyright © 2008-2009 Raphaël Hertzog
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 .
15              
16             package Dpkg::Vendor;
17              
18 13     13   796 use strict;
  13         49  
  13         438  
19 13     13   66 use warnings;
  13         27  
  13         356  
20 13     13   68 use feature qw(state);
  13         23  
  13         1561  
21              
22             our $VERSION = '1.01';
23             our @EXPORT_OK = qw(
24             get_current_vendor
25             get_vendor_info
26             get_vendor_file
27             get_vendor_dir
28             get_vendor_object
29             run_vendor_hook
30             );
31              
32 13     13   92 use Exporter qw(import);
  13         32  
  13         376  
33              
34 13     13   485 use Dpkg ();
  13         33  
  13         309  
35 13     13   511 use Dpkg::ErrorHandling;
  13         29  
  13         1121  
36 13     13   88 use Dpkg::Gettext;
  13         27  
  13         764  
37 13     13   3539 use Dpkg::Build::Env;
  13         29  
  13         431  
38 13     13   6637 use Dpkg::Control::HashCore;
  13         39  
  13         9129  
39              
40             my $origins = "$Dpkg::CONFDIR/origins";
41             $origins = $ENV{DPKG_ORIGINS_DIR} if $ENV{DPKG_ORIGINS_DIR};
42              
43             =encoding utf8
44              
45             =head1 NAME
46              
47             Dpkg::Vendor - get access to some vendor specific information
48              
49             =head1 DESCRIPTION
50              
51             The files in $Dpkg::CONFDIR/origins/ can provide information about various
52             vendors who are providing Debian packages. Currently those files look like
53             this:
54              
55             Vendor: Debian
56             Vendor-URL: https://www.debian.org/
57             Bugs: debbugs://bugs.debian.org
58              
59             If the vendor derives from another vendor, the file should document
60             the relationship by listing the base distribution in the Parent field:
61              
62             Parent: Debian
63              
64             The file should be named according to the vendor name. The usual convention
65             is to name the vendor file using the vendor name in all lowercase, but some
66             variation is permitted. Namely, spaces are mapped to dashes ('-'), and the
67             file can have the same casing as the Vendor field, or it can be capitalized.
68              
69             =head1 FUNCTIONS
70              
71             =over 4
72              
73             =item $dir = get_vendor_dir()
74              
75             Returns the current dpkg origins directory name, where the vendor files
76             are stored.
77              
78             =cut
79              
80             sub get_vendor_dir {
81 1     1 1 639 return $origins;
82             }
83              
84             =item $fields = get_vendor_info($name)
85              
86             Returns a Dpkg::Control object with the information parsed from the
87             corresponding vendor file in $Dpkg::CONFDIR/origins/. If $name is omitted,
88             it will use $Dpkg::CONFDIR/origins/default which is supposed to be a symlink
89             to the vendor of the currently installed operating system. Returns undef
90             if there's no file for the given vendor.
91              
92             =cut
93              
94             sub get_vendor_info(;$) {
95 518   100 518 1 1288 my $vendor = shift || 'default';
96 518         652 state %VENDOR_CACHE;
97 518 100       1299 return $VENDOR_CACHE{$vendor} if exists $VENDOR_CACHE{$vendor};
98              
99 14         48 my $file = get_vendor_file($vendor);
100 14 50       51 return unless $file;
101 14         131 my $fields = Dpkg::Control::HashCore->new();
102 14 50       101 $fields->load($file, compression => 0) or error(g_('%s is empty'), $file);
103 14         53 $VENDOR_CACHE{$vendor} = $fields;
104 14         39 return $fields;
105             }
106              
107             =item $name = get_vendor_file($name)
108              
109             Check if there's a file for the given vendor and returns its
110             name.
111              
112             =cut
113              
114             sub get_vendor_file(;$) {
115 14   50 14 1 60 my $vendor = shift || 'default';
116 14         23 my $file;
117 14         83 my @tries = ($vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor)));
118 14 50       68 if ($vendor =~ s/\s+/-/) {
119 0         0 push @tries, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
120             }
121 14         33 foreach my $name (@tries) {
122 56 100       904 $file = "$origins/$name" if -e "$origins/$name";
123             }
124 14         76 return $file;
125             }
126              
127             =item $name = get_current_vendor()
128              
129             Returns the name of the current vendor. If DEB_VENDOR is set, it uses
130             that first, otherwise it falls back to parsing $Dpkg::CONFDIR/origins/default.
131             If that file doesn't exist, it returns undef.
132              
133             =cut
134              
135             sub get_current_vendor() {
136 517     517 1 692 my $f;
137 517 100       1308 if (Dpkg::Build::Env::has('DEB_VENDOR')) {
138 254         525 $f = get_vendor_info(Dpkg::Build::Env::get('DEB_VENDOR'));
139 254 50       721 return $f->{'Vendor'} if defined $f;
140             }
141 263         590 $f = get_vendor_info();
142 263 50       827 return $f->{'Vendor'} if defined $f;
143 0         0 return;
144             }
145              
146             =item $object = get_vendor_object($name)
147              
148             Return the Dpkg::Vendor::* object of the corresponding vendor.
149             If $name is omitted, return the object of the current vendor.
150             If no vendor can be identified, then return the Dpkg::Vendor::Default
151             object.
152              
153             =cut
154              
155             sub get_vendor_object {
156 515   50 515 1 3118 my $vendor = shift || get_current_vendor() || 'Default';
157 515         887 state %OBJECT_CACHE;
158 515 100       1393 return $OBJECT_CACHE{$vendor} if exists $OBJECT_CACHE{$vendor};
159              
160 13         28 my ($obj, @names);
161 13         49 push @names, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
162              
163 13         109 foreach my $name (@names) {
164 16         1827 eval qq{
165             pop \@INC if \$INC[-1] eq '.';
166             require Dpkg::Vendor::$name;
167             \$obj = Dpkg::Vendor::$name->new();
168             };
169 16 100       123 unless ($@) {
170 12         36 $OBJECT_CACHE{$vendor} = $obj;
171 12         42 return $obj;
172             }
173             }
174              
175 1         3 my $info = get_vendor_info($vendor);
176 1 50 33     7 if (defined $info and defined $info->{'Parent'}) {
177 1         4 return get_vendor_object($info->{'Parent'});
178             } else {
179 0         0 return get_vendor_object('Default');
180             }
181             }
182              
183             =item run_vendor_hook($hookid, @params)
184              
185             Run a hook implemented by the current vendor object.
186              
187             =cut
188              
189             sub run_vendor_hook {
190 511     511 1 1122 my $vendor_obj = get_vendor_object();
191 511         1548 $vendor_obj->run_hook(@_);
192             }
193              
194             =back
195              
196             =head1 CHANGES
197              
198             =head2 Version 1.01 (dpkg 1.17.0)
199              
200             New function: get_vendor_dir().
201              
202             =head2 Version 1.00 (dpkg 1.16.1)
203              
204             Mark the module as public.
205              
206             =head1 SEE ALSO
207              
208             deb-origin(5).
209              
210             =cut
211              
212             1;