Vendor.pm 4.1 KB

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