Vendor.pm 4.0 KB

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