#!/usr/bin/perl -w # # rpmbuildupdate by Julien Danjou # # Copyright (c) 2003-2004 by MandrakeSoft # # Permission to use, copy, modify, and distribute this software and its # documentation under the terms of the GNU General Public License is hereby # granted. No representations are made about the suitability of this software # for any purpose. It is provided "as is" without express or implied warranty. # See the GNU General Public License for more details. # use strict; use AppConfig; my %config; sub build_from_src { $_ = shift; my $nv = shift; my $b = qx[basename $_]; if($b =~ m|^(.*)-([^-]+)-([^-]+)\.[^\.]+\.rpm|) { &build($1, $nv, $_); } } sub fetch { my ($url) = @_; my $basename = `basename $url`; chomp($basename); my ($temp, $turl); my $wget = "wget -N -q"; system("rm -f $basename"); print "Trying to fetch $url...\n"; system("$wget $url"); if (! -f $basename) { ($turl = $url) =~ s/bz2/gz/; $temp = `basename $turl`; chomp($temp); print "Trying to fetch $turl...\n"; system("$wget $turl; [ -f $temp ] && bzme $temp"); } if (! -f $basename) { ($turl = $url) =~ s/\.tar\.bz2/.tgz/; $temp = `basename $turl`; chomp($temp); print "Trying to fetch $turl...\n"; system("$wget $turl; [ -f $temp ] && bzme $temp"); } if (! -f $basename) { ($turl = $url) =~ s/\.tar\.bz2/.zip/; $temp = `basename $turl`; chomp($temp); print "Trying to fetch $turl...\n"; system("$wget $turl; [ -f $temp ] && bzme $temp"); } return (-f $basename); } sub build { my $pkg = shift; my $newversion = shift; my $pkgrpm = shift; my $rpm; my $found = 0; my ($version, $release); my ($spec, @url, @rpms, %specvars); my $top = $config{top} || `rpm --eval '%_topdir'`; chomp($top); chdir("$top/SOURCES") or die "Unable to chdir to $top/SOURCES"; $rpm = "rpm --define \"_topdir $top\""; print "===> Building $pkg $newversion\n"; my $pkge = $pkg; $pkge =~ s/\+/\\\+/; # Search for file if we do not use --src if(!$config{src}) { opendir(MP, $config{srpms}) or die "$config{srpms} is not a directory"; @rpms = readdir(MP); foreach (@rpms) { if(m|^($pkge)-([^-]+)-([^-]+)\.[^\.]+\.rpm|) { $pkgrpm = $_; $version = $2; $release = $3; $found = 1; last;} } closedir(MP); if($found == 0) { print "Package $pkg has no source, skipping.\n\n"; return; } } else { my $pkgrpm_basename = qx[basename $pkgrpm]; if($pkgrpm_basename =~ m|^($pkge)-([^-]+)-([^-]+)\.[^\.]+\.rpm|) { $version = $2; $release = $3; $found = 1;} } if($config{deps}) { print("sudo /usr/sbin/urpmi --auto --force ".$config{srpms}."/".$pkgrpm); wait; } if($config{src}) { system("$rpm -ivh $pkgrpm"); wait; } elsif(!$config{nosource}) { system("/usr/sbin/urpmi --install-src --force ".$config{srpms}."/".$pkgrpm); wait; } $found = 0; if(!open(SPECFILE, "../SPECS/".$pkg.".spec")) { print STDERR "Unable to open spec file !\n"; return; } while() { # Doing a s/// version s/\%define\s+version\s+$version/\%define version $newversion/g; s/Version:\s+$version/Version: $newversion/g; $spec .= $_; $spec =~ s/\%define(\s+)release(\s+)(.*)/\%define release $config{release}/; $spec =~ s/Release:(\s+)(.*mdk)/Release:\1$config{release}/; push(@url, $2) if(/(Source[0-9]*):\s+((?:ftp|http):\S+)/i); # For %vars ! $specvars{$1} = $2 if(/\%define\s+(.+?)\s+(.+)/g); $specvars{version} = $1 if(!$specvars{version} && /Version:\s+(.+)/gi); $specvars{name} = $1 if(!$specvars{name} && /Name:\s+(.+)/gi); if(/\%changelog/) { my $email; my @l = getpwuid($<); $email = $ENV{EMAIL} || $l[6]." <$l[0]\@mandrakesoft.com>"; $spec .= "* ".qx[LC_TIME=C date '+%a %b %d %Y'|tr -d '\n']." ".$email. " ".$newversion."-".$config{release}."\n"; $spec .= "- New release $newversion\n\n"; } } close(SPECFILE); if(!$url[0]) { print "URL was not found ! Skipping...\n"; return; } $found = 0; foreach (@url) { # Replace variable from spec (%blabla) while(/\%/) { s/\%\{(.*?)\}/$specvars{$1}/g; s/\%(\w+)/$specvars{$1}/g; s/\%\{name\}/$pkg/gi; s/\%\{version\}/$newversion/gi; } my $basename = `basename $_`; chomp($basename); # download from Fedora if(/ftp\.redhat\.com/) { opendir(MP, $config{fedora}) or die "$config{fedora} is not a directory"; my @rpmsrh = readdir(MP); my $pkgrpmrh; foreach (@rpmsrh) { if(m|^($pkge)-([^-]+)-([^-]+)\.[^\.]+\.rpm|) { $pkgrpmrh = $_; last;} } closedir(MP); print "Trying from fedora($basename): $config{fedora}/$pkgrpmrh\n"; system("cd ${top}/SOURCES; rpm2cpio $config{fedora}/$pkgrpmrh | cpio -id $basename"); wait; if (! -f "${top}/SOURCES/$basename") { (my $bname = $basename) =~ s/bz2/gz/; print "Trying from fedora($bname): $config{fedora}/$pkgrpmrh\n"; system("cd ${top}/SOURCES; rpm2cpio $config{fedora}/$pkgrpmrh | cpio -id $bname; bzme $bname"); wait; } } # download from sourceforge mirrors if (m@http://prdownloads.sourceforge.net@) { foreach my $site ("http://heanet.dl.sourceforge.net/sourceforge/", "http://aleron.dl.sourceforge.net/sourceforge/", "http://keihanna.dl.sourceforge.net/sourceforge/", "http://belnet.dl.sourceforge.net/sourceforge/", "http://unc.dl.sourceforge.net/sourceforge/", "http://twtelecom.dl.sourceforge.net/sourceforge/", ) { (my $dest = $_) =~ s@http://prdownloads.sourceforge.net/@$site@; last if (fetch($dest)); } } # download specified url if (! -f "${top}/SOURCES/$basename") { fetch($_); } $found++ if(-e $basename); } open(SPECFILE, ">../SPECS/".$pkg.".spec") or die "Unable to open $pkg.spec"; print SPECFILE $spec; close(SPECFILE); if(!$found) { print "Unable to download file: URL is not valid ! :-(\n\n"; return; } if(system("$rpm -ba ../SPECS/".$pkg.".spec")) { wait; print "Binary build fails: building source only\n"; system("$rpm -bs ../SPECS/".$pkg.".spec"); } wait; } sub wget_check { my $wgetv = `wget --version`; $wgetv =~ /Wget/ or die "You need `wget' binary for FTP/HTTP download\n"; } sub parse_argv { my $conf = AppConfig->new({ CASE => 1 }); $conf->define("rpmmon", { ARGS => "=s", ALIAS => "r", ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->define("release", { ARGS => "=s", ALIAS => "l", DEFAULT => "1mdk", ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->define("srpms", { ARGS => "=s", ALIAS => "m", DEFAULT => "/mnt/BIG/distrib/cooker/SRPMS/", ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->define("src", { ARGS => "=s", ALIAS => "s", ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->define("fedora", { ARGS => "=s", ALIAS => "h", DEFAULT => "/mnt/BIG/distrib/fedora/development/SRPMS/", ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->define("deps", { ALIAS => "d", DEFAULT => 0, ARGCOUNT => AppConfig::ARGCOUNT_NONE } ); $conf->define("nosource", { ALIAS => "n", DEFAULT => 0, ARGCOUNT => AppConfig::ARGCOUNT_NONE } ); $conf->define("top", { ARGS => "=t", ALIAS => "h", DEFAULT => 0, ARGCOUNT => AppConfig::ARGCOUNT_ONE } ); $conf->args(); $config{rpmmon} = $conf->get("rpmmon"); $config{deps} = $conf->get("deps"); $config{srpms} = $conf->get("srpms"); $config{src} = $conf->get("src"); $config{release} = $conf->get("release"); $config{nosource} = $conf->get("nosource"); $config{fedora} = $conf->get("fedora"); $config{top} = $conf->get("top"); } sub usage { print "rpmbuildupdate v0.4 helps you build up to date RPMs.\n\n"; print "By Julien Danjou \n"; print "Copyright (c) 2003-2004 by MandrakeSoft.\n"; print "This is free software under the GPL License.\n"; print "Usage: rpmbuildupdate [options] [pkg] [newversion]\n\n"; print " --rpmmon : Parse output of rpmmon from file\n"; print " --srpms : Specify SRPMS path\n"; print " --src : Build new version from this source RPM\n"; print " --release : Release version of package (default: 1mdk)\n"; print " --deps: Install builds dependencies\n"; print " --nosource: Do not install source from (urpmi x.src.rpm)\n"; print " --top : specify rpm top dir (default: `rpm --eval %_topdir`)\n"; exit 0; } sub parse_rpmmon { my $f; ($f = shift()); -f $f or die "Error: $f is not a file.\n"; open(RPMMON, $f); while() { &build($1, $3) if(/^\s+(\S+)\s+(\S+)\s+(\S+)$/ && ! /Package/); &build($2, $4) if(/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/ && ! /Package/); } close(RPMMON); } sub check_dir { $_ = shift; -d $_ or die $_." is not a directory.\n"; } sub main { &parse_argv; &wget_check; if($config{rpmmon}) { print($config{srpms}); &parse_rpmmon($config{rpmmon}); } elsif($config{src} && $ARGV[0]) { &build_from_src($config{src}, $ARGV[0]); } elsif($ARGV[0] && $ARGV[1]) { &check_dir($config{srpms}); &build($ARGV[0], $ARGV[1]); } else { &usage; } } &main;