From 31b246c5be2072f4a59b1ce8a2c193d2cc01af2c Mon Sep 17 00:00:00 2001 From: Will Budic Date: Fri, 22 Aug 2025 06:36:22 +1000 Subject: [PATCH] Lists now after distro user installed packages. --- sudo_apt.pl | 55 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/sudo_apt.pl b/sudo_apt.pl index 2873295..6cf7dfe 100755 --- a/sudo_apt.pl +++ b/sudo_apt.pl @@ -28,7 +28,9 @@ my $result = GetOptions( "hist|history" => \$history, "cmd|command|c=s" => \$command, "apt-help" => sub{say `apt-get --help`; help()}, - "showLog" => \$showLog + "showLog" => \$showLog, + "list" => \&list_sys_installed + ); my $packages = join ' ', @ARGV; $data_dir .= "/" if $data_dir && $data_dir !~ m/ \/$/; @@ -57,7 +59,7 @@ if(!$command){ $packages = $2; } if ( !$command || $command eq 'install' && !$packages ) -{say "Hey ", RED, $ENV{USER}, RESET,"! What you wanna do? Maybe try option [ $0 --help ]?";exit 0} +{say "Hey ", RED, $ENV{USER}, RESET,"! What you wanna do? Maybe try option [ $0 --help ]?";exit 0} $| = 1; # Disable buffering on STDOUT. @@ -73,7 +75,7 @@ if ( defined($command) and $result = `sudo -k apt-get --yes $command $packages my @res = split "\n", $result; my $log = 1; my $output =""; my ($aptResult, $f_app_ins, $f_app_rem, $appRemoved,$appInstalled); - + foreach my $next(@res){ if($next =~ m/sudo: a terminal is required to read the password;/){ die $next @@ -124,12 +126,12 @@ if ( defined($command) and $result = `sudo -k apt-get --yes $command $packages if($appInstalled){ my @installed = split ',', $appInstalled; foreach my $package(@installed){ - my $info = `apt-cache show $package`; + my $info = `apt-cache show $package`; $info =~ m/^Description[-\w]*:\s*(.*?)\./ms; my $desc = $1; $info =~ m/(^Version\s*:.*?\n)/ms; my $ver = $1; $ver =~ m/.*?:(.*)/; my $version = $1; $info =~ m/(^Origin\s*:.*?\n)/ms; my $origin = $1; - $info =~ m/(^Depends\s*:.*?\n)/ms; my $depends = $1; - $info =~ m/(^Recommends\s*:.*?\n)/ms; my $recommends = $1; + $info =~ m/(^Depends\s*:.*?\n)/ms; my $depends = $1; + $info =~ m/(^Recommends\s*:.*?\n)/ms; my $recommends = $1; $info =~ m/(^Filename\s*:.*?\n)/ms; my $filename = $1; $info =~ m/(^Maintainer\s*:.*?\n)/ms; my $maintainer = $1; my $log_row = "$date`$package`$version`$desc`\n$ver$origin$depends$recommends$filename$maintainer`~\n"; @@ -143,10 +145,10 @@ if ( defined($command) and $result = `sudo -k apt-get --yes $command $packages if($appRemoved){ my @removed = split ',', $appRemoved; foreach my $package(@removed){ - my $info = `apt-cache show $package`; + my $info = `apt-cache show $package`; $info =~ m/^Description[-\w]*:\s*(.*?)\./ms; my $desc = $1; $info =~ m/(^Version\s*:.*?\n)/ms; my $ver = $1; $ver =~ m/.*?:(.*)/; my $version = $1; - $info =~ m/(^Filename\s*:.*?\n)/ms; my $filename = $1; + $info =~ m/(^Filename\s*:.*?\n)/ms; my $filename = $1; my $log_row = "$date`$package`$version`$desc`$filename~"; $CNF->parse(undef,qq( << CNF_APP_APT_REMOVED_PACKAGES __HAS_HEADER__ @@ -222,6 +224,35 @@ sub fit { return $txt . ' ' x $l . " | "; } +sub list_sys_installed { + my (@ret, @ret_installed); + my @res = map { /^(\S+)/ } + split( "\n", `dpkg --get-selections | grep -w "install"` ); + my $distro_install_date = `stat \/ | grep \"Birth\"`; + $distro_install_date =~ s/Birth:\s//; + + say +"Current after system $distro_install_date date User installed packages (dpkg --get-selections | grep -w \"install\"):\n"; + foreach my $package (@res) { + my $r = `whatis $package 2>&1`; + if ( $r !~ /nothing\sappropriate\.\s*$/ ) { + $r =~ s/^\s*|\s*$//gm; + $ret[@ret] = $package; + my $info = `zgrep \"install $package\" /var/log/dpkg.log*`; + #say $r; + if($info){ + # Is in dpkg log and past birth, definitely was installed after the ubuntu/mint/debian distro. + #say $info; + my @date = ($info =~ /(\S+):(\S*\s\S*)/m); + my $cdate = CNFDateTime::_toCNFDate($date[1],$TZ); + say $r, " [Installed: ", $cdate->toSchlong(), " (", $cdate->toDateTimeFormat(),")]"; + $ret_installed[@ret_installed] = [$package, $cdate]; + } + } + } + return \@ret_installed; +} + sub createDataCNFFile { my @content = ; $CNF->parse( undef, \@content ); @@ -239,7 +270,8 @@ sub help { say qq(Apt Installation with PerlCNF v.).CNFParser::VERSION().qq( This script runs sudo apt install, keeping in CNF data script format an log, of apt-get packages installed. -Useful is also tool to list all what have installed using it, to output to an bash script format. +Useful is also tool to list all what hase been installed using it, + to output to an bash script format . So when that new computer arrives, you edit and run that bash script, on it to install, what commonly use. Note - You can modify this script to suit your needs for provided default options. @@ -248,7 +280,7 @@ Usage: perl sudo_apt.pl install {some_package} Available options: --help|? - This Help. --apt-help - To obtain available apt-get commands to ie. -> apt-get --help - --data_dir={\$path} - Where to log history, default is "$data_dir". +--data_dir = { \$path } - Where to log history, default is "$data_dir" . --data_file={name} - Name of log to use, default is "$data_file". --showLog - Prints the whole log sofar to the STDOUT, useful if --xz=1. --tz|timezone={zone} - Timezone to use, default is "$TZ". @@ -256,8 +288,9 @@ Available options: --hist|history - Display the CNF data log data, other options are ignored. --generate-batch - Generate install batchfile from history. - --cmd|command|c={apt command} The default apt-get command to use is assumed to be 'install', + --cmd|command|c={apt command} The default apt-get command to use is assumed to be 'install', followed by packages or list of packages. +--list - List user installed all packages, regardless of utility . ); exit } -- 2.34.1