Vendor.pm 4.4 KB

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