Merge pull request #429 from chris5560/master
ipkg.lua: new function compare_version()
This commit is contained in:
commit
fa88550215
2 changed files with 58 additions and 1 deletions
|
@ -122,7 +122,7 @@ function upgrade()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- List helper
|
-- List helper
|
||||||
function _list(action, pat, cb)
|
local function _list(action, pat, cb)
|
||||||
local fd = io.popen(ipkg .. " " .. action ..
|
local fd = io.popen(ipkg .. " " .. action ..
|
||||||
(pat and (" '%s'" % pat:gsub("'", "")) or ""))
|
(pat and (" '%s'" % pat:gsub("'", "")) or ""))
|
||||||
|
|
||||||
|
@ -189,3 +189,43 @@ function overlay_root()
|
||||||
|
|
||||||
return od
|
return od
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function compare_versions(ver1, comp, ver2)
|
||||||
|
if not ver1 or not ver2
|
||||||
|
or not comp or not (#comp > 0) then
|
||||||
|
error("Invalid parameters")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
-- correct compare string
|
||||||
|
if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
|
||||||
|
elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
|
||||||
|
elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
|
||||||
|
elseif comp == "=" or comp == "==" then comp = "=="
|
||||||
|
elseif comp == "<<" then comp = "<"
|
||||||
|
elseif comp == ">>" then comp = ">"
|
||||||
|
else
|
||||||
|
error("Invalid compare string")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local av1 = util.split(ver1, "[%.%-]", nil, true)
|
||||||
|
local av2 = util.split(ver2, "[%.%-]", nil, true)
|
||||||
|
|
||||||
|
for i = 1, math.max(table.getn(av1),table.getn(av2)), 1 do
|
||||||
|
local s1 = av1[i] or ""
|
||||||
|
local s2 = av2[i] or ""
|
||||||
|
|
||||||
|
-- first "not equal" found return true
|
||||||
|
if comp == "~=" and (s1 ~= s2) then return true end
|
||||||
|
-- first "lower" found return true
|
||||||
|
if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
|
||||||
|
-- first "greater" found return true
|
||||||
|
if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
|
||||||
|
-- not equal then return false
|
||||||
|
if (s1 ~= s2) then return false end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- all equal and not compare greater or lower then true
|
||||||
|
return not (comp == "<" or comp == ">")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
|
@ -107,3 +107,20 @@ Determines the overlay root used by opkg.
|
||||||
@return String containing the directory path of the overlay root.
|
@return String containing the directory path of the overlay root.
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
---[[
|
||||||
|
lua version of opkg compare-versions
|
||||||
|
|
||||||
|
@class function
|
||||||
|
@name compare_versions
|
||||||
|
@param ver1 string version 1
|
||||||
|
@param ver2 string version 2
|
||||||
|
@param comp string compare versions using
|
||||||
|
"<=" or "<" lower-equal
|
||||||
|
">" or ">=" greater-equal
|
||||||
|
"=" equal
|
||||||
|
"<<" lower
|
||||||
|
">>" greater
|
||||||
|
"~=" not equal
|
||||||
|
@return Boolean indicating the status of the compare
|
||||||
|
]]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue