Hello World

File createrepomddeps 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 Digest::MD5 ();
use File::Path ();
use Data::Dumper;

use Build ':rpm';
use Build::Rpm;
use Build::Rpmmd;
use Build::Options;
use Build::Download;

my $cachedir = "/var/cache/build";

my $options = {
  'nosrc' => '',
  'dump' => '',
  'old' => '',
  'zypp' => ':',
  'cachedir' => ':',
};


sub printold {
  my ($pkg, $baseurl, $old_seen) = @_;

  my $evr = $pkg->{'version'}.'-'.$pkg->{'release'};
  $evr = "$pkg->{'epoch'}:$evr" if $pkg->{'epoch'};
  my $loc = $baseurl . $pkg->{'location'};
  my $k = "$pkg->{'name'}.$pkg->{'arch'}";
  if ($old_seen->{$k}) {
    my $vv = Build::Rpm::verscmp($old_seen->{$k}->[0], $evr, 0);
    if ($vv >= 0) {
      print "$loc\n";
      return;
    }
    print $old_seen->{$k}->[1]."\n";
  }
  $old_seen->{$k} = [ $evr, $loc ];
}

my ($opts, @args) = Build::Options::parse_options($options, @ARGV);
$cachedir = $opts->{'cachedir'} if $opts->{'cachedir'};

my $old_seen = {};	# for --old
my @packages;		# for --dump

my $ua = Build::Download::create_ua();

for my $url (@args) {
  my $dir;
  my $baseurl = $url;
  if ($opts->{'zypp'}) {
    $dir = $opts->{'zypp'};
  } elsif ($url =~ /^(?:ftps?|https?):\/\/([^\/]*)\/?/) {
    my $repoid = Digest::MD5::md5_hex($url);
    $dir = "$cachedir/$repoid/";
    $baseurl .= '/' unless $baseurl =~ /\/$/;
    File::Path::mkpath("${dir}repodata");
    Build::Download::download("${baseurl}repodata/repomd.xml", "${dir}repodata/.repomd.xml.$$", "${dir}repodata/repomd.xml", 'ua' => $ua, 'retry' => 3);
  } else {
    $dir = $url;
  }
  $dir .= '/' unless $dir =~ /\/$/;
  $baseurl .= '/' unless $baseurl =~ /\/$/;

  if (! -s "${dir}repodata/repomd.xml") {
    die("zypp repo $url is not up to date, please refresh first\n") if $opts->{'zypp'};
    die("repo $url does not contain a repomd.xml file\n");
  }

  my @primaryfiles;
  Build::Rpmmd::parse_repomd("${dir}repodata/repomd.xml", \@primaryfiles);
  @primaryfiles = grep {$_->{'type'} eq 'primary' && defined($_->{'location'})} @primaryfiles;

  for my $f (@primaryfiles) {
    my $u = $f->{'location'};
    if ($] > 5.007) {
      require Encode;
      utf8::downgrade($u);
    }
    $u =~ s/.*\///;	# strip "repodata" part
    my $cached;
    if (-e "${dir}repodata/$u") {
      $cached = 1;
      $cached = 0 if exists($f->{'size'}) && $f->{'size'} != (-s _);
      $cached = 0 if !$opts->{'zypp'} && !exists($f->{'size'}) && $u !~ /[0-9a-f]{32}-primary/;
    }
    if (!$cached) {
      die("zypp repo $url is not up to date, please refresh first\n") if $opts->{'zypp'};
      die("inconsistent repodata in $url\n") unless $url =~ /^(?:ftps?|https?):\/\/([^\/]*)\/?/;
      Build::Download::download("${baseurl}repodata/$u", "${dir}repodata/.$u.$$", "${dir}repodata/$u", 'ua' => $ua, 'retry' => 3, 'digest' => $f->{'checksum'});
    }
    my $fh;
    open($fh, '<', "${dir}repodata/$u") or die "Error opening ${dir}repodata/$u: $!\n";
    if ($u =~ /\.gz$/) {
      use IO::Uncompress::Gunzip qw($GunzipError);
      $fh = new IO::Uncompress::Gunzip $fh or die "Error opening $u: $GunzipError\n";
    }
    my $parsefn;
    if ($opts->{'dump'}) {
      $parsefn = sub {
	return if $opts->{'nosrc'} && ($_[0]->{'arch'} eq 'src' || $_[0]->{'arch'} eq 'nosrc');
        $_[0]->{'baseurl'} = $baseurl;
        push @packages, $_[0];
      };
    } elsif ($opts->{'old'}) {
      $parsefn = sub {
	return if $opts->{'nosrc'} && ($_[0]->{'arch'} eq 'src' || $_[0]->{'arch'} eq 'nosrc');
	printold($_[0], $baseurl, $old_seen);
      };
    } else {
      $parsefn = sub {
	return if $opts->{'nosrc'} && ($_[0]->{'arch'} eq 'src' || $_[0]->{'arch'} eq 'nosrc');
        binmode STDOUT, ":utf8";
        Build::writedeps(\*STDOUT, $_[0], $baseurl)
      };
    }
    Build::Rpmmd::parse($fh, $parsefn, 'addselfprovides' => 1);
    close($fh);
  }
}

if ($opts->{'dump'}) {
  print Data::Dumper->Dump([\@packages], ['packages']); # caution: excessive memory consumption!
}