Compare commits

...

10 Commits

Author SHA1 Message Date
Nicolás A. Ortega Froysa 8a62fc9064 Avoid invalid ranges.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-11 20:09:16 +01:00
Nicolás A. Ortega Froysa e6514c37d3 Simplify syntax.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-11 20:05:40 +01:00
Nicolás A. Ortega Froysa e11cc694f5 Fix selection regex.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-11 20:02:59 +01:00
Nicolás A. Ortega Froysa 4cb213d585 Refactor into subroutines.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-09 16:47:25 +01:00
Nicolás A. Ortega Froysa e9eec6cfcc Use local $~
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-07 16:25:03 +01:00
Nicolás A. Ortega Froysa 1065595f6d Change grep format.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-07 16:15:28 +01:00
Nicolás A. Ortega Froysa e7b3192a00 Add 'return' statement to subroutines.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-07 16:12:29 +01:00
Nicolás A. Ortega Froysa 891427ead0 Use || instead of 'or'.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-03 18:51:44 +01:00
Nicolás A. Ortega Froysa 9dc43cc866 Implement interactive mode.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-03 18:34:18 +01:00
Nicolás A. Ortega Froysa 86c20031bc Use // operator for args/flags default values.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-01 18:00:25 +01:00
1 changed files with 99 additions and 45 deletions

View File

@ -24,6 +24,7 @@
use strict;
use warnings;
use feature qw(signatures);
use Getopt::Std;
use File::ReadBackwards;
@ -31,15 +32,12 @@ use File::ReadBackwards;
my $VERSION = "1.0";
my $PROG_NAME = "pacundo";
my $r_flag = 0;
my $dry_run = 0;
my $num_txs = 1;
sub print_version {
sub print_version() {
print("$PROG_NAME v$VERSION\n");
return;
}
sub print_help {
sub print_help() {
&print_version();
print("A time machine to return your ArchLinux machine back to a working state.\n");
print("\nUSAGE:
@ -54,6 +52,95 @@ OPTIONS:
-d Dry run, i.e. don't actually do anything
-h Show this help information
-v Print program version\n");
return;
}
sub read_txs($num_txs = 1) {
my $found_txs = 0;
my $in_tx = 0;
my @undo_txs;
my $pacman_log = File::ReadBackwards->new("/var/log/pacman.log") ||
die("Failed to load pacman log file.\n$!");
while ($found_txs < $num_txs && defined(my $line = $pacman_log->readline)) {
unless ($in_tx) {
# Remeber that we're reading this in reverse order
if ($line =~ /\[ALPM\] transaction completed/) {
$in_tx = 1;
}
} elsif ($line =~ /\[ALPM\] transaction started/) {
$found_txs++;
$in_tx = 0;
} elsif ($line =~ /\[ALPM\] (upgraded|downgraded)/) {
my ($action, $pkg_name, $oldver, $newver) =
$line =~ /\[ALPM\] (upgraded|downgraded) ([^\s]+) \((.*) -> (.*)\)/;
push(@undo_txs,
{
'action' => $action,
'pkg_name' => $pkg_name,
'oldver' => $oldver,
'newver' => $newver,
}
);
} elsif ($line =~ /\[ALPM\] (installed|removed)/) {
my ($action, $pkg_name) = $line =~ /\[ALPM\] (installed|removed) ([^\s]+)/;
push(@undo_txs,
{
'action' => $action,
'pkg_name' => $pkg_name,
}
);
}
}
return @undo_txs;
}
sub select_txs(@undo_txs) {
print("Last changes:\n");
my $n = 1;
foreach my $tx (@undo_txs) {
format UPGRFORMAT =
@|| @<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<< -> @<<<<<<<<<<<<<
$n, $tx->{action}, $tx->{pkg_name}, $tx->{oldver}, $tx->{newver}
.
format INSTFORMAT =
@|| @<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$n, $tx->{action}, $tx->{pkg_name}
.
local $~ = ($tx->{action} =~ /(upgraded|downgraded)/) ? "UPGRFORMAT" : "INSTFORMAT";
write();
$n++;
}
print("Select transactions to undo (e.g. '1 2 3', '1-3')\n");
print("> ");
my @sel = split(' ', <STDIN>);
foreach my $i (@sel) {
if ($i =~ /^[0-9]+-[0-9]+$/) {
my ($start, $end) = $i =~ /^([0-9]+)-([0-9]+)$/;
if ($start >= $end) {
die("Invalid range: $start-$end\n");
}
push(@sel, ($start..$end));
}
}
@sel = sort grep({!/[0-9+]-[0-9+]/} @sel);
my @sel_undo;
foreach my $i (@sel) {
push(@sel_undo, $undo_txs[$i-1]);
}
return @sel_undo;
}
getopts("irt:dvh", \my %opts);
@ -73,44 +160,11 @@ if ($opts{'v'}) {
exit 1;
}
$r_flag = 1 if ($opts{'r'});
$dry_run = 1 if ($opts{'d'});
$num_txs = $opts{'t'} if ($opts{'t'});
my $r_flag = $opts{'r'} // 0;
my $dry_run = $opts{'d'} // 0;
my $num_txs = $opts{'t'} // 1;
my $pacman_log = File::ReadBackwards->new("/var/log/pacman.log") or
die("Failed to load pacman log file.\n$!");
my @undo_txs = &read_txs($num_txs);
my $found_txs = 0;
my $in_tx = 0;
my @undo_txs;
while ($found_txs < $num_txs && defined(my $line = $pacman_log->readline)) {
# Remeber that we're reading this in reverse order
if (!$in_tx && $line =~ /\[ALPM\] transaction completed/) {
$in_tx = 1;
} elsif ($in_tx) {
if ($line =~ /\[ALPM\] transaction started/) {
$found_txs++;
$in_tx = 0;
} elsif ($line =~ /\[ALPM\] (upgraded|downgraded)/) {
my ($action, $pkg_name, $oldver, $newver) = $line =~ /\[ALPM\] (upgraded|downgraded) ([^\s]+) \((.*) -> (.*)\)/;
push(@undo_txs,
{
'action' => $action,
'pkg_name' => $pkg_name,
'oldver' => $oldver,
'newver' => $newver,
}
);
} elsif ($line =~ /\[ALPM\] (installed|removed)/) {
my ($action, $pkg_name) = $line =~ /\[ALPM\] (installed|removed) ([^\s]+)/;
push(@undo_txs,
{
'action' => $action,
'pkg_name' => $pkg_name,
}
);
}
}
}
# Interactive mode
@undo_txs = &select_txs(@undo_txs) unless ($r_flag);