[Yum-devel] [PATCH 5/8] Add a swap command, for simple shell cases.

James Antill james at and.org
Wed Jan 16 07:26:03 UTC 2013


---
 cli.py         |    1 +
 docs/yum.8     |   16 ++++++++
 yumcommands.py |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/cli.py b/cli.py
index 01b21e6..b74606c 100755
--- a/cli.py
+++ b/cli.py
@@ -107,6 +107,7 @@ class YumBaseCli(yum.YumBase, output.YumOutput):
         self.registerCommand(yumcommands.CheckRpmdbCommand())
         self.registerCommand(yumcommands.DistroSyncCommand())
         self.registerCommand(yumcommands.LoadTransactionCommand())
+        self.registerCommand(yumcommands.SwapCommand())
 
     def registerCommand(self, command):
         """Register a :class:`yumcommands.YumCommand` so that it can be called by
diff --git a/docs/yum.8 b/docs/yum.8
index 604bc26..505289d 100644
--- a/docs/yum.8
+++ b/docs/yum.8
@@ -310,6 +310,22 @@ should work (thus, all the simple cases will work). Also this does not
 work for "installonly" packages, like Kernels. downgrade operates
 on groups, files, provides, filelists and rpm files just like the "install" command\&.
 .IP
+.IP "\fBswap\fP"
+At it's simplest this is just a simpler way to remove one set of package(s) and
+install another set of package(s) without having to use the "shell" command.
+However you can specify different commands to call than just remove or install,
+and you can list multiple packages (it splits using the "--" marker).
+Note that option parsing will remove the first "--" in an argument list on the
+command line.
+
+
+Examples:
+
+swap foo bar
+swap -- remove foo -- install bar
+swap foo group install bar-grp
+swap -- group remove foo-grp -- group install bar-grp
+.IP
 .IP "\fBdeplist\fP"
 Produces a list of all dependencies and what packages provide those
 dependencies for the given packages. As of 3.2.30 it now just shows the latest
diff --git a/yumcommands.py b/yumcommands.py
index 14a1375..3e02dd6 100644
--- a/yumcommands.py
+++ b/yumcommands.py
@@ -97,6 +97,25 @@ def checkPackageArg(base, basecmd, extcmds):
         _err_mini_usage(base, basecmd)
         raise cli.CliError
 
+def checkSwapPackageArg(base, basecmd, extcmds):
+    """Verify that *extcmds* contains the name of at least two packages for
+    *basecmd* to act on.
+
+    :param base: a :class:`yum.Yumbase` object.
+    :param basecmd: the name of the command being checked for
+    :param extcmds: a list of arguments passed to *basecmd*
+    :raises: :class:`cli.CliError`
+    """
+    min_args = 2
+    if '--' in extcmds:
+        min_args = 3
+    if len(extcmds) < min_args:
+        base.logger.critical(
+                _('Error: Need at least two packages to %s') % basecmd)
+        _err_mini_usage(base, basecmd)
+        raise cli.CliError
+
+
 def checkItemArg(base, basecmd, extcmds):
     """Verify that *extcmds* contains the name of at least one item for
     *basecmd* to act on.  Generally, the items are command-line
@@ -3017,6 +3036,7 @@ class CheckRpmdbCommand(YumCommand):
         """
         return False
 
+
 class LoadTransactionCommand(YumCommand):
     """A class containing methods needed by the cli to execute the
     load-transaction command.
@@ -3083,3 +3103,96 @@ class LoadTransactionCommand(YumCommand):
         """
         return True
 
+
+class SwapCommand(YumCommand):
+    """A class containing methods needed by the cli to execute the
+    swap command.
+    """
+
+    def getNames(self):
+        """Return a list containing the names of this command.  This
+        command can be called from the command line by using any of these names.
+
+        :return: a list containing the names of this command
+        """
+        return ['swap']
+
+    def getUsage(self):
+        """Return a usage string for this command.
+
+        :return: a usage string for this command
+        """
+        return "[remove|cmd] <pkg|arg(s)> [-- install|cmd] <pkg|arg(s)>"
+
+    def getSummary(self):
+        """Return a one line summary of this command.
+
+        :return: a one line summary of this command
+        """
+        return _("Simple way to swap packages, isntead of using shell")
+
+    def doCheck(self, base, basecmd, extcmds):
+        """Verify that conditions are met so that this command can run.
+        These include that the program is being run by the root user,
+        that there are enabled repositories with gpg keys, and that
+        this command is called with appropriate arguments.
+
+        :param base: a :class:`yum.Yumbase` object
+        :param basecmd: the name of the command
+        :param extcmds: the command line arguments passed to *basecmd*
+        """
+        checkRootUID(base)
+        checkGPGKey(base)
+        checkSwapPackageArg(base, basecmd, extcmds)
+        checkEnabledRepo(base, extcmds)
+
+    def doCommand(self, base, basecmd, extcmds):
+        """Execute this command.
+
+        :param base: a :class:`yum.Yumbase` object
+        :param basecmd: the name of the command
+        :param extcmds: the command line arguments passed to *basecmd*
+        :return: (exit_code, [ errors ])
+
+        exit_code is::
+
+            0 = we're done, exit
+            1 = we've errored, exit with error string
+            2 = we've got work yet to do, onto the next stage
+        """
+
+        if '--' in extcmds:
+            off = extcmds.index('--')
+            rextcmds = extcmds[:off]
+            iextcmds = extcmds[off+1:]
+        else:
+            rextcmds = extcmds[:1]
+            iextcmds = extcmds[1:]
+
+        if not (rextcmds and iextcmds):
+            return 1, ['swap'] # impossible
+
+        if rextcmds[0] not in base.yum_cli_commands:
+            rextcmds = ['remove'] + rextcmds
+        if iextcmds[0] not in base.yum_cli_commands:
+            iextcmds = ['install'] + iextcmds
+
+        # Very similar to what the shell command does...
+        ocmds = base.cmds
+        oline = base.cmdstring
+        for cmds in (rextcmds, iextcmds):
+            base.cmdstring = " ".join(cmds)
+            base.cmds = cmds
+            #  Don't call this atm. as the line has gone through it already,
+            # also makes it hard to do the "is ?extcmds[0] a cmd" check.
+            # base.plugins.run('args', args=base.cmds)
+
+            # We don't catch exceptions, just pass them up and fail...
+            base.parseCommands()
+            cmdret = base.doCommands()
+            if cmdret[0] != 2:
+                return cmdret[0], ['%s %s' % (basecmd, " ".join(cmds))]
+        base.cmds      = ocmds
+        base.cmdstring = oline
+
+        return 2, ['%s %s' % (basecmd, " ".join(extcmds))]
-- 
1.7.6.5



More information about the Yum-devel mailing list