epm-history 4.68 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
EHOG='\(apt-get\|rpm\)'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
21
JCHAN='-t apt-get -t rpm'
22 23 24

__alt_epm_history_journal()
{
25
    a= journalctl $JCHAN
26 27
}

28 29
__alt_epm_history_uniq()
{
30
    __alt_epm_history_journal | grep "$EHOG\[[0-9][0-9]*\]:" | sed -e "s@.*$EHOG\[\([0-9][0-9]*\)\]: .*@\2@" | uniq | tac
31 32 33 34 35
}

# args: pid, removed|installed
__alt_epm_history_select()
{
36 37 38
    local pid="$1"
    local verb="$2"
    __alt_epm_history_journal | grep "$EHOG\[$pid\]: .*$verb" | sed -e "s@.*$EHOG\[[0-9][0-9]*\]: @@" | cut -d" " -f 1
39 40 41 42
}

_alt_epm_history_date()
{
43 44
    local pid="$1"
    __alt_epm_history_journal | grep "$EHOG\[$pid\]: " | head -n1 | cut -d" " -f 1-3,5 | sed -e 's|:$||'
45 46
}

47 48
_alt_epm_history_print_group()
{
49 50 51 52 53 54 55 56 57 58 59 60 61
    local i

    if [ -n "$2" ] ; then
        echo
        echo "$1 session:"
        shift
    else
        return
    fi

    for i in $* ; do
        echo "    $i"
    done
62 63 64
}


65 66
__alt_epm_history_removed()
{
67 68 69 70 71 72 73
    echo "Removed packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $pid)"
        removed="$(epm print shortname for $(__alt_epm_history_select $pid "removed") )"
        installed="$(epm print shortname for $(__alt_epm_history_select $pid "installed") )"
        _alt_epm_history_print_group "$date" $(estrlist exclude "$installed" "$removed")
    done
74 75 76 77
}

__alt_epm_history_installed()
{
78 79 80 81 82 83 84 85
    echo "Installed packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $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") )"
        _alt_epm_history_print_group "$date" $(estrlist exclude "$removed" "$installed")
    done
86 87 88 89
}

__alt_epm_history_updated()
{
90 91 92 93 94 95 96 97
    echo "Updated packages history:"
    __alt_epm_history_uniq | while read pid ; do
        date="$(_alt_epm_history_date $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") )"
        _alt_epm_history_print_group "$date" $(estrlist intersection "$removed" "$installed")
    done
98 99 100 101
}

epm_history_help()
{
102 103 104
    echo "package management history"
            get_help HELPCMD $SHAREDIR/epm-history
    cat <<EOF
105 106 107 108 109 110 111
Examples:
  epm history
  epm history --removed
EOF
}


112 113 114
epm_history()
{

115
if [ $PMTYPE = "apt-rpm" ] ; then
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    case "$1" in
        "-h"|"--help"|"help")      # HELPCMD: help
            epm_history_help
            return
            ;;
        --installed)               # HELPCMD: print only new installed packages
            __alt_epm_history_installed
            return
            ;;
        --removed)                 # HELPCMD: print only removed packages
            __alt_epm_history_removed
            return
            ;;
        --updated)                 # HELPCMD: print only updated packages
            __alt_epm_history_updated
            return
            ;;
        --list)                    # HELPCMD: (or empty) print all history entries
            docmd journalctl $JCHAN
            return
            ;;
        "")
            ;;
        *)
            fatal "Unknown option $1. Use epm history --help to get help."
    esac
142 143
fi

144 145 146
[ -z "$*" ] || fatal "No arguments are allowed here"

case $PMTYPE in
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    apt-rpm)
        docmd journalctl $JCHAN -r
        ;;
    apt-dpkg)
        docmd cat /var/log/dpkg.log
        ;;
    dnf-rpm)
        sudocmd dnf history
        ;;
    eopkg)
        sudocmd eopkg history
        ;;
    zypper-rpm)
        docmd cat /var/log/zypp/history
        ;;
    pacman)
        docmd cat /var/log/pacman.log
        ;;
    emerge)
        docmd cat /var/log/portage
        ;;
    *)
        fatal "Have no suitable command for $PMTYPE"
        ;;
171 172 173
esac

}