[yum-commits] completion-helper.py etc/yum.bash Makefile yum.spec

Ville Skyttä scop at osuosl.org
Tue Dec 20 16:51:49 UTC 2011


 Makefile             |    1 
 completion-helper.py |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 etc/yum.bash         |   63 +++++++++-----------------------------------
 yum.spec             |    1 
 4 files changed, 88 insertions(+), 49 deletions(-)

New commits:
commit a49224e12ea9d6fe14b6c5bc35c54cee365ee230
Author: Ville Skyttä <ville.skytta at iki.fi>
Date:   Thu Dec 1 22:12:23 2011 +0200

    Add completion helper to get rid of some screen scraping.

diff --git a/Makefile b/Makefile
index f73239b..e15a3f8 100644
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ install:
 	for p in $(PYFILES) ; do \
 		install -m 644 $$p $(DESTDIR)/usr/share/yum-cli/$$p; \
 	done
+	chmod 755 $(DESTDIR)/usr/share/yum-cli/completion-helper.py
 	mv $(DESTDIR)/usr/share/yum-cli/yum-updatesd.py $(DESTDIR)/usr/share/yum-cli/yumupd.py
 	$(PYTHON) -c "import compileall; compileall.compile_dir('$(DESTDIR)/usr/share/yum-cli', 1, '$(PYDIR)', 1)"
 
diff --git a/completion-helper.py b/completion-helper.py
new file mode 100755
index 0000000..7aecb23
--- /dev/null
+++ b/completion-helper.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python -t
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2011 Ville Skyttä
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import sys
+
+import cli
+import yumcommands
+
+
+class GroupsCompletionCommand(yumcommands.GroupsCommand):
+    def doCommand(self, base, basecmd, extcmds):
+        cmd, extcmds = self._grp_cmd(basecmd, extcmds)
+        # case insensitivity is fine here because groupinstall etc are that too
+        installed, available = base.doGroupLists()
+        if extcmds[0] in ("installed", "all"):
+            for group in installed:
+                print group.ui_name
+        if extcmds[0] in ("available", "all"):
+            for group in available:
+                print group.ui_name
+
+class ListCompletionCommand(yumcommands.ListCommand):
+    def doCommand(self, base, basecmd, extcmds):
+        ypl = base.doPackageLists(pkgnarrow=extcmds[0])
+        if extcmds[0] in ("installed", "all"):
+            for pkg in ypl.installed:
+                print pkg.na
+        if extcmds[0] in ("available", "all"):
+            for pkg in ypl.available:
+                print pkg.na
+
+class RepoListCompletionCommand(yumcommands.RepoListCommand):
+    def doCommand(self, base, basecmd, extcmds):
+        for repo in base.repos.repos.values():
+            if extcmds[0] == "all" \
+                    or (extcmds[0] == "enabled" and repo.isEnabled()) \
+                    or (extcmds[0] == "disabled" and not repo.isEnabled()):
+                print repo.id
+
+
+def main(args):
+    base = cli.YumBaseCli()
+    base.yum_cli_commands.clear()
+    base.registerCommand(GroupsCompletionCommand())
+    base.registerCommand(ListCompletionCommand())
+    base.registerCommand(RepoListCompletionCommand())
+    base.getOptionsConfig(args)
+    base.parseCommands()
+    base.doCommands()
+
+if __name__ == "__main__":
+    try:
+        main(sys.argv[1:])
+    except KeyboardInterrupt, e:
+        sys.exit(1)
diff --git a/etc/yum.bash b/etc/yum.bash
index f6a0039..69d7823 100644
--- a/etc/yum.bash
+++ b/etc/yum.bash
@@ -1,53 +1,18 @@
 # bash completion for yum
 
