Vendor.pm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright © 2008 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. =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. foreach my $name ($vendor, lc($vendor), ucfirst($vendor)) {
  58. $file = "/etc/dpkg/origins/$name" if -e "/etc/dpkg/origins/$name";
  59. }
  60. return $file;
  61. }
  62. =item $name = Dpkg::Vendor::get_current_vendor()
  63. Returns the name of the current vendor. If DEB_VENDOR is set, it uses
  64. that first, otherwise it falls back to parsing /etc/dpkg/origins/default.
  65. If that file doesn't exist, it returns undef.
  66. =cut
  67. sub get_current_vendor() {
  68. my $f;
  69. if ($ENV{'DEB_VENDOR'}) {
  70. $f = get_vendor_info($ENV{'DEB_VENDOR'});
  71. return $f->{'Vendor'} if defined $f;
  72. }
  73. $f = get_vendor_info();
  74. return $f->{'Vendor'} if defined $f;
  75. return undef;
  76. }
  77. 1;