From b53e28d5eeed784952d2033a3772057b536812bc Mon Sep 17 00:00:00 2001 From: Matteo Pasotti Date: Sun, 20 Mar 2016 23:28:08 +0100 Subject: urpm-downloader (from urpmex) has been ported to manatools --- lib/ManaTools/Module/PkgDownloader.pm | 207 ++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 lib/ManaTools/Module/PkgDownloader.pm (limited to 'lib') diff --git a/lib/ManaTools/Module/PkgDownloader.pm b/lib/ManaTools/Module/PkgDownloader.pm new file mode 100644 index 00000000..e78c9eb4 --- /dev/null +++ b/lib/ManaTools/Module/PkgDownloader.pm @@ -0,0 +1,207 @@ +#!/usr/bin/perl -w +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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. If not, see . +# +# Copyright: 2012 - 2016 by Matteo Pasotti + +package ManaTools::Module::PkgDownloader; + +use strict; +use warnings; +# use diagnostics; +use Term::ANSIColor qw(:constants); +# use Getopt::Long qw(:config permute); +# use urpmex::Urpmex; +use Moose; +use Moose::Autobox; +use ManaTools::Shared::urpmi_backend::DB; + + +with 'MooseX::Getopt'; + +extends qw( ManaTools::Module ); + +my $PKG_QUERYMAKER = "urpmq"; +my $QUERY_LISTMEDIA_PARM = "--list-media"; +my $QUERY_LISTURL_PARM = "--list-url"; +my $QUERY_LOOKFORSRPM_PARM = "--sourcerpm"; +my $QUERY_PKG_FULL = "-f"; +my $DLDER = "--wget"; + + +has 'use_wget' => ( + is => 'rw', + isa => 'Bool', + default => sub { return 0; }, +); + +has 'use_axel' => ( + is => 'rw', + isa => 'Bool', + default => sub { return 0; }, +); + +has 'srpm' => ( + is => 'rw', + isa => 'Bool', + default => sub { return 0; }, +); + +has 'use_major' => ( + is => 'rw', + isa => 'Bool', + default => sub { return 0; }, +); + +has 'packagelist' => ( + is => 'rw', + isa => 'Str', + required => 1, +); + +has 'packages' => ( + is => 'rw', + isa => 'ArrayRef', + default => sub { [] }, +); + +sub process_args { + my ($self, $pkglist) = @_; + for(split(/\s/,$pkglist)){ + push @{$self->packages()}, $_; + } +} + +sub download { + my $self = shift(); + my $url = shift(); + my $rpm = shift(); + if($self->use_wget()){ + `wget "$url/$rpm" -O $rpm`; + }elsif($self->use_axel()){ + `axel -a "$url/$rpm" -o $rpm`; + }else{ + `curl -s "$url/$rpm" -o $rpm`; + } +} + + +# IMPORTANT! +# THOSE TWO ROUTINES MUST BE PORTED TO THE URPMI_BACKEND FROM MANATOOLS: +# * retrieve_brpm_pkgname +# * retrieve_srpm_pkgname + +# ---------------------------------------------------------------------- +# retrieve the binary rpm's pkg name - array +# ---------------------------------------------------------------------- +sub retrieve_brpm_pkgname { + my $pkg = shift(); + my @lista_brpms = `$PKG_QUERYMAKER -a $QUERY_PKG_FULL $pkg | grep "^$pkg" | sort -u`; + return @lista_brpms; +} + +# ---------------------------------------------------------------------- +# retrieve the srpm's pkg name - array +# ---------------------------------------------------------------------- +sub retrieve_srpm_pkgname { + my $pkg = shift(); + my @lista_srpms = `$PKG_QUERYMAKER $QUERY_LOOKFORSRPM_PARM $pkg | sort -u | grep "$pkg:" | awk -F':' '{print \$2}'`; + return @lista_srpms; +} + +sub start { + my $self = shift; + my $pkg = ""; + my $rpmbackend = ManaTools::Shared::urpmi_backend::DB->new(); + my $urpm = $rpmbackend->fast_open_urpmi_db(); + my @media_urls=map { if(!$self->srpm()) { + $_->{url} + }else{ + my @a = split(/\//,$_->{url}); + my $newurl = ""; + my $i = 0; + for($i=0;$iget_active_media($urpm,$self->srpm()); + + $self->process_args($self->packagelist()); + + + if(scalar(@media_urls) lt 1) + { + print BOLD, RED, "==", RESET, " no active media found\n"; + return 3; + } + + if(scalar(@{$self->packages()}) gt 0){ + for $pkg(@{$self->packages()}){ + my @lista_srpms; + if($self->srpm()){ + @lista_srpms = retrieve_srpm_pkgname($pkg); + }else{ + @lista_srpms = retrieve_brpm_pkgname($pkg); + } + #print "@lista_srpms\n"; + if($self->use_major()){ + #print "Using only major version\n"; + for(my $i=0;$isrpm()); + print BOLD, WHITE, "== Processing $srpm\n", RESET; + for my $url(@media_urls){ + chomp $url; + my @protocol = split(':',$url); + if($protocol[0] eq "http"){ + print BOLD, WHITE, "== ", RESET, "protocol in use: ", BOLD, WHITE, $protocol[0], RESET, "\n"; + print BOLD, WHITE, "== ", RESET, "trying with $url/$srpm\n"; + my $check = `curl -s --head "$url/$srpm" | head -n 1 | grep "200 OK" > /dev/null ; echo \$?`; + chomp $check; + if($check eq "0"){ + $self->download($url,$srpm); + last; + } + }elsif($protocol[0] eq "ftp"){ + print BOLD, WHITE, "== ", "protocol in use: ", BOLD, WHITE, $protocol[0], RESET, "\n"; + my $check = `curl -s --head "$url/$srpm"`; + $check =~s/\n/ /g; + $check =~s/^\s+//g; + $check =~s/\s+$//g; + if($check ne ""){ + $self->download($url,$srpm); + last; + } + }elsif($protocol[0] eq "rsync"){ + print BOLD, WHITE, "== ", "protocol in use: ", BOLD, WHITE, $protocol[0], RESET, "\n"; + } + } + print BOLD, WHITE, "== $srpm", RESET, " downloaded successfully\n"; + } + } + return 0; + }else{ + print BOLD, RED, "== no packages passed as argument\n", RESET; + return 2; + } +} + +1; -- cgit v1.2.1