Vendor.pm 4.1 KB

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