Vendor.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Vendor;
  14. use strict;
  15. use warnings;
  16. use Dpkg::ErrorHandling;
  17. use Dpkg::Gettext;
  18. use Dpkg::Cdata;
  19. use base qw(Exporter);
  20. our @EXPORT_OK = qw(get_vendor_info get_current_vendor get_vendor_file
  21. get_vendor_object run_vendor_hook);
  22. =head1 NAME
  23. Dpkg::Vendor - get access to some vendor specific information
  24. =head1 DESCRIPTION
  25. The files in /etc/dpkg/origins/ can provide information about various
  26. vendors who are providing Debian packages. Currently those files look like
  27. this:
  28. Vendor: Debian
  29. Vendor-URL: http://www.debian.org/
  30. Bugs: debbugs://bugs.debian.org
  31. The file should be named according to the vendor name.
  32. =head1 FUNCTIONS
  33. =over 4
  34. =item $fields = Dpkg::Vendor::get_vendor_info($name)
  35. Returns a Dpkg::Fields object with the information parsed from the
  36. corresponding vendor file in /etc/dpkg/origins/. If $name is omitted,
  37. it will use /etc/dpkg/origins/default which is supposed to be a symlink
  38. to the vendor of the currently installed operating system. Returns undef
  39. if there's no file for the given vendor.
  40. =cut
  41. sub get_vendor_info(;$) {
  42. my $vendor = shift || "default";
  43. my $file = get_vendor_file($vendor);
  44. return undef unless $file;
  45. open(my $fh, "<", $file) || syserr(_g("cannot read %s"), $file);
  46. my $fields = parsecdata($fh, $file);
  47. close($fh);
  48. return $fields;
  49. }
  50. =item $name = Dpkg::Vendor::get_vendor_file($name)
  51. Check if there's a file for the given vendor and returns its
  52. name.
  53. =cut
  54. sub get_vendor_file(;$) {
  55. my $vendor = shift || "default";
  56. my $file;
  57. my @tries = ($vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor)));
  58. if ($vendor =~ s/\s+/-/) {
  59. push @tries, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  60. }
  61. foreach my $name (@tries) {
  62. $file = "/etc/dpkg/origins/$name" if -e "/etc/dpkg/origins/$name";
  63. }
  64. return $file;
  65. }
  66. =item $name = Dpkg::Vendor::get_current_vendor()
  67. Returns the name of the current vendor. If DEB_VENDOR is set, it uses
  68. that first, otherwise it falls back to parsing /etc/dpkg/origins/default.
  69. If that file doesn't exist, it returns undef.
  70. =cut
  71. sub get_current_vendor() {
  72. my $f;
  73. if ($ENV{'DEB_VENDOR'}) {
  74. $f = get_vendor_info($ENV{'DEB_VENDOR'});
  75. return $f->{'Vendor'} if defined $f;
  76. }
  77. $f = get_vendor_info();
  78. return $f->{'Vendor'} if defined $f;
  79. return undef;
  80. }
  81. =item $object = Dpkg::Vendor::get_vendor_object($name)
  82. Return the Dpkg::Vendor::* object of the corresponding vendor.
  83. If $name is omitted, return the object of the current vendor.
  84. If no vendor can be identified, then return the Dpkg::Vendor::Default
  85. object.
  86. =cut
  87. my %OBJECT_CACHE;
  88. sub get_vendor_object {
  89. my $vendor = shift || get_current_vendor() || "Default";
  90. return $OBJECT_CACHE{$vendor} if exists $OBJECT_CACHE{$vendor};
  91. my ($obj, @names);
  92. if ($vendor ne "Default") {
  93. push @names, $vendor, lc($vendor), ucfirst($vendor), ucfirst(lc($vendor));
  94. }
  95. foreach my $name (@names, "Default") {
  96. eval qq{
  97. require Dpkg::Vendor::$name;
  98. \$obj = Dpkg::Vendor::$name->new();
  99. };
  100. last unless $@;
  101. }
  102. $OBJECT_CACHE{$vendor} = $obj;
  103. return $obj;
  104. }
  105. =item Dpkg::Vendor::run_vendor_hook($hookid, @params)
  106. Run a hook implemented by the current vendor object.
  107. =cut
  108. sub run_vendor_hook {
  109. my $vendor_obj = get_vendor_object();
  110. $vendor_obj->run_hook(@_);
  111. }
  112. =back
  113. =cut
  114. 1;