Skip to content

Instantly share code, notes, and snippets.

@fumiyas
Last active May 8, 2017 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumiyas/67b842325284c8adc186a6d7d541c656 to your computer and use it in GitHub Desktop.
Save fumiyas/67b842325284c8adc186a6d7d541c656 to your computer and use it in GitHub Desktop.
RPM: Macros and SPEC to detect "from/to" package version in scriptlet

RPM %trigger* is broken:

  • %triggerin -- %{name} < %{version}-%{release} is always triggered on upgrade · Issue #209 · rpm-software-management/rpm
    rpm-software-management/rpm#209
%pre
if %{ver_cmp_opposite < 1.2.3-222.new}; then
echo "Upgrade from $(%{ver_opposite}) (< 1.2.3-222.new)"
fi
exit 0
%postun
if %{ver_cmp_opposite < 1.2.3-222.new}; then
echo "Downgrade to $(%{ver_opposite}) (< 1.2.3-222.new)"
fi
exit 0
## In %pre, %post of the installing package on upgrade or downgrade:
## Print version string of the uninstalling package
## In %preun, %postun of the uninstalling package on upgrade or downgrade:
## Print version string of the installing package
%ver_opposite(n:) \
LC_ALL=C \\\
rpm \\\
-q \\\
--queryformat '%%|epoch?{%%{epoch}:}|%%{version}-%%{release}\\n' \\\
%{!-n:%{name}}%{-n*} \\\
|fgrep -v '%{?epoch:%{epoch}:}%{version}-%{release}' \\\
|grep -v ' is not installed$' \\\
|| :
## Compare 2 version strings with an operator
%ver_cmp() \
%{__python} -c '\
from __future__ import print_function\
import sys\
import rpm\
\
\
def stringToEVR(verstring):\
i = verstring.find(":")\
if i == -1:\
epoch = None\
else:\
epoch = verstring[:i]\
i += 1\
j = verstring.find("-", i)\
if j == -1:\
version = verstring[i:]\
release = None\
else:\
version = verstring[i:j]\
release = verstring[j + 1:]\
return (epoch, version, release)\
\
\
v1 = sys.argv[1]\
operator = sys.argv[2]\
v2 = sys.argv[3]\
rc = rpm.labelCompare(stringToEVR(v1), stringToEVR(v2))\
sys.exit(0 if operator == ("<", "==", ">")[rc + 1] else 1)\
'
## In %pre, %post on upgrade or downgrade:
## Compare the uninstalling package version string and the specified one
## In %preun, %postun on upgrade or downgrade:
## Compare the installing package version string and the specified one
%ver_cmp_opposite(n:) \
(\
ver_opposite=$(%{expand:%%{ver_opposite %{-n*}}})\
[[ -z $ver_opposite ]] && exit 1\
%{ver_cmp} "$ver_opposite" "%{1}" "%{2}"\
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment