Commit a1f0dc8a by PotatoGim

rpm_downgrade 스크립트

parent 61385285
#!/usr/bin/perl
#=============================================================================
# FILE: rpm_downgrade.pl
# USAGE: ./rpm_downgrade.pl
# OPTIONS: ---
# BRIEF:
# AUTHOR: Ji-Hyeon Gim (http://www.potatogim.net/)
# EMAIL: potatogim@potatogim.net
# DATE: 11/20/2014 04:45:49 PM
# VERSION: 1.0
# LICENSE: Artistic License
# NOTE: ---
#=============================================================================
use LWP::UserAgent;
use HTML::TreeBuilder;
use Encode qw/decode/;
use IO::File;
use Data::Dumper;
use utf8;
my $version = shift(@ARGV);
my $arch = shift(@ARGV);
my $listfile = shift(@ARGV);
if (!defined($version))
{
warn "[ERR] Required parameter is missing\n";
exit -1;
}
if (!defined($arch))
{
warn "[ERR] Required parameter is missing\n";
exit -1;
}
if (!defined($listfile))
{
warn "[ERR] Required parameter is missing\n";
exit -1;
}
my $mirrorurl = "http://mirror.centos.org/centos/$version/os/$arch/Packages";
sub get_pkglist
{
my @pkglist = ();
my $ua = LWP::UserAgent->new;
my $contents = $ua->get($mirrorurl);
my $html = decode("utf8", $contents->content);
my $tree = HTML::TreeBuilder->new;
my $result = $tree->parse($html);
my @htmls = $result->look_down (
sub
{
$_[0]->attr("href") =~ /\.rpm$/
}
);
foreach my $html (@htmls)
{
if ($html->attr_get_i("href"))
{
push (@pkglist, $html->as_text);
}
}
print @pkglist;
sleep 10;
return @pkglist;
}
sub get_downlist
{
my @downlist = ();
my $fh = IO::File->new("< $listfile");
if (!defined($fh))
{
warn "[ERR] Failed to open the file: $listfile ($!)";
}
@lines = $fh->getlines();
$fh->close();
foreach my $line (@lines)
{
chomp ($line);
my @tmp = split(/\s*:\s*/, $line);
my $pkg = (@tmp == 2) ? $tmp[1] : $tmp[2];
$pkg =~ s/\-[0-9].+$//g;
push (@downlist, $pkg);
}
return @downlist;
}
sub get_pkg
{
my $pkg = shift;
# 파일 내려받기
mkdir("pkgs");
system("wget -c $mirrorurl/$pkg -Opkgs/$pkg");
}
# downlist의 목록을 pkglist와 비교하여 패키지명이 일치하면 해당 패키지
# 내려받아서 설치
#
# 우선 배열로 매치하는게 되면 해시로 바꾸자... 효율이 너무 떨어진다.
my @pkglist = get_pkglist();
foreach my $downpkg (get_downlist())
{
for (my $i=0; $i<@pkglist; ++$i)
{
my $tmp = $pkglist[$i];
next if ($tmp !~ m/($arch|noarch)\.rpm$/);
$tmp =~ s/\-[0-9].+$//g;
if ($tmp eq $downpkg)
{
get_pkg($pkglist[$i]);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment