Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

apt4rpm completion: _rpm_packages



Hello,

I would like to add apt4rpm support to _apt completion.

The first step is to introduce Completion/Redhat/Type/_rpm_packages
(attached) tailored after Completion/Debian/Type/_deb_packages.
Except massive s/deb/rpm/ substitutions, difference is as follows:

--- zsh-4.2.4/Completion/Redhat/Type/_rpm_packages-	2005-01-09 09:34:11 +0300
+++ zsh-4.2.4/Completion/Redhat/Type/_rpm_packages	2005-03-11 12:30:53 +0300
@@ -19,10 +19,9 @@ _rpm_packages_update_installed () {
   if ( [[ ${+_rpm_packages_cache_installed} -eq 0 ]] ||
       _cache_invalid RPMS_installed ) && ! _retrieve_cache RPMS_installed;
   then
-    _rpm_packages_cache_installed=()
-    dpkg --get-selections | while read package state ; do
-        [[ $state = (install|hold) ]] && _rpm_packages_cache_installed+=$package
-    done
+    _rpm_packages_cache_installed=(
+      ${(f)"$(rpm -qa --qf '%{NAME}\n')"}
+    )
     _store_cache RPMS_installed _rpm_packages_cache_installed
   fi
   cachevar=_rpm_packages_cache_installed
@@ -106,13 +105,23 @@ _rpm_packages () {
   _tags packages && compadd "$expl[@]" - "${(@P)cachevar}"
 }
 
- _rpms_caching_policy () {
-    # rebuild if cache is more than a week old
-      oldp=( "$1"(mw+1) )
-        (( $#oldp )) && return 0
-
-	  [[ /var/cache/apt/pkgcache.bin -nt "$1" ||
-	     /var/lib/dpkg/available -nt "$1" ]]
-	  }
+_rpms_caching_policy () {
+  local cache_path="$1" cache_id="${_cache_ident:-${1##*/}}"
+  
+  # rebuild if cache is more than a week old
+  oldp=( "$cache_path"(mw+1) )
+  (( $#oldp )) && return 0
+
+  if [[ $cache_id = RPMS_installed ]]; then
+    pkg_indices=( /var/lib/rpm/{packages.rpm,Packages}(N) )
+  else
+    pkg_indices=( /var/lib/apt/lists/*list*(.N) )
+  fi
+
+  for pkg_index in $pkg_indices; do
+    [[ "$pkg_index" -nt "$cache_path" ]] && return 0
+  done
+  return 1
+}
 
 _rpm_packages "$@"
End of patch

This has a few tricks:

1) _cache_ident variable (found in _retrieve_cache) is used to determine
which cache is being checked for invalidation;

2) for installed RPM packages, cache invalidation is checked against RPM
database;

3) for packages which are not installed, cache invalidation is checked
against APT source repository files.  This is different from original
check against /var/cache/apt/pkgcache.bin -- this file gets rebuilt on
every apt-cache invocation.

I also noticed that in recent zsh versions additional support for held
and deinstalled packages was added to _deb_packages, but since RPM has
no simple notion of held/deinstalled packages, those functions are left
intact for now.

Please comment.

-- 
Alexey Tourbin
ALT Linux Team
#autoload

# Usage: _rpm_packages expl... avail|installed|uninstalled

_rpm_packages_update_avail () {
  if ( [[ ${+_rpm_packages_cache_avail} -eq 0 ]] ||
      _cache_invalid RPMS_avail ) && ! _retrieve_cache RPMS_avail;
  then
    _rpm_packages_cache_avail=(
      ${(f)"$(apt-cache --generate pkgnames)"}
    )

    _store_cache RPMS_avail _rpm_packages_cache_avail
  fi
  cachevar=_rpm_packages_cache_avail
}

_rpm_packages_update_installed () {
  if ( [[ ${+_rpm_packages_cache_installed} -eq 0 ]] ||
      _cache_invalid RPMS_installed ) && ! _retrieve_cache RPMS_installed;
  then
    _rpm_packages_cache_installed=(
      ${(f)"$(rpm -qa --qf '%{NAME}\n')"}
    )
    _store_cache RPMS_installed _rpm_packages_cache_installed
  fi
  cachevar=_rpm_packages_cache_installed
}

_rpm_packages_update_held () {
  if ( [[ ${+_rpm_packages_cache_held} -eq 0 ]] ||
      _cache_invalid RPMS_held ) && ! _retrieve_cache RPMS_held;
  then
    _rpm_packages_cache_held=()
    dpkg --get-selections | while read package state ; do
        [[ $state = hold ]] && _rpm_packages_cache_held+=$package
    done
    _store_cache RPMS_held _rpm_packages_cache_held
  fi
  cachevar=_rpm_packages_cache_held
}

_rpm_packages_update_deinstalled () {
  if ( [[ ${+_rpm_packages_cache_deinstalled} -eq 0 ]] ||
      _cache_invalid RPMS_deinstalled ) && ! _retrieve_cache RPMS_deinstalled;
  then
    _rpm_packages_cache_deinstalled=()
    dpkg --get-selections | while read package state ; do
        [[ $state = deinstall ]] && _rpm_packages_cache_deinstalled+=$package
    done
    _store_cache RPMS_deinstalled _rpm_packages_cache_deinstalled
  fi
  cachevar=_rpm_packages_cache_deinstalled
}

_rpm_packages_update_xinstalled () {
  if ( [[ ${+_rpm_packages_cache_xinstalled} -eq 0 ]] ||
      _cache_invalid RPMS_xinstalled ) && ! _retrieve_cache RPMS_xinstalled;
  then
    _rpm_packages_cache_xinstalled=()
    dpkg --get-selections | while read package state ; do
        _rpm_packages_cache_xinstalled+=$package
    done
    _store_cache RPMS_xinstalled _rpm_packages_cache_xinstalled
  fi
  cachevar=_rpm_packages_cache_xinstalled
}

_rpm_packages_update_uninstalled () {
  _rpm_packages_update_avail
  _rpm_packages_update_installed
  if (( ! $+_rpm_packages_cache_uninstalled )); then
    _rpm_packages_cache_uninstalled=(
      ${_rpm_packages_cache_avail:#${(j:|:)~${_rpm_packages_cache_installed:q}}}
    )
  fi
  cachevar=_rpm_packages_cache_uninstalled
}

_rpm_packages () {
  local command="$argv[$#]" expl cachevar pkgset update_policy

  zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
  if [[ -z "$update_policy" ]]; then
    zstyle ":completion:*:*:$service:*" cache-policy _rpms_caching_policy
  fi

  [[ "$command" = (installed|deinstalled|xinstalled|held|uninstalled|avail|available) ]] || {
    _message "unknown command: $command"
    return
  }

  zstyle -s ":completion:${curcontext}:" packageset pkgset

  [[ "$pkgset" = (installed|deinstalled|xinstalled|held|uninstalled|avail|available) ]] || {
    pkgset="$command"
  }

  [[ "$pkgset" = "available" ]] && pkgset="avail"

  expl=("${(@)argv[1,-2]}")

  _rpm_packages_update_$pkgset

  _tags packages && compadd "$expl[@]" - "${(@P)cachevar}"
}

_rpms_caching_policy () {
  local cache_path="$1" cache_id="${_cache_ident:-${1##*/}}"
  
  # rebuild if cache is more than a week old
  oldp=( "$cache_path"(mw+1) )
  (( $#oldp )) && return 0

  if [[ $cache_id = RPMS_installed ]]; then
    pkg_indices=( /var/lib/rpm/{packages.rpm,Packages}(N) )
  else
    pkg_indices=( /var/lib/apt/lists/*list*(.N) )
  fi

  for pkg_index in $pkg_indices; do
    [[ "$pkg_index" -nt "$cache_path" ]] && return 0
  done
  return 1
}

_rpm_packages "$@"

Attachment: pgpbvsYmnLYS5.pgp
Description: PGP signature



Messages sorted by: Reverse Date, Date, Thread, Author