Hello World

File queryconfig of Package build

#!/usr/bin/perl -w

################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

BEGIN {
  unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}

use strict;

use Build;
use Build::Rpm;

sub evalmacros {
  my ($config, $str) = @_;
  my @xspec;
  Build::Rpm::parse({%$config, 'save_expanded' => 1} , [ "$str" ], \@xspec);
  return @xspec && ref($xspec[0]) ? $xspec[0]->[1] : '';
}

my ($dist, $archs, $configdir, $type, $argument);

$configdir = ($::ENV{'BUILD_DIR'} || '/usr/lib/build') . '/configs';

while (@ARGV)  {
  if ($ARGV[0] eq '--dist') {
    shift @ARGV;
    $dist = shift @ARGV;
    next;
  } elsif ($ARGV[0] eq '--archpath' || $ARGV[0] eq '--arch') {
    shift @ARGV;
    $archs = shift @ARGV;
    next;
  } elsif ($ARGV[0] eq '--configdir') {
    shift @ARGV;
    $configdir = shift @ARGV;
    next;
  } elsif (defined($type)) {
    $argument = shift @ARGV; 
  } else {
    $type = shift @ARGV;
  }
}

die("Please specify what to query\n") unless defined $type;

my $cf = Build::read_config_dist($dist, $archs, $configdir);
die("Unable to read config\n") unless $cf;

if ($type eq 'buildflags') {
  die("Specify which buildflag to query\n") unless $argument;
  my $result = $cf->{"buildflags:$argument"};
  print "$result\n" if defined $result;
} elsif ($type eq 'buildflags+') {
  die("Specify which buildflag to query\n") unless $argument;
  for (@{$cf->{'buildflags'} || []}) {
    print "$1\n" if /\Q$argument\E:(.*)/;
  }
} elsif ($type eq 'hostarch') {
  my $result = $cf->{"hostarch"};
  print "$result\n" if defined $result;
} elsif ($type eq 'target' || $type eq 'type' || $type eq 'binarytype' || $type eq 'buildengine' || $type eq 'rawmacros') {
  print "$cf->{$type}\n" if $cf->{$type};
} elsif ($type eq 'repotype' || $type eq 'repourl' || $type eq 'registryurl') {
  print join(' ', @{$cf->{$type}})."\n" if $cf->{$type};
} elsif ($type eq 'optflags') {
  exit(0) unless $cf->{'optflags'};
  my $all = $cf->{'optflags'}->{'*'};
  $all = defined($all) && $all ne '' ? " $all" : '';
  $all .= " -g" if $argument && $argument eq 'debug';
  for (sort keys %{$cf->{'optflags'}}) {
    next if $_ eq '*';
    print "optflags: $_ $cf->{'optflags'}->{$_}$all\n";
  }
} elsif ($type eq 'substitute') {
  die("Specify which substitute to query\n") unless $argument;
  my @res =  @{$cf->{'substitute'}->{$argument} || []};
  print join(' ', @res)."\n" if @res;
} elsif ($type eq 'eval') {
  die("Specify what to eval\n") unless defined $argument;
  my $result = evalmacros($cf, $argument);
  print "$result\n";
} else {
  die("unsupported query type: $type\n");
}

exit(0);