2020-04-02 15:57:28 +02:00
|
|
|
#!/bin/sh
|
|
|
|
# rpmsort implemented mostly in Lua
|
|
|
|
# Copyright (c) 2020 SUSE LLC
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
# Author: fvogt@suse.de
|
|
|
|
# Enhanced by: mwilck@suse.com
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
rpmsort() {
|
|
|
|
direction=$1
|
|
|
|
script='
|
|
|
|
function parse(ver)
|
|
|
|
local epoch, version, release = 0, ver, 0
|
|
|
|
_, eend, e = ver:find("^(%d+):")
|
|
|
|
if eend then
|
|
|
|
ver = ver:sub(eend + 1)
|
|
|
|
version = ver
|
|
|
|
epoch = e
|
|
|
|
end
|
|
|
|
_, _, v, r = ver:find("(.+)%-(.+)$")
|
|
|
|
if v then
|
|
|
|
version = v
|
|
|
|
release = r
|
|
|
|
end
|
|
|
|
return epoch, version, release
|
|
|
|
end
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
function pkgvercmp(a, b)
|
|
|
|
local ae, av, ar = parse(a)
|
|
|
|
local be, bv, br = parse(b)
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
local ecmp = rpm.vercmp(ae, be)
|
|
|
|
if ecmp ~= 0 then return ecmp end
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
local vcmp = rpm.vercmp(av, bv)
|
|
|
|
if vcmp ~= 0 then return vcmp end
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
return rpm.vercmp(ar, br)
|
|
|
|
end
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
vers = {}
|
|
|
|
for line in io.stdin:lines() do
|
|
|
|
table.insert(vers, line)
|
|
|
|
end
|
|
|
|
table.sort(vers, function(a, b) return pkgvercmp(a, b) == '"$direction"' end)
|
|
|
|
print(table.concat(vers, "\n"))
|
|
|
|
'
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
rpm --eval "%{lua: ${script}}"
|
2006-12-19 00:17:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
usage() {
|
|
|
|
cat >&2 <<EOF
|
|
|
|
$0 - sort input according to rpm version sorting conventions
|
2011-05-05 00:04:44 +02:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
Expects rpm package versions separated by newlines as input and outputs
|
|
|
|
them sorted according to rpm version sorting conventions, with old versions
|
|
|
|
at the top.
|
2011-05-05 00:04:44 +02:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
Options:
|
|
|
|
-r|--reverse sort backwards
|
|
|
|
-h|--help print this help
|
|
|
|
EOF
|
|
|
|
exit 0
|
2011-05-05 00:04:44 +02:00
|
|
|
}
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
DIRECTION=-1
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case $1 in
|
|
|
|
-r|--reverse) DIRECTION=1;;
|
|
|
|
-h|--help) usage;;
|
|
|
|
*) echo "$0: invalid parameter $1 ignored" >&2;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2006-12-19 00:17:44 +01:00
|
|
|
|
2020-04-02 15:57:28 +02:00
|
|
|
rpmsort "$DIRECTION"
|