epm-history 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
__alt_epm_history_uniq()
{
	a= journalctl -t apt-get | grep "apt-get\[[0-9][0-9]*\]:" | sed -e "s|.*apt-get\[\([0-9][0-9]*\)\]: .*|\1|" | uniq | tac
}

# args: pid, removed|installed
__alt_epm_history_select()
{
	local pid="$1"
	local verb="$2"
	a= journalctl -t apt-get | grep "apt-get\[$pid\]: .*$verb" | sed -e "s|.*apt-get\[[0-9][0-9]*\]: ||" | cut -d" " -f 1
}

_alt_epm_history_date()
{
	local pid="$1"
	a= journalctl -t apt-get | grep  "apt-get\[$pid\]: " | head -n1 | cut -d" " -f 1-3
}

__alt_epm_history_removed()
{
	echo "Removed packages history:"
	__alt_epm_history_uniq | while read pid ; do
		date="$(_alt_epm_history_date $pid)"
		echo
		echo "$date apt-get session $pid:"
		removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
		installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
		epm tool --quiet estrlist exclude "$installed" "$removed" | xargs -n1 echo | sed -e "s|^|    |"
	done
}

__alt_epm_history_installed()
{
	echo "Installed packages history:"
	__alt_epm_history_uniq | while read pid ; do
		echo
		date="$(_alt_epm_history_date $pid)"
		echo "$date apt-get session $pid:"
		#epm print shortname for $(__alt_epm_history_select $pid "installed") | sed -e "s|^|    |"
		removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
		installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
		epm tool --quiet estrlist exclude "$removed" "$installed" | xargs -n1 echo | sed -e "s|^|    |"
	done
}

__alt_epm_history_updated()
{
	echo "Updated packages history:"
	__alt_epm_history_uniq | while read pid ; do
		echo
		date="$(_alt_epm_history_date $pid)"
		echo "$date apt-get session $pid:"
		#epm print shortname for $(__alt_epm_history_select $pid "installed") | sed -e "s|^|    |"
		removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
		installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
		epm tool --quiet estrlist intersection "$removed" "$installed" | xargs -n1 echo | sed -e "s|^|    |"
	done
}

epm_history_help()
{
	echo "package management history"
			get_help HELPCMD $SHAREDIR/epm-history
	cat <<EOF
Examples:
  epm history
  epm history --removed
EOF
}


92 93 94
epm_history()
{

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
if [ $PMTYPE = "apt-rpm" ] ; then
	case "$1" in
		"-h"|"--help"|"help")      # HELPCMD: help
			epm_history_help
			return
			;;
		--installed)               # HELPCMD: print only new installed packages
			__alt_epm_history_installed | less
			return
			;;
		--removed)                 # HELPCMD: print only removed packages
			__alt_epm_history_removed #| less
			return
			;;
		--updated)                 # HELPCMD: print only updated packages
			__alt_epm_history_updated | less
			return
			;;
113 114 115 116 117 118
		--list)                    # HELPCMD: (or empty) print all history entries
			docmd journalctl -t apt-get
			return
			;;
		"")
			;;
119 120 121 122 123
		*)
			fatal "Unknown option $1"
	esac
fi

124 125 126
[ -z "$*" ] || fatal "No arguments are allowed here"

case $PMTYPE in
127 128 129
	apt-rpm)
		docmd journalctl -t apt-get
		;;
130 131 132 133 134 135
	apt-dpkg)
		docmd less /var/log/dpkg.log
		;;
	dnf-rpm)
		sudocmd dnf history
		;;
136 137 138
	eopkg)
		sudocmd eopkg history
		;;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	zypper-rpm)
		less /var/log/zypp/history
		;;
	pacman)
		docmd less /var/log/pacman.log
		;;
	emerge)
		docmd less /var/log/portage
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}