Blame SOURCES/BZ-1470647-add-pre-transaction-actions-plugin.patch

5b4f08
diff -up yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.conf.orig yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.conf
5b4f08
--- yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.conf.orig	2017-10-27 17:30:16.579550073 +0200
5b4f08
+++ yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.conf	2017-10-27 17:30:16.579550073 +0200
5b4f08
@@ -0,0 +1,3 @@
5b4f08
+[main]
5b4f08
+enabled = 1
5b4f08
+actiondir = /etc/yum/pre-actions/
5b4f08
diff -up yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.py.orig yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.py
5b4f08
--- yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.py.orig	2017-10-27 17:30:16.579550073 +0200
5b4f08
+++ yum-utils-1.1.31/plugins/pre-transaction-actions/pre-transaction-actions.py	2017-10-27 17:30:16.579550073 +0200
5b4f08
@@ -0,0 +1,164 @@
5b4f08
+# This program is free software; you can redistribute it and/or modify
5b4f08
+# it under the terms of the GNU General Public License as published by
5b4f08
+# the Free Software Foundation; either version 2 of the License, or
5b4f08
+# (at your option) any later version.
5b4f08
+#
5b4f08
+# This program is distributed in the hope that it will be useful,
5b4f08
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
5b4f08
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5b4f08
+# GNU Library General Public License for more details.
5b4f08
+#
5b4f08
+# You should have received a copy of the GNU General Public License
5b4f08
+# along with this program; if not, write to the Free Software
5b4f08
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5b4f08
+
5b4f08
+# Copyright 2008 Red Hat, Inc
5b4f08
+# written by Seth Vidal <skvidal@fedoraproject.org>
5b4f08
+
5b4f08
+"""
5b4f08
+This plugin runs actions prior to the transaction based on the content of the
5b4f08
+transaction.
5b4f08
+"""
5b4f08
+
5b4f08
+
5b4f08
+from yum.plugins import TYPE_CORE
5b4f08
+from yum.constants import *
5b4f08
+import yum.misc
5b4f08
+from yum.parser import varReplace
5b4f08
+from yum.packages import parsePackages
5b4f08
+import fnmatch
5b4f08
+import re
5b4f08
+import os
5b4f08
+import glob
5b4f08
+import shlex
5b4f08
+
5b4f08
+requires_api_version = '2.4'
5b4f08
+plugin_type = (TYPE_CORE,)
5b4f08
+
5b4f08
+def parse_actions(ddir, conduit):
5b4f08
+    """read in .action files from ddir path. 
5b4f08
+       store content in a list of tuples"""
5b4f08
+    action_tuples = [] # (action key, action_state, shell command)
5b4f08
+    action_file_list = []
5b4f08
+    if os.access(ddir, os.R_OK): 
5b4f08
+        action_file_list.extend(glob.glob(ddir + "*.action"))
5b4f08
+
5b4f08
+    if action_file_list:
5b4f08
+        for f in action_file_list:
5b4f08
+            for line in open(f).readlines():
5b4f08
+                line = line.strip()
5b4f08
+                if line and line[0] != "#":
5b4f08
+                    try:
5b4f08
+                        (a_key, a_state, a_command) = line.split(':', 2)
5b4f08
+                    except ValueError,e:
5b4f08
+                        conduit.error(2,'Bad Action Line: %s' % line)
5b4f08
+                        continue
5b4f08
+                    else:
5b4f08
+                        action_tuples.append((a_key, a_state, a_command))
5b4f08
+
5b4f08
+    return action_tuples
5b4f08
+
5b4f08
+def _convert_vars(txmbr, command):
5b4f08
+    """converts %options on the command to their values from the package it
5b4f08
+       is running it for: takes $name, $arch, $ver, $rel, $epoch, 
5b4f08
+       $state, $repoid"""
5b4f08
+    state_dict = { TS_INSTALL: 'install',
5b4f08
+                   TS_TRUEINSTALL: 'install',
5b4f08
+                   TS_OBSOLETING: 'obsoleting',
5b4f08
+                   TS_UPDATE: 'updating',
5b4f08
+                   TS_ERASE: 'remove',
5b4f08
+                   TS_OBSOLETED: 'obsoleted',
5b4f08
+                   TS_UPDATED: 'updated'}
5b4f08
+    try:
5b4f08
+        state = state_dict[txmbr.output_state]
5b4f08
+    except KeyError:
5b4f08
+        state = 'unknown - %s' % txmbr.output_state
5b4f08
+
5b4f08
+    vardict = {'name': txmbr.name,
5b4f08
+               'arch': txmbr.arch,
5b4f08
+               'ver': txmbr.version,
5b4f08
+               'rel': txmbr.release,
5b4f08
+               'epoch': txmbr.epoch,
5b4f08
+               'repoid': txmbr.repoid,
5b4f08
+               'state':  state }
5b4f08
+
5b4f08
+    result = varReplace(command, vardict)
5b4f08
+    return result
5b4f08
+            
5b4f08
+
5b4f08
+def pretrans_hook(conduit):
5b4f08
+    # we have provides/requires for everything
5b4f08
+    # we do not have filelists for erasures
5b4f08
+    # we have to fetch filelists for the package object for installs/updates
5b4f08
+    action_dir = conduit.confString('main','actiondir','/etc/yum/pre-actions/')
5b4f08
+    action_tuples = parse_actions(action_dir, conduit)
5b4f08
+    commands_to_run = {}
5b4f08
+    ts = conduit.getTsInfo()
5b4f08
+    all = ts.getMembers()
5b4f08
+    removes = ts.getMembersWithState(output_states=TS_REMOVE_STATES)
5b4f08
+    installs = ts.getMembersWithState(output_states=TS_INSTALL_STATES)
5b4f08
+    updates = ts.getMembersWithState(output_states=[TS_UPDATE, TS_OBSOLETING])
5b4f08
+
5b4f08
+    for (a_k, a_s, a_c) in action_tuples:
5b4f08
+        #print 'if %s in state %s the run %s' %( a_k, a_s, a_c)
5b4f08
+        if a_s  == 'update':
5b4f08
+            pkgset = updates
5b4f08
+        elif a_s == 'install':
5b4f08
+            pkgset = installs
5b4f08
+        elif a_s == 'remove':
5b4f08
+            pkgset = removes
5b4f08
+        elif a_s == 'any':
5b4f08
+            pkgset = all
5b4f08
+        else:
5b4f08
+            # no idea what this is skip it
5b4f08
+            conduit.error(2,'whaa? %s' % a_s)
5b4f08
+            continue
5b4f08
+
5b4f08
+        if a_k.startswith('/'):
5b4f08
+            if yum.misc.re_glob(a_k):
5b4f08
+                restring = fnmatch.translate(a_k)
5b4f08
+                c_string = re.compile(restring)
5b4f08
+
5b4f08
+            for txmbr in pkgset:
5b4f08
+                matched = False
5b4f08
+                thispo = txmbr.po
5b4f08
+        
5b4f08
+                if not yum.misc.re_glob(a_k):
5b4f08
+                    if a_k in thispo.filelist + thispo.dirlist + thispo.ghostlist:
5b4f08
+                        thiscommand = _convert_vars(txmbr, a_c)
5b4f08
+                        commands_to_run[thiscommand] = 1
5b4f08
+                        matched = True
5b4f08
+                else:
5b4f08
+                    for name in thispo.filelist + thispo.dirlist + thispo.ghostlist:
5b4f08
+                        if c_string.match(name):
5b4f08
+                            thiscommand = _convert_vars(txmbr, a_c)
5b4f08
+                            commands_to_run[thiscommand] = 1
5b4f08
+                            matched = True
5b4f08
+                            break
5b4f08
+                
5b4f08
+                if matched:
5b4f08
+                    break
5b4f08
+            continue
5b4f08
+        
5b4f08
+        if a_k.find('/') == -1: # pkgspec
5b4f08
+            pkgs = [ txmbr.po for txmbr in pkgset ]
5b4f08
+            e,m,u = parsePackages(pkgs, [a_k])
5b4f08
+            if not u:
5b4f08
+                for pkg in e+m:
5b4f08
+                    for txmbr in ts.getMembers(pkgtup=pkg.pkgtup):
5b4f08
+                        thiscommand = _convert_vars(txmbr, a_c)
5b4f08
+                        commands_to_run[thiscommand] = 1
5b4f08
+            continue
5b4f08
+
5b4f08
+    for comm in commands_to_run.keys():
5b4f08
+        try:
5b4f08
+            args = shlex.split(comm)
5b4f08
+        except ValueError, e:
5b4f08
+            conduit.error(2,"command was not parseable: %s" % comm)
5b4f08
+            continue
5b4f08
+        #try
5b4f08
+        conduit.info(2,'Running pre transaction command: %s' % comm)
5b4f08
+        p = os.system(comm)
5b4f08
+        #except?
5b4f08
+
5b4f08
+
5b4f08
diff -up yum-utils-1.1.31/plugins/pre-transaction-actions/sample.action.orig yum-utils-1.1.31/plugins/pre-transaction-actions/sample.action
5b4f08
--- yum-utils-1.1.31/plugins/pre-transaction-actions/sample.action.orig	2017-10-27 17:30:16.579550073 +0200
5b4f08
+++ yum-utils-1.1.31/plugins/pre-transaction-actions/sample.action	2017-10-27 17:30:16.579550073 +0200
5b4f08
@@ -0,0 +1,21 @@
5b4f08
+#action_key:transaction_state:command
5b4f08
+# action_key can be: pkgglob, /path/to/file (wildcards allowed)
5b4f08
+# transaction_state can be: install,update,remove,any
5b4f08
+# command can be: any shell command
5b4f08
+#  the following variables are allowed to be passed to any command:
5b4f08
+#   $name - package name
5b4f08
+#   $arch - package arch
5b4f08
+#   $ver - package version
5b4f08
+#   $rel - package release
5b4f08
+#   $epoch - package epoch
5b4f08
+#   $repoid - package repository id
5b4f08
+#   $state - text string of state of the package in the transaction set
5b4f08
+#
5b4f08
+# file matches cannot be used with removes b/c we don't have the info available
5b4f08
+
5b4f08
+*:install:touch /tmp/$name-installed
5b4f08
+zsh:remove:touch /tmp/zsh-removed
5b4f08
+zsh:install:touch /tmp/zsh-installed-also
5b4f08
+/bin/z*h:install:touch /tmp/bin-zsh-installed
5b4f08
+z*h:any:touch /tmp/bin-zsh-any
5b4f08
+