dpkg-vendor.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-vendor
  4. #
  5. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. use strict;
  21. use warnings;
  22. use Dpkg;
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Vendor qw(get_vendor_info get_current_vendor);
  26. textdomain("dpkg-dev");
  27. sub version {
  28. printf _g("Debian %s version %s.\n"), $progname, $version;
  29. printf _g("
  30. Copyright (C) 2009 Raphael Hertzog <hertzog\@debian.org>.");
  31. printf _g("
  32. This is free software; see the GNU General Public Licence version 2 or
  33. later for copying conditions. There is NO warranty.
  34. ");
  35. }
  36. sub usage {
  37. printf _g(
  38. "Usage: %s [<option> ...] [<action>]
  39. Options:
  40. --vendor <vendor> assume <vendor> is the current vendor
  41. Actions:
  42. --is <vendor> returns true if current vendor is <vendor>.
  43. --derives-from <vendor> returns true if current vendor derives from <vendor>.
  44. --query <field> print the content of the vendor-specific field.
  45. --help show this help message.
  46. --version show the version.
  47. "), $progname;
  48. }
  49. my ($vendor, $param, $action);
  50. while (@ARGV) {
  51. $_ = shift(@ARGV);
  52. if (m/^--vendor$/) {
  53. $vendor = shift(@ARGV);
  54. } elsif (m/^--(is|derives-from|query)$/) {
  55. usageerr(_g("two commands specified: --%s and --%s"), $1, $action)
  56. if defined($action);
  57. $action = $1;
  58. $param = shift(@ARGV);
  59. } elsif (m/^-(h|-help)$/) {
  60. usage();
  61. exit 0;
  62. } elsif (m/^--version$/) {
  63. version();
  64. exit 0;
  65. } else {
  66. usageerr(_g("unknown option \`%s'"), $_);
  67. }
  68. }
  69. usageerr(_g("need an action option")) unless defined($action);
  70. # Uses $ENV{DEB_VENDOR} if set
  71. $vendor = get_current_vendor() unless defined($vendor);
  72. my $info = get_vendor_info($vendor);
  73. unless (defined($info)) {
  74. error(_g("vendor %s doesn't exist in /etc/dpkg/origins/"),
  75. $vendor || "default");
  76. }
  77. if ($action eq 'is') {
  78. exit(0) if lc($param) eq lc($info->{'Vendor'});
  79. exit(1);
  80. } elsif ($action eq 'derives-from') {
  81. exit(0) if lc($param) eq lc($info->{'Vendor'});
  82. while (defined($info) && exists $info->{'Parent'}) {
  83. $info = get_vendor_info($info->{'Parent'});
  84. exit(0) if lc($param) eq lc($info->{'Vendor'});
  85. }
  86. exit(1);
  87. } elsif ($action eq 'query') {
  88. if (exists $info->{$param}) {
  89. print $info->{$param} . "\n";
  90. exit(0);
  91. } else {
  92. exit(1);
  93. }
  94. }