Vendor.pm 5.0 KB

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