Vendor.pm 5.0 KB

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