Vendor.pm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Copyright © 2008-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. package Dpkg::Vendor;
  16. use strict;
  17. use warnings;
  18. use feature qw(state);
  19. our $VERSION = '1.01';
  20. our @EXPORT_OK = qw(
  21. get_current_vendor
  22. get_vendor_info
  23. get_vendor_file
  24. get_vendor_dir
  25. get_vendor_object
  26. run_vendor_hook
  27. );
  28. use Exporter qw(import);
  29. use Dpkg ();
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Gettext;
  32. use Dpkg::Build::Env;
  33. use Dpkg::Control::HashCore;
  34. my $origins = "$Dpkg::CONFDIR/origins";
  35. $origins = $ENV{DPKG_ORIGINS_DIR} if $ENV{DPKG_ORIGINS_DIR};
  36. =encoding utf8
  37. =head1 NAME
  38. Dpkg::Vendor - get access to some vendor specific information
  39. =head1 DESCRIPTION
  40. The files in $Dpkg::CONFDIR/origins/ can provide information about various
  41. vendors who are providing Debian packages. Currently those files look like
  42. this:
  43. Vendor: Debian
  44. Vendor-URL: https://www.debian.org/
  45. Bugs: debbugs://bugs.debian.org
  46. If the vendor derives from another vendor, the file should document
  47. the relationship by listing the base distribution in the Parent field:
  48. Parent: Debian
  49. The file should be named according to the vendor name. The usual convention
  50. is to name the vendor file using the vendor name in all lowercase, but some
  51. variation is permitted. Namely, spaces are mapped to dashes ('-'), and the
  52. file can have the same casing as the Vendor field, or it can be capitalized.
  53. =head1 FUNCTIONS
  54. =over 4
  55. =item $dir = Dpkg::Vendor::get_vendor_dir()
  56. Returns the current dpkg origins directory name, where the vendor files
  57. are stored.
  58. =cut
  59. sub get_vendor_dir {
  60. return $origins;
  61. }
  62. =item $fields = Dpkg::Vendor::get_vendor_info($name)
  63. Returns a Dpkg::Control object with the information parsed from the
  64. corresponding vendor file in $Dpkg::CONFDIR/origins/. If $name is omitted,
  65. it will use $Dpkg::CONFDIR/origins/default which is supposed to be a symlink
  66. to the vendor of the currently installed operating system. Returns undef
  67. if there's no file for the given vendor.
  68. =cut
  69. sub get_vendor_info(;$) {
  70. my $vendor = shift || 'default';
  71. state %VENDOR_CACHE;
  72. return $VENDOR_CACHE{$vendor} if exists $VENDOR_CACHE{$vendor};
  73. my $file = get_vendor_file($vendor);
  74. return unless $file;
  75. my $fields = Dpkg::Control::HashCore->new();
  76. $fields->load($file) or error(g_('%s is empty'), $file);
  77. $VENDOR_CACHE{$vendor} = $fields;
  78. return $fields;
  79. }
  80. =item $name = Dpkg::Vendor::get_vendor_file($name)
  81. Check if there's a file for the given vendor and returns its
  82. name.
  83. =cut
  84. sub get_vendor_file(;$) {
  85. my $vendor = shift || 'default';
  86. my $file;
  87. my @tries = ($vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor)));
  88. if ($vendor =~ s/\s+/-/) {
  89. push @tries, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  90. }
  91. foreach my $name (@tries) {
  92. $file = "$origins/$name" if -e "$origins/$name";
  93. }
  94. return $file;
  95. }
  96. =item $name = Dpkg::Vendor::get_current_vendor()
  97. Returns the name of the current vendor. If DEB_VENDOR is set, it uses
  98. that first, otherwise it falls back to parsing $Dpkg::CONFDIR/origins/default.
  99. If that file doesn't exist, it returns undef.
  100. =cut
  101. sub get_current_vendor() {
  102. my $f;
  103. if (Dpkg::Build::Env::has('DEB_VENDOR')) {
  104. $f = get_vendor_info(Dpkg::Build::Env::get('DEB_VENDOR'));
  105. return $f->{'Vendor'} if defined $f;
  106. }
  107. $f = get_vendor_info();
  108. return $f->{'Vendor'} if defined $f;
  109. return;
  110. }
  111. =item $object = Dpkg::Vendor::get_vendor_object($name)
  112. Return the Dpkg::Vendor::* object of the corresponding vendor.
  113. If $name is omitted, return the object of the current vendor.
  114. If no vendor can be identified, then return the Dpkg::Vendor::Default
  115. object.
  116. =cut
  117. sub get_vendor_object {
  118. my $vendor = shift || get_current_vendor() || 'Default';
  119. state %OBJECT_CACHE;
  120. return $OBJECT_CACHE{$vendor} if exists $OBJECT_CACHE{$vendor};
  121. my ($obj, @names);
  122. push @names, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  123. foreach my $name (@names) {
  124. eval qq{
  125. pop \@INC if \$INC[-1] eq '.';
  126. require Dpkg::Vendor::$name;
  127. \$obj = Dpkg::Vendor::$name->new();
  128. };
  129. unless ($@) {
  130. $OBJECT_CACHE{$vendor} = $obj;
  131. return $obj;
  132. }
  133. }
  134. my $info = get_vendor_info($vendor);
  135. if (defined $info and defined $info->{'Parent'}) {
  136. return get_vendor_object($info->{'Parent'});
  137. } else {
  138. return get_vendor_object('Default');
  139. }
  140. }
  141. =item Dpkg::Vendor::run_vendor_hook($hookid, @params)
  142. Run a hook implemented by the current vendor object.
  143. =cut
  144. sub run_vendor_hook {
  145. my $vendor_obj = get_vendor_object();
  146. $vendor_obj->run_hook(@_);
  147. }
  148. =back
  149. =head1 CHANGES
  150. =head2 Version 1.01 (dpkg 1.17.0)
  151. New function: get_vendor_dir().
  152. =head2 Version 1.00 (dpkg 1.16.1)
  153. Mark the module as public.
  154. =head1 SEE ALSO
  155. deb-origin(5).
  156. =cut
  157. 1;