-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.sh
More file actions
164 lines (140 loc) · 4.18 KB
/
Copy pathsorting.sh
File metadata and controls
164 lines (140 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
# regex to match any ebuild https://projects.gentoo.org/pms/8/pms.html
# [0-9]+(\.[0-9]+)+([a-z])?(_alpha|_beta|_pre|_rc|_p)?([a-z])?(-r([0-9]))?.ebuild
function pms_sort() {
(( $# < 2 )) && if [[ -n "$1" ]]; then
echo "$1"
return 0
else
ewarn "No version for sorting provided" "\n"
return 2
fi
local latest=""
local args=""
args=("${@:1}")
for (( i = 1; i < $#; i++ )); do
latest="$(pms_inner_sort "${args[$i]}" "${latest:-${args[$((i - 1))]}}")"
echo "comparing: $((i - 1)) and $i" >> ./test
echo -e "args[0] = ${args[0]} and args[1] = ${args[1]} and args[2] = ${args[2]}\n" >> ./test
done
echo "$latest"
}
# Original python code for portage can be found here:
# /usr/lib/python3.12/site-packages/portage/versions.py:121
function pms_inner_sort() {
local _unknown_repo="__unknown__"
# \w is [a-zA-Z0-9_]
# PMS 3.1.3: A slot name may contain any of the characters [A-Za-z0-9+_.-].
# It must not begin with a hyphen or a dot.
local _slot="([\w+][\w+.-]*)"
# 2.1.1 A category name may contain any of the characters [A-Za-z0-9+_.-].
# It must not begin with a hyphen or a dot.
local _cat="[\w+][\w+.-]*"
# 2.1.2 A package name may contain any of the characters [A-Za-z0-9+_-].
# It must not begin with a hyphen,
# and must not end in a hyphen followed by one or more digits.
local _pkg="[\w+][\w+-]*?"
local _v="([0-9]+)((\.[0-9]+)*)([a-z]?)((_(pre|p|beta|alpha|rc)[0-9]*)*)"
local _rev="[0-9]+"
local _vr="$_v"
_vr+="?(-r("
_vr+="$_rev"
_vr+="))?"
local _cp="("
_cp+="$_cat"
_cp+="/"
_cp+="$_pkg"
_cp+="(-"
_cp+="$_vr"
_cp+=")?)"
local _cpv="("
_cpv+="$_cp"
_cpv+="-"
_cpv+="$_vr"
_cpv+=")"
local _pv="(?P<pn>"
_pv+="$_pkg"
_pv+="(?P<pn_inval>-"
_pv+="$_vr"
_pv+=")?)"
_pv+="-(?P<ver>"
_pv+="$_v"
_pv+=")(-r(?P<rev>"
_pv+="$_rev"
_pv+="))?"
# local ver_regexp=
local ver_regexp="^"
ver_regexp+="$_vr"
ver_regexp+="$"
declare -a match1=()
declare -a match2=()
if [[ $1 =~ $ver_regexp ]]; then
for (( i=1; i<${#BASH_REMATCH[@]}; i++ )); do
match1+=("${BASH_REMATCH[$i]}")
done
else
# TODO: replace these with eerrors and ETOOLS_DEBUG checks
echo "ERROR: syntax error in version: $1" && return 2;
fi
if [[ $2 =~ $ver_regexp ]]; then
for (( i=1; i<${#BASH_REMATCH[@]}; i++ )); do
match2+=("${BASH_REMATCH[$i]}")
done
else
# TODO: replace these with eerrors and ETOOLS_DEBUG checks
echo "ERROR: syntax error in version: $2" && return 2;
fi
echo "$ver_regexp"
echo "${match1[@]}"
echo "${match2[@]}"
if (( match1[0] < match2[0] )); then
echo "$2"
return 0
elif (( match1[0] > match2[0] )); then
echo "$1"
return 0
fi
declare -a list1=()
declare -a list2=()
IFS='.' read -r -a vlist1 <<< "${match1[1]:1}"
IFS='.' read -r -a vlist2 <<< "${match2[1]:1}"
for (( i=0; i<( ${#vlist1[@]} > ${#vlist2[@]} ? ${#vlist1[@]} : ${#vlist2[@]} ); i++ )); do
if (( ${#vlist1[@]} <= i || ${#vlist1[$i]} == 0 )); then
# echo -e "length: ${#vlist1[@]}\tvalue: ${vlist1[$i]}\tvalue length: ${#vlist1[$i]}"
list1+=("-1")
list2+=("${vlist2[$i]}")
elif (( ${#vlist2[@]} <= i || ${#vlist2[$i]} == 0 )); then
list1+=("${vlist1[$i]}")
list2+=("-1")
elif [[ ${vlist1[$i]} != 0* && ${vlist2[$i]} != 0* ]]; then
list1+=("${vlist1[$i]}")
list2+=("${vlist2[$i]}")
else
local max_len=$(( ${#vlist1[$i]} > ${#vlist2[$i]} ? ${#vlist1[$i]} : ${#vlist2[$i]} ))
list1+=("$(printf "%-s" "${vlist1[$i]}$(printf "%${max_len}s" | tr ' ' "0")")")
list2+=("$(printf "%-s" "${vlist2[$i]}$(printf "%${max_len}s" | tr ' ' "0")")")
fi
done
[[ -n ${match1[3]} ]] && list1+=("$(printf "%d" "'${match1[3]}")")
[[ -n ${match2[3]} ]] && list2+=("$(printf "%d" "'${match2[3]}")")
for (( i=0; i<( ${#list1[@]} > ${#list2[@]} ? ${#list1[@]} : ${#list2[@]} ); i++ )); do
if (( ${#list1[@]} <= i )); then
# WARN: in bash this will expand to exit code 255
return -1
fi
if (( ${#list2[@]} <= i )); then
return 1
fi
if [[ ${list1[$i]} != "${list2[$i]}" ]]; then
local a=${list1[$i]}
local b=${list2[$i]}
local rval=$(( (a > b) - (a < b) ))
return $rval
fi
done
IFS='_' read -r -a list1 <<< "${match1[4]:1}"
IFS='_' read -r -a list2 <<< "${match2[4]:1}"
echo "${list1[@]}"
echo "${list2[@]}"
echo "$1"
}