~fredg/twapake

d790b9588a0e354d7284cdd2f4a3ea014e8858c2 — Fred. Galusik 1 year, 2 months ago 4dec22f
make subroutine to allow the dir choices
1 files changed, 19 insertions(+), 35 deletions(-)

M Twapake/Twapake.pm
M Twapake/Twapake.pm => Twapake/Twapake.pm +19 -35
@@ 27,7 27,10 @@ our $removepkg = "/sbin/removepkg";

our $file;
our @files;
our %hache;
our @sortfiles;
our $npkg;
our $nlpkg;
our ( $a, $b );
our $now = localtime;
our $nowfile = strftime("%Y-%m-%d-%H-%M-%S", localtime(time));


@@ 56,42 59,25 @@ sub checkrm {
#
# get the packages list
#
$dir = $pkgdir unless defined($dir);
sub get_files {
    $dir = shift;
    print "get_files_dir: $dir\n";
    opendir(DIR, $dir) or die "Cannot open '$dir' : $!\n";
    @files = grep !/^\./, readdir(DIR);
    closedir(DIR);
    return @files;
}
get_files($dir);

#
# get the total of installed packages
#
sub nbpkg {
    our $npkg = @files;
    return $npkg;
}

#
# build a hash with key = pkg name and value = last time modification
#
keys our %hache = @files;
foreach $file (@files) {
    our $stpkg = stat("$dir/$file");
    $hache{$file} = $stpkg->mtime;
}

#
# sort hash by descending value date
#
sub sortfiles {
    # get the number of pkgs
    $npkg = @files;
    # build a hash whith key = pkg name and value = mtime
    keys our %hache = @files;
    foreach $file (@files) {
        our $stpkg = stat("$dir/$file");
        $hache{$file} = $stpkg->mtime;
        }
    # sort files last mtime first
    @sortfiles = sort { $hache{$b} <=> $hache{$a} } @files;
    return @sortfiles;
    
    
    return @files, $npkg, @sortfiles, %hache;
}
sortfiles();

#
# make the twapake snapshot:


@@ 101,7 87,7 @@ sub twalog {
    open(TWAFILE, ">$twa_path/${nowfile}_twapake.log");
    print TWAFILE "\nTwapake snapshot on $now\n";
    print TWAFILE '='x72 ."\n\n";
    print TWAFILE "Installed dates\t\tPackages (total: ", nbpkg(), ")\n";
    print TWAFILE "Installed dates\t\tPackages (total: ", $npkg, ")\n";
    print TWAFILE '-'x19 ."\t" . '-'x45 ."\n";
    for $file (@sortfiles) {
        printf TWAFILE "%s\t%s\n",


@@ 114,11 100,10 @@ sub twalog {
# make a less detailed list
# give the choice of number of packages
#
our $nlpkg;
$nlpkg = nbpkg() unless defined($nlpkg);
sub checknpkg {
    if ($nlpkg > nbpkg()) {
        die "Error: only ", nbpkg(), " packages are installed.\n";
    $nlpkg = $npkg unless defined($nlpkg);
    if ($nlpkg > $npkg) {
        die "Error: only ", $npkg, " packages are installed.\n";
    }
}



@@ 133,7 118,6 @@ sub sortmtimelist {
# remove last installed packages
#
sub rmpkg {
    checknpkg();
    checkrm();
    print "Do you wish to remove all these packages ?\n";
    foreach $file ( 0..($nlpkg - 1) ) {