-# arguments:
-#   1 = argument to "yum list" (all, available, updates etc)
-#   2 = current word to be completed
-_yum_list()
+_yum_helper()
 {
-    # Fail fast for things that look like paths.
-    [[ $2 == */* || $2 == [.~]* ]] && return
-
-    if [[ $1 == all ]] ; then
-        # Try to strip in between headings like "Available Packages" - would
-        # be nice if e.g. -d 0 did that for us.  This will obviously only work
-        # for English :P
-        COMPREPLY+=( $( ${yum:-yum} -d 0 -C list $1 "$2*" 2>/dev/null | \
-            sed -ne '/^Available /d' -e '/^Installed /d' -e '/^Updated /d' \
-            -e 's/[[:space:]].*//p' ) )
-    else
-        # Drop first line (e.g. "Updated Packages") - would be nice if e.g.
-        # -d 0 did that for us.
-        COMPREPLY+=( $( ${yum:-yum} -d 0 -C list $1 "$2*" 2>/dev/null | \
-            sed -ne 1d -e 's/[[:space:]].*//p' ) )
-    fi
-}
-
-# arguments:
-#   1 = argument to "yum repolist" (enabled, disabled etc)
-#   2 = current word to be completed
-_yum_repolist()
-{
-    # TODO: add -d 0 when http://yum.baseurl.org/ticket/29 is fixed
-    #       (for now --noplugins is used to get rid of "Loaded plugins: ...")
-    # Drop first ("repo id      repo name") and last ("repolist: ...") rows -
-    # would be nice if e.g. -d 0 did that for us.
-    COMPREPLY+=(
-        $( compgen -W "$( ${yum:-yum} --noplugins -C repolist $1 2>/dev/null | \
-            sed -ne '/^repo\s\{1,\}id/d' -e '/^repolist:/d' \
-            -e 's/[[:space:]].*//p' )" -- "$2" ) )
+    local IFS=$'\n'
+    COMPREPLY+=( $( compgen -W "$(
+        /usr/share/yum-cli/completion-helper.py -d 0 -C $@ 2>/dev/null )" \
+            -- "$cur" ) )
 }
 
-# arguments:
-#   1 = argument to "yum grouplist" (usually empty (""), or hidden)
-#   2 = current word to be completed
-_yum_grouplist()
+_yum_list()
 {
-    local IFS=$'\n'
-    COMPREPLY=( $( compgen -W "$( ${yum:-yum} -d 0 -C grouplist $1 \
-        2>/dev/null | sed -e 's/[[:space:]]\{1,\}\[.*$//' \
-        -ne 's/^[[:space:]]\{1,\}\(.\{1,\}\)/\1/p' )" \
-        -- "$2" ) )
+    # Fail fast for things that look like paths.
+    [[ $2 == */* || $2 == [.~]* ]] && return
+    _yum_helper list "$@"
 }
 
 # arguments:
@@ -92,7 +57,7 @@ _yum_transactions()
 _yum_atgroups()
 {
     if [[ $1 == \@* ]]; then
-        _yum_grouplist "" "${1:1}"
+        _yum_helper groups list all "${1:1}"
         COMPREPLY=( "${COMPREPLY[@]/#/@}" )
         return 0
     fi
@@ -129,17 +94,17 @@ _yum_complete_baseopts()
             ;;
 
         --enablerepo)
-            _yum_repolist disabled "$1"
+            _yum_helper repolist disabled "$1"
             return 0
             ;;
 
         --disablerepo)
-            _yum_repolist enabled "$1"
+            _yum_helper repolist enabled "$1"
             return 0
             ;;
 
         --disableexcludes)
-            _yum_repolist all "$1"
+            _yum_helper repolist all "$1"
             COMPREPLY=( $( compgen -W '${COMPREPLY[@]} all main' -- "$1" ) )
             return 0
             ;;
@@ -254,7 +219,7 @@ _yum()
             ;;
 
         group*)
-            _yum_grouplist "" "$cur"
+            _yum_helper groups list all "$cur"
             return 0
             ;;
 
diff --git a/yum.spec b/yum.spec
index b78a9f6..572112a 100644
--- a/yum.spec
+++ b/yum.spec
@@ -254,6 +254,7 @@ exit 0
 %{_sysconfdir}/bash_completion.d
 %dir %{_datadir}/yum-cli
 %{_datadir}/yum-cli/*
+%exclude %{_datadir}/yum-cli/completion-helper.py?
 %if %{yum_updatesd}
 %exclude %{_datadir}/yum-cli/yumupd.py*
 %endif


More information about the Yum-commits mailing list