Vendor.pm 4.3 KB

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