1

cr.pl: added --noclean option, fix alignment

This commit is contained in:
eleksir
2017-11-15 18:13:30 +03:00
parent dfe37e2f24
commit c1dbcb52d3

80
cr.pl Normal file → Executable file
View File

@@ -1,5 +1,6 @@
#!/usr/bin/perl
# TODO: --clean option to cleanup repo accrding to metadata
# TODO: implement paths fool proof paths (add trailing backslash if it absent on url and path)
#
use strict;
use warnings "all";
@@ -17,16 +18,33 @@ sub urlencode($);
sub getmetadata();
sub help($);
sub path_normalize_by_string_manipulation($);
sub getfilenames($);
help(0) unless (defined $ARGV[0]);
help(1) unless (defined $ARGV[1]);
help(0) if ($ARGV[0] eq '--help');
help(0) if ($ARGV[0] eq '-h');
my $noclean = 0;
if (defined($ARGV[0]) and $ARGV[0] eq '--noclean') {
$noclean = 1;
}
my $BASEURL = $ARGV[0];
my $BASEDIR = $ARGV[1];
my $primaryxmlgz = getmetadata();
if ($noclean == 1) {
help(1) unless (defined $ARGV[2]);
$BASEURL = $ARGV[1];
$BASEDIR = $ARGV[2];
}
my @REPOFILES;
my @FILES;
my $primaryxmlgz = getmetadata();
my $fh = new IO::Zlib;
my $xmldata = '';
@@ -77,14 +95,46 @@ for (my $i = 0; $i < @array; $i++) {
}
}
exit 0 if ($noclean == 1);
my $searchpath = $BASEDIR;
chop $searchpath;
getfilenames($searchpath);
my @filesToDelete;
foreach my $fileOnDisk (@FILES) {
my $flag = 0;
foreach my $fileInXML (@REPOFILES) {
if ($fileInXML eq $fileOnDisk) {
$flag = 1;
last;
}
}
if ($flag == 0) {
push @filesToDelete, $fileOnDisk;
}
}
foreach my $file (@filesToDelete) {
unless (-d $file) {
unlink $file;
}
}
exit 0;
sub help($) {
my $rc = shift;
my $help = <<'HELP';
Usage: cr.pl source destination
Usage: cr.pl [--noclean] source destination
Where
--noclean is optional argument, if it specified, no cleanup operation will
be performed after rpm downloading complete.
Source must be http(s) url which trimmed at repodata folder. If full url is
http://mirror.yandex.ru/centos/7.4.1708/os/x86_64/repodata/repomd.xml
source must be given as:
@@ -111,6 +161,8 @@ sub dlfunc(@) {
return;
}
push @REPOFILES, $savepath;
# do not re-download already downloaded rpm packages
unless ($size == -1) {
@@ -229,4 +281,26 @@ sub path_normalize_by_string_manipulation ($) {
return $ret;
}
sub getfilenames($) {
my $path = shift;
opendir (DIR, $path) or return "";
my @localfiles;
@localfiles =
map { $path . '/' . $_ }
grep { !/^\.{1,2}$/ }
readdir (DIR);
@FILES = (@FILES, @localfiles);
foreach (@localfiles) {
next if(-l $_);
if( -d $_){
getfilenames($_);
}
}
}
__END__