dpkg-vendor.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, see <http://www.gnu.org/licenses/>.
  19. use strict;
  20. use warnings;
  21. use Dpkg;
  22. use Dpkg::Gettext;
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Vendor qw(get_vendor_info get_current_vendor);
  25. textdomain("dpkg-dev");
  26. sub version {
  27. printf _g("Debian %s version %s.\n"), $progname, $version;
  28. printf _g("
  29. This is free software; see the GNU General Public License version 2 or
  30. later for copying conditions. There is NO warranty.
  31. ");
  32. }
  33. sub usage {
  34. printf _g(
  35. "Usage: %s [<option> ...] [<action>]
  36. Options:
  37. --vendor <vendor> assume <vendor> is the current vendor
  38. Actions:
  39. --is <vendor> returns true if current vendor is <vendor>.
  40. --derives-from <vendor> returns true if current vendor derives from <vendor>.
  41. --query <field> print the content of the vendor-specific field.
  42. --help show this help message.
  43. --version show the version.
  44. "), $progname;
  45. }
  46. my ($vendor, $param, $action);
  47. while (@ARGV) {
  48. $_ = shift(@ARGV);
  49. if (m/^--vendor$/) {
  50. $vendor = shift(@ARGV);
  51. usageerr(_g("%s needs a parameter"), $_) unless defined $vendor;
  52. } elsif (m/^--(is|derives-from|query)$/) {
  53. usageerr(_g("two commands specified: --%s and --%s"), $1, $action)
  54. if defined($action);
  55. $action = $1;
  56. $param = shift(@ARGV);
  57. usageerr(_g("%s needs a parameter"), $_) unless defined $param;
  58. } elsif (m/^-(h|-help)$/) {
  59. usage();
  60. exit 0;
  61. } elsif (m/^--version$/) {
  62. version();
  63. exit 0;
  64. } else {
  65. usageerr(_g("unknown option \`%s'"), $_);
  66. }
  67. }
  68. usageerr(_g("need an action option")) unless defined($action);
  69. # Uses $ENV{DEB_VENDOR} if set
  70. $vendor = get_current_vendor() unless defined($vendor);
  71. my $info = get_vendor_info($vendor);
  72. unless (defined($info)) {
  73. error(_g("vendor %s doesn't exist in /etc/dpkg/origins/"),
  74. $vendor || "default");
  75. }
  76. if ($action eq 'is') {
  77. exit(0) if lc($param) eq lc($info->{'Vendor'});
  78. exit(1);
  79. } elsif ($action eq 'derives-from') {
  80. exit(0) if lc($param) eq lc($info->{'Vendor'});
  81. while (defined($info) && exists $info->{'Parent'}) {
  82. $info = get_vendor_info($info->{'Parent'});
  83. exit(0) if lc($param) eq lc($info->{'Vendor'});
  84. }
  85. exit(1);
  86. } elsif ($action eq 'query') {
  87. if (exists $info->{$param}) {
  88. print $info->{$param} . "\n";
  89. exit(0);
  90. } else {
  91. exit(1);
  92. }
  93. }