Vendor.pm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <http://www.gnu.org/licenses/>.
  15. package Dpkg::Vendor;
  16. use strict;
  17. use warnings;
  18. use Dpkg::ErrorHandling;
  19. use Dpkg::Gettext;
  20. use Dpkg::Control::Hash;
  21. use base qw(Exporter);
  22. our @EXPORT_OK = qw(get_vendor_info get_current_vendor get_vendor_file
  23. get_vendor_object run_vendor_hook);
  24. =head1 NAME
  25. Dpkg::Vendor - get access to some vendor specific information
  26. =head1 DESCRIPTION
  27. The files in /etc/dpkg/origins/ can provide information about various
  28. vendors who are providing Debian packages. Currently those files look like
  29. this:
  30. Vendor: Debian
  31. Vendor-URL: http://www.debian.org/
  32. Bugs: debbugs://bugs.debian.org
  33. The file should be named according to the vendor name.
  34. =head1 FUNCTIONS
  35. =over 4
  36. =item $fields = Dpkg::Vendor::get_vendor_info($name)
  37. Returns a Dpkg::Control object with the information parsed from the
  38. corresponding vendor file in /etc/dpkg/origins/. If $name is omitted,
  39. it will use /etc/dpkg/origins/default which is supposed to be a symlink
  40. to the vendor of the currently installed operating system. Returns undef
  41. if there's no file for the given vendor.
  42. =cut
  43. sub get_vendor_info(;$) {
  44. my $vendor = shift || "default";
  45. my $file = get_vendor_file($vendor);
  46. return undef unless $file;
  47. my $fields = Dpkg::Control::Hash->new();
  48. $fields->load($file) || error(_g("%s is empty"), $file);
  49. return $fields;
  50. }
  51. =item $name = Dpkg::Vendor::get_vendor_file($name)
  52. Check if there's a file for the given vendor and returns its
  53. name.
  54. =cut
  55. sub get_vendor_file(;$) {
  56. my $vendor = shift || "default";
  57. my $file;
  58. my @tries = ($vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor)));
  59. if ($vendor =~ s/\s+/-/) {
  60. push @tries, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  61. }
  62. foreach my $name (@tries) {
  63. $file = "/etc/dpkg/origins/$name" if -e "/etc/dpkg/origins/$name";
  64. }
  65. return $file;
  66. }
  67. =item $name = Dpkg::Vendor::get_current_vendor()
  68. Returns the name of the current vendor. If DEB_VENDOR is set, it uses
  69. that first, otherwise it falls back to parsing /etc/dpkg/origins/default.
  70. If that file doesn't exist, it returns undef.
  71. =cut
  72. sub get_current_vendor() {
  73. my $f;
  74. if ($ENV{'DEB_VENDOR'}) {
  75. $f = get_vendor_info($ENV{'DEB_VENDOR'});
  76. return $f->{'Vendor'} if defined $f;
  77. }
  78. $f = get_vendor_info();
  79. return $f->{'Vendor'} if defined $f;
  80. return undef;
  81. }
  82. =item $object = Dpkg::Vendor::get_vendor_object($name)
  83. Return the Dpkg::Vendor::* object of the corresponding vendor.
  84. If $name is omitted, return the object of the current vendor.
  85. If no vendor can be identified, then return the Dpkg::Vendor::Default
  86. object.
  87. =cut
  88. my %OBJECT_CACHE;
  89. sub get_vendor_object {
  90. my $vendor = shift || get_current_vendor() || "Default";
  91. return $OBJECT_CACHE{$vendor} if exists $OBJECT_CACHE{$vendor};
  92. my ($obj, @names);
  93. if ($vendor ne "Default") {
  94. push @names, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  95. }
  96. foreach my $name (@names, "Default") {
  97. eval qq{
  98. require Dpkg::Vendor::$name;
  99. \$obj = Dpkg::Vendor::$name->new();
  100. };
  101. last unless $@;
  102. }
  103. $OBJECT_CACHE{$vendor} = $obj;
  104. return $obj;
  105. }
  106. =item Dpkg::Vendor::run_vendor_hook($hookid, @params)
  107. Run a hook implemented by the current vendor object.
  108. =cut
  109. sub run_vendor_hook {
  110. my $vendor_obj = get_vendor_object();
  111. $vendor_obj->run_hook(@_);
  112. }
  113. =back
  114. =cut
  115. 1;