Skip to content

Commit

Permalink
Use Build::Options to parse the command options
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Feb 7, 2022
1 parent 904d2da commit 8950426
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 152 deletions.
16 changes: 10 additions & 6 deletions createarchdeps
Expand Up @@ -29,23 +29,27 @@ BEGIN {
use strict;
use Build ':arch';
use Build::Archrepo;
use Build::Options;
use Digest::MD5 ();
use File::Path;
use Getopt::Long;

Getopt::Long::Configure("no_ignore_case");
use File::Path ();

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

my $options = {
'cachedir' => ':',
};

sub getreponame {
my ($url) = @_;
return $1 if "/$url/" =~ /.*\/([^\/]+)\/os\//;
return undef;
}

GetOptions("cachedir=s" => \$cachedir) or exit(1);

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

for my $url (@args) {
die("$url: not an remote Archlinux repo") unless $url =~ /^(:?ftps?|https?):\/\/([^\/]*)\/?/;
my $reponame = getreponame($url);
die("could not determine reponame from url $url\n") unless defined $reponame;
Expand Down
25 changes: 15 additions & 10 deletions createdebdeps
Expand Up @@ -25,36 +25,41 @@ BEGIN {
}

use strict;
use Digest::MD5;
use File::Path;
use Getopt::Long;

use Digest::MD5 ();
use File::Path ();

use Build ':deb';
use Build::Options;
use Build::Deb;
use Build::Debrepo;

Getopt::Long::Configure("no_ignore_case");
my $cachedir = "/var/cache/build";
my $options = {
'cachedir' => ':',
'archpath' => ':',
};

#
# supported urls
#
# distribution: <baseurl>/<dist>/[components]
# flat repo: <baseurl>/.

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

GetOptions('cachedir=s' => \$cachedir, 'archpath=s' => \$archpath) or exit(1);

my ($opts, @args) = Build::Options::parse_options($options, @ARGV);
$cachedir = $opts->{'cachedir'} if $opts->{'cachedir'};
my $archpath = $opts->{'archpath'};
if (!$archpath) {
$archpath = `uname -p` || 'unknown';
chomp $archpath;
}

my $basearch = $archpath;
$basearch =~ s/:.*//;
$basearch = Build::Deb::basearch($basearch);
my $pkgnum = 0;

for my $url (@ARGV) {
for my $url (@args) {
die("$url: not an remote debian repo\n") unless $url =~ /^(:?ftps?|https?):\/\/([^\/]*)\/?/;
my $repoid = Digest::MD5::md5_hex($url);
my $dir = "$cachedir/$repoid";
Expand Down
18 changes: 9 additions & 9 deletions createdirdeps
Expand Up @@ -24,16 +24,14 @@ BEGIN {
unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}

use Build;
use Getopt::Long;

use strict;

Getopt::Long::Configure("no_ignore_case");

my $oldfile;
use Build;
use Build::Options;

GetOptions ("oldfile=s" => \$oldfile) or exit(1);
my $options = {
'oldfile' => ':',
};

sub queryfromfilename {
my ($fn) = @_;
Expand All @@ -47,8 +45,10 @@ sub queryfromfilename {

######################################################################

my ($opts, @args) = Build::Options::parse_options($options, @ARGV);

my %old;
if (defined($oldfile) && open(F, '<', $oldfile)) {
if (defined($opts->{'oldfile'}) && open(F, '<', $opts->{'oldfile'})) {
while (<F>) {
chomp;
$old{$1} = $_ if /^([PRrCOI]:[^ ]+): /;
Expand All @@ -58,7 +58,7 @@ if (defined($oldfile) && open(F, '<', $oldfile)) {

my %seen;

for my $dir (@ARGV) {
for my $dir (@args) {
my $cmd = "find $dir -follow -type f \\( -name \"*.rpm\" -o -name \"*.deb\" -o -name \"*.pkg.tar.gz\" -o -name \"*.pkg.tar.xz\" \\) -a ! -name \"*src.rpm\" -printf '\%T@/\%s/\%i \%p\\n'";
open(F, '-|', $cmd) or next;
while (<F>) {
Expand Down
19 changes: 12 additions & 7 deletions createmdkdeps
Expand Up @@ -27,19 +27,24 @@ BEGIN {
}

use strict;
use Build ':mdk';
use Build::Mdkrepo;

use Digest::MD5 ();
use File::Path;
use Getopt::Long;
use File::Path ();

Getopt::Long::Configure("no_ignore_case");
use Build ':mdk';
use Build::Options;
use Build::Mdkrepo;

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

GetOptions("cachedir=s" => \$cachedir) or exit(1);
my $options = {
'cachedir' => ':',
};

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

for my $url (@ARGV) {
for my $url (@args) {
die("$url: not an remote mandriva/mageia repo") unless $url =~ /^(:?ftps?|https?):\/\/([^\/]*)\/?/;
my $repoid = Digest::MD5::md5_hex($url);
my $dir = "$cachedir/$repoid";
Expand Down
106 changes: 54 additions & 52 deletions createrepomddeps
Expand Up @@ -25,68 +25,61 @@ BEGIN {
}

use strict;

use Digest::MD5 ();
use File::Path ();
use Data::Dumper;
use Getopt::Long;

use Build ':rpm';
use Build::Rpm;
use Build::Rpmmd;
use Digest::MD5 ();
use File::Path qw(mkpath rmtree);
use File::Basename;
use Build::Options;

Getopt::Long::Configure("no_ignore_case");

my $opt_dump;
my $opt_old;
my $opt_nosrc;
my $opt_bc;
my $opt_zypp;
my $cachedir = "/var/cache/build";

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


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

my $arch = $pkg->{'arch'};
$arch = 'src' if $pkg->{'arch'} eq 'nosrc';
return if $arch eq 'src' && $opt_nosrc;
my $evr = $pkg->{'version'}.'-'.$pkg->{'release'};
$evr = "$pkg->{'epoch'}:$evr" if $pkg->{'epoch'};
my $loc = $baseurl . $pkg->{'location'};
if ($old_seen->{$pkg->{'name'}}->{$arch}) {
my $vv = Build::Rpm::verscmp($old_seen->{$pkg->{'name'}}->{$arch}->{'evr'}, $evr, 0);
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->{$pkg->{'name'}}->{$arch}->{'loc'}."\n";
print $old_seen->{$k}->[1]."\n";
}
$old_seen->{$pkg->{'name'}}->{$arch}->{'evr'} = $evr;
$old_seen->{$pkg->{'name'}}->{$arch}->{'loc'} = $loc;
$old_seen->{$k} = [ $evr, $loc ];
}

GetOptions (
"nosrc" => \$opt_nosrc,
"dump" => \$opt_dump,
"old" => \$opt_old,
"zypp=s" => \$opt_zypp,
"cachedir=s" => \$cachedir,
) or exit(1);
my ($opts, @args) = Build::Options::parse_options($options, @ARGV);
$cachedir = $opts->{'cachedir'} if $opts->{'cachedir'};

$opt_bc = 1 unless $opt_dump || $opt_old;
my $old_seen = {}; # for --old
my @packages; # for --dump

my $old_seen = {}; # for opt_old
my @packages; # for opt_dump

for my $url (@ARGV) {
for my $url (@args) {
my $dir;
my $baseurl = $url;
if ($opt_zypp) {
$dir = $opt_zypp;
if ($opts->{'zypp'}) {
$dir = $opts->{'zypp'};
} elsif ($url =~ /^(?:ftps?|https?):\/\/([^\/]*)\/?/) {
my $repoid = Digest::MD5::md5_hex($url);
$dir = "$cachedir/$repoid/";
$baseurl .= '/' unless $baseurl =~ /\/$/;
mkpath("${dir}repodata");
File::Path::mkpath("${dir}repodata");
system($INC[0].'/download', "${dir}repodata", "${baseurl}repodata/repomd.xml");
} else {
$dir = $url;
Expand All @@ -95,7 +88,7 @@ for my $url (@ARGV) {
$baseurl .= '/' unless $baseurl =~ /\/$/;

if (! -s "${dir}repodata/repomd.xml") {
die("zypp repo $url is not up to date, please refresh first\n") if $opt_zypp;
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");
}

Expand All @@ -105,50 +98,59 @@ for my $url (@ARGV) {
# print Dumper(\@primaryfiles);

for my $f (@primaryfiles) {
my $u = "$dir$f->{'location'}";
my $u = $f->{'location'};
if ($] > 5.007) {
require Encode;
utf8::downgrade($u);
}
$u =~ s/.*\///; # strip "repodata" part
my $cached;
if (-e $u) {
if (-e "${dir}repodata/$u") {
$cached = 1;
$cached = 0 if exists($f->{'size'}) && $f->{'size'} != (-s _);
$cached = 0 if !$opt_zypp && !exists($f->{'size'}) && $u !~ /[0-9a-f]{32}-primary/;
$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 $opt_zypp;
die("zypp repo $url is not up to date, please refresh first\n") if $opts->{'zypp'};
if ($url =~ /^(?:ftps?|https?):\/\/([^\/]*)\/?/) {
if (system("$INC[0]/download", "${dir}repodata/", "${baseurl}repodata/" . basename($u))) {
if (system("$INC[0]/download", "${dir}repodata/", "${baseurl}repodata/$u")) {
die("download failed\n");
}
} else {
die("inconsistent repodata in $url\n");
}
}
my $fh;
open($fh, '<', $u) or die "Error opening $u: $!\n";
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";
}
Build::Rpmmd::parse($fh, sub {
if ($opt_dump) {
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] if $opt_dump;
}
if ($opt_bc) {
binmode STDOUT, ":utf8";
Build::writedeps(\*STDOUT, $_[0], $baseurl);
} elsif ($opt_old) {
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);
}
}, 'addselfprovides' => 1);
};
} 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 ($opt_dump) {
if ($opts->{'dump'}) {
print Data::Dumper->Dump([\@packages], ['packages']); # caution: excessive memory consumption!
}

29 changes: 17 additions & 12 deletions createyastdeps
Expand Up @@ -24,34 +24,39 @@ BEGIN {
unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}

use Build ':rpm';
use Build::Susetags;
use Getopt::Long;
use File::Path qw(mkpath);

use strict;

Getopt::Long::Configure("no_ignore_case");
use Digest::MD5();
use File::Path ();

use Build ':rpm';
use Build::Susetags;
use Build::Options;

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

GetOptions ("zypp=s" => \$opt_zypp, "cachedir=s" => \$cachedir) or exit(1);
my $options = {
'cachedir' => ':',
'zypp' => ':',
};

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

for my $url (@ARGV) {
for my $url (@args) {
# XXX: use descrdir/datadir from content file
my $descrdir = 'suse/setup/descr';
my $datadir = 'suse';

my $dir;
my $baseurl = $url;
if ($opt_zypp) {
$dir = $opt_zypp;
if ($opts->{'zypp'}) {
$dir = $opts->{'zypp'};
} elsif ($url =~ /^(?:ftps?|https?):\/\/([^\/]*)\/?/) {
my $repoid = Digest::MD5::md5_hex($url);
$dir = "$cachedir/$repoid/";
$baseurl .= '/' unless $baseurl =~ /\/$/;
mkpath($dir);
File::Path::mkpath($dir);
system("$INC[0]/download", "$dir/", "${baseurl}$descrdir/packages.gz");
$descrdir = '.';
} else {
Expand Down

0 comments on commit 8950426

Please sign in to comment.