27 Commits

Author SHA1 Message Date
858d44b5ca Bump version to v1.0.1
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-21 15:45:25 +02:00
e18136e43c Makefile: create parent directories if not exists.
Signed-off-by: Nicolas A. Ortega Froysa <nicolas.ortega@babelgroup.com>
2024-05-21 15:41:36 +02:00
d692d4aa59 Makefile: Fix PREFIX handling.
Signed-off-by: Nicolas A. Ortega Froysa <nicolas.ortega@babelgroup.com>
2024-05-21 15:41:36 +02:00
1516ce9ff3 Include section for supported AUR helpers.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-07 18:07:13 +02:00
555ead272e Improve repository check.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-07 18:05:11 +02:00
d616930208 Remove unnecessary sudo.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-07 17:49:20 +02:00
347b49d5ee Find local package when reinstalling removed package.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-02 13:36:07 +02:00
641163a495 Implement dry-run mode.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-02 13:17:02 +02:00
75c663eb1e Update help information.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-02 13:12:34 +02:00
8525b7f4e8 Automatically detect package manager.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-05-02 13:00:27 +02:00
e6d814d438 Add more helpful information to the README.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-30 17:21:08 +02:00
a7bae301b3 Add compressed manpage to gitignore.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-29 18:54:52 +02:00
b2f5793c21 Add manpage.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-29 18:53:57 +02:00
edd942b91a Create script to build and install pacundo.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-29 18:29:04 +02:00
b30dea35b8 Implement package management commands.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-26 10:00:28 +02:00
d90016fd8c Obtain package files for installation.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-04-26 09:48:45 +02:00
c08a9f4b40 Simplify and clean up strings and output.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 11:40:57 +01:00
ec535f860c Add TODO for autodetecting AUR helper.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 11:32:38 +01:00
a458f2dc67 Fix filter regex.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 11:14:16 +01:00
615f491433 Simplify get_pkgmgr() subroutine.
Currently it only is configured to deal with pacman or yay (as noted in
the comment). Other AUR helpers would need to be configured.

Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 10:45:24 +01:00
aa7e2d2fbc Add package manager remove command.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 10:42:49 +01:00
f55124759e Don't print stderr output of commands (send to stdout).
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-28 10:37:46 +01:00
3822696640 Use single print statement for help information.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-27 10:39:14 +01:00
b7e98246ad Align assignment operators.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-27 10:33:50 +01:00
2d4ec58a81 Get package manager variables.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-27 10:26:35 +01:00
9e1ba7159f Simplify code.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-13 17:15:31 +01:00
601b4b7b81 Change license copyright year.
Signed-off-by: Nicolás Ortega Froysa <nicolas@ortegas.org>
2024-03-12 09:27:27 +01:00
6 changed files with 265 additions and 33 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
pacundo
pacundo.1.gz

View File

@ -1,4 +1,4 @@
Copyright (C) [year] Nicolás A. Ortega Froysa <nicolas@ortegas.org> Copyright (C) 2024 Nicolás A. Ortega Froysa <nicolas@ortegas.org>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages

43
Makefile Normal file
View File

@ -0,0 +1,43 @@
# Copyright (C) 2024 Ortega Froysa, Nicolás <nicolas@ortegas.org> All rights reserved.
# Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source
# distribution.
ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
pacundo: pacundo.pl
pp -o $@ $^
pacundo.1.gz: pacundo.1
gzip -c $^ > $@
.PHONY: install clean
clean:
$(RM) pacundo
$(RM) pacundo.1.gz
install: pacundo pacundo.1.gz
install -d $(PREFIX)/bin
install -m 755 pacundo $(PREFIX)/bin/
install -d $(PREFIX)/share/man/man1
install -m 644 pacundo.1.gz $(PREFIX)/share/man/man1/

View File

@ -7,10 +7,47 @@ to boot from a USB depending on just how broken it is).
## Installation ## Installation
Dependencies: ### Dependencies
- Perl 5 - Perl 5
- `File::ReadBackwards` module - `File::ReadBackwards` module
- PAR Packager (for compiling)
- GNU Makefile
You can install these packages with the following command:
```console
# pacman -S perl perl-file-readbackwards perl-par-packer
```
### Compiling & Installing
The script is compiled and installed using GNU Makefile. Therefore you can use
`make install` to build and install the script and its man-page as expected.
They are installed (by default) to `/usr/local`. To change this to a different
directory simply prepend the `PREFIX=<path>` to your `make install` command.
## Usage
The first concept to understand is that of a transaction. A transaction is
defined in the pacman logs as package operations done during a single use of the
command (or so it seems, at least). If you look at the logs
(`/var/log/pacman.log`) this would be everything between the lines `[ALPM]
transaction started` and `[ALPM] transaction completed`. You can set how many
transactions to list/undo by using the `-t` argument.
There are two modes for undoing pacman transactions:
- Interactive (`-i`, default): will show you a numbered list with all the package
operations of the selected transactions.
- Automatic (`-r`): will automatically undo all package operations of the
selected transactions.
Look at the man-page (`man pacundo`) for more information.
### Supported AUR Helpers
- [yay](https://github.com/Jguer/yay)
## License ## License

49
pacundo.1 Normal file
View File

@ -0,0 +1,49 @@
.TH PACUNDO "1" "April 2024" "pacundo 1.0.1" "User Commands"
.SH "NAME"
pacundo - A time machine to return your ArchLinux machine back to a working state.
.SH "SYNOPSIS"
.B pacundo
[\fI\-i\fR|\fI\-r\fR] [\fI\-t\fR <\fInum\fR>] [\fI\-d\fR]
.B pacundo
\fI\-h\fR
.B pacundo
\fI\-v\fR
.SH "DESCRIPTION"
This program helps to undo the last pacman/yay transactions automatically, which
is especially helpful if the last update broke your system for some reason.
\fBNote:\fR the term \fItransaction\fR refers to an operation done by pacman
(e.g. all packages upgraded by \fIpacman -Syu\fR).
.SH "OPTIONS"
.TP
\fB\-i\fR
Enter interactive mode to select package operations to undo (default behavior)
.TP
\fB\-r\fR
Non-interactively undo entire transactions
.TP
\fB\-t\fR <\fInum\fR>
Specify the number of transactions to include (default: 1)
.TP
\fB\-d\fR
Dry run, i.e. don't actually do anything
.TP
\fB\-h\fR
Show this help information
.TP
\fB\-v\fR
Print program version
.SH "AUTHOR"
Written by Nicolás A. Ortega Froysa.
.SH "COPYRIGHT"
Copyright \(co 2024 Ortega Froysa, Nicolás A. <nicolas@ortegas.org>.
License: ZLib License.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

View File

@ -29,8 +29,8 @@ use feature qw(signatures);
use Getopt::Std; use Getopt::Std;
use File::ReadBackwards; use File::ReadBackwards;
my $VERSION = "1.0"; my $VERSION = '1.0.1';
my $PROG_NAME = "pacundo"; my $PROG_NAME = 'pacundo';
sub print_version() { sub print_version() {
print("$PROG_NAME v$VERSION\n"); print("$PROG_NAME v$VERSION\n");
@ -39,16 +39,17 @@ sub print_version() {
sub print_help() { sub print_help() {
&print_version(); &print_version();
print("A time machine to return your ArchLinux machine back to a working state.\n"); print("A time machine to return your ArchLinux machine back to a working state.
print("\nUSAGE:
USAGE:
$PROG_NAME [-i|-r] [-t <num>] [-d] $PROG_NAME [-i|-r] [-t <num>] [-d]
$PROG_NAME -h $PROG_NAME -h
$PROG_NAME -v $PROG_NAME -v
OPTIONS: OPTIONS:
-i Enter interactive mode to select packages to downgrade [default behavior] -i Enter interactive mode to select package operations to undo (default behavior)
-r Automatically downgrade all packages from last upgrade -r Non-interactively undo entire transactions
-t <num> Specify the number of transactions to include for undoing selection [default 1] -t <num> Specify the number of transactions to include (default: 1)
-d Dry run, i.e. don't actually do anything -d Dry run, i.e. don't actually do anything
-h Show this help information -h Show this help information
-v Print program version\n"); -v Print program version\n");
@ -59,8 +60,8 @@ sub read_txs($num_txs = 1) {
my $found_txs = 0; my $found_txs = 0;
my $in_tx = 0; my $in_tx = 0;
my @undo_txs; my @undo_txs;
my $pacman_log = File::ReadBackwards->new("/var/log/pacman.log") || my $pacman_log = File::ReadBackwards->new('/var/log/pacman.log') or
die("Failed to load pacman log file.\n$!"); die("Failed to load pacman log file.\n$!\n");
while ($found_txs < $num_txs && defined(my $line = $pacman_log->readline)) { while ($found_txs < $num_txs && defined(my $line = $pacman_log->readline)) {
unless ($in_tx) { unless ($in_tx) {
@ -76,18 +77,18 @@ sub read_txs($num_txs = 1) {
$line =~ /\[ALPM\] (upgraded|downgraded) ([^\s]+) \((.*) -> (.*)\)/; $line =~ /\[ALPM\] (upgraded|downgraded) ([^\s]+) \((.*) -> (.*)\)/;
push(@undo_txs, push(@undo_txs,
{ {
'action' => $action, action => $action,
'pkg_name' => $pkg_name, pkg_name => $pkg_name,
'oldver' => $oldver, oldver => $oldver,
'newver' => $newver, newver => $newver,
} }
); );
} elsif ($line =~ /\[ALPM\] (installed|removed)/) { } elsif ($line =~ /\[ALPM\] (installed|removed)/) {
my ($action, $pkg_name) = $line =~ /\[ALPM\] (installed|removed) ([^\s]+)/; my ($action, $pkg_name) = $line =~ /\[ALPM\] (installed|removed) ([^\s]+)/;
push(@undo_txs, push(@undo_txs,
{ {
'action' => $action, action => $action,
'pkg_name' => $pkg_name, pkg_name => $pkg_name,
} }
); );
} }
@ -117,30 +118,95 @@ $n, $tx->{action}, $tx->{pkg_name}
$n++; $n++;
} }
print("Select transactions to undo (e.g. '1 2 3', '1-3')\n"); print("Select transactions to undo (e.g. '1 2 3', '1-3')\n> ");
print("> ");
my @sel = split(' ', <STDIN>); my @sel = split(' ', <STDIN>);
foreach my $i (@sel) { foreach my $i (grep({/^[0-9]+-[0-9]+$/} @sel)) {
if ($i =~ /^[0-9]+-[0-9]+$/) { my ($start, $end) = $i =~ /^([0-9]+)-([0-9]+)$/;
my ($start, $end) = $i =~ /^([0-9]+)-([0-9]+)$/; die("Invalid range: $start-$end\n") if ($start >= $end);
if ($start >= $end) { push(@sel, ($start..$end));
die("Invalid range: $start-$end\n"); }
}
push(@sel, ($start..$end)); @sel = sort grep({!/[0-9]+-[0-9]+/} @sel);
my @sel_undo;
push(@sel_undo, $undo_txs[$_-1]) foreach (@sel);
return @sel_undo;
}
# NOTE: Currently this subroutine only works for pacman and yay. You'll have to
# add options for additional AUR helpers.
sub get_pkgmgr() {
my $mgr = '';
my $mgr_bin;
my @supported_mgrs = (
'yay',
'pacman',
);
my $sudo = '';
my $user = $ENV{LOGNAME} || $ENV{USER};
foreach my $i (@supported_mgrs) {
$mgr_bin = `which $i 2>&1`;
if ($? == 0) {
$mgr = $i;
last;
} }
} }
@sel = sort grep({!/[0-9+]-[0-9+]/} @sel); if ($mgr eq '') {
print(STDERR "Failed to find pacman executable. Are you using an ArchLinux system?\n");
exit 1;
}
chomp($mgr_bin);
my @sel_undo; if ($mgr eq 'pacman' && $user ne 'root') {
$sudo = 'sudo';
foreach my $i (@sel) {
push(@sel_undo, $undo_txs[$i-1]);
} }
return @sel_undo; my %pkgmgr = (
name => $mgr,
bin => $mgr_bin,
search => "$mgr_bin -Ss",
info => "$mgr_bin -Si",
install_remote => "$sudo $mgr_bin -S",
install_local => "$sudo $mgr_bin -U",
remove => "$sudo $mgr_bin -R",
);
return \%pkgmgr;
}
sub find_local_pkg($pkgmgr, $pkg_name, $pkg_ver='') {
my $pkg_file = '';
my $pkg_pat;
my $repo = `$pkgmgr->{info} $pkg_name | awk '{ if (\$1 == "Repository") print \$3; }'`;;
if ($pkg_ver ne '') {
$pkg_pat = "$pkg_name-$pkg_ver-*.pkg.tar.zst";
} else {
$pkg_pat = "$pkg_name-*.pkg.tar.zst";
}
if ($repo eq 'aur') {
my $aur_dir;
if ($pkgmgr->{name} eq 'yay') {
$aur_dir = "$ENV{'XDG_CACHE_HOME'}/yay/$pkg_name";
} else {
return '';
}
$pkg_file = `ls $aur_dir/$pkg_pat | tail -n1`;
} else {
$pkg_file = `ls /var/cache/pacman/pkg/$pkg_pat | tail -n1`;
}
chomp($pkg_file);
return $pkg_file;
} }
getopts("irt:dvh", \my %opts); getopts("irt:dvh", \my %opts);
@ -160,11 +226,46 @@ if ($opts{'v'}) {
exit 1; exit 1;
} }
my $r_flag = $opts{'r'} // 0; my $r_flag = $opts{'r'} // 0;
my $dry_run = $opts{'d'} // 0; my $dry_run = $opts{'d'} // 0;
my $num_txs = $opts{'t'} // 1; my $num_txs = $opts{'t'} // 1;
my $pkgmgr = &get_pkgmgr();
my @undo_txs = &read_txs($num_txs); my @undo_txs = &read_txs($num_txs);
# Interactive mode # Interactive mode
@undo_txs = &select_txs(@undo_txs) unless ($r_flag); @undo_txs = &select_txs(@undo_txs) unless ($r_flag);
my $remove_pkgs = ""; # executed via -R
my $remote_install_pkgs = ""; # executed via -S
my $local_install_pkgs = ""; # executed via -U
foreach my $tx (@undo_txs) {
if ($tx->{action} eq 'installed') {
$remove_pkgs .= "$tx->{pkg_name} ";
} elsif ($tx->{action} eq 'removed') {
my $pkg_file = &find_local_pkg($pkgmgr, $tx->{pkg_name});
if ($pkg_file eq '') {
$remote_install_pkgs .= "$tx->{pkg_name} ";
} else {
$local_install_pkgs .= "$pkg_file ";
}
} else {
my $pkg_file = &find_local_pkg($pkgmgr, $tx->{pkg_name}, $tx->{oldver});
if ($pkg_file eq '') {
$remote_install_pkgs .= "$tx->{pkg_name} ";
} else {
$local_install_pkgs .= "$pkg_file ";
}
}
}
if ($dry_run) {
print("$pkgmgr->{remove} $remove_pkgs\n") if ($remove_pkgs ne '');
print("$pkgmgr->{install_remote} $remote_install_pkgs\n") if ($remote_install_pkgs ne '');
print("$pkgmgr->{install_local} $local_install_pkgs\n") if ($local_install_pkgs ne '');
} else {
system("$pkgmgr->{remove} $remove_pkgs") if ($remove_pkgs ne '');
system("$pkgmgr->{install_remote} $remote_install_pkgs") if ($remote_install_pkgs ne '');
system("$pkgmgr->{install_local} $local_install_pkgs") if ($local_install_pkgs ne '');
}