summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/luasrc/model/ipkg.lua
diff options
context:
space:
mode:
authorChristian Schoenebeck <christian.schoenebeck@gmail.com>2015-07-27 18:08:54 +0200
committerChristian Schoenebeck <christian.schoenebeck@gmail.com>2015-07-27 18:08:54 +0200
commit305528b3ccc5972e159385014f61fcea01b6ac9f (patch)
tree9098c521da0a6cfec5d19d86658a84639ca001ca /modules/luci-base/luasrc/model/ipkg.lua
parentec170d62821579dea84e9333a7c7561dd7d59157 (diff)
ipkg.lua: new function compare_version
* minor fix function _list() set to local * new function compare_version() lua version of opkg compare-version Signed-off-by: Christian Schoenebeck <christian.schoenebeck@gmail.com>
Diffstat (limited to 'modules/luci-base/luasrc/model/ipkg.lua')
-rw-r--r--modules/luci-base/luasrc/model/ipkg.lua42
1 files changed, 41 insertions, 1 deletions
diff --git a/modules/luci-base/luasrc/model/ipkg.lua b/modules/luci-base/luasrc/model/ipkg.lua
index 587637272d..976081b84d 100644
--- a/modules/luci-base/luasrc/model/ipkg.lua
+++ b/modules/luci-base/luasrc/model/ipkg.lua
@@ -122,7 +122,7 @@ function upgrade()
end
-- List helper
-function _list(action, pat, cb)
+local function _list(action, pat, cb)
local fd = io.popen(ipkg .. " " .. action ..
(pat and (" '%s'" % pat:gsub("'", "")) or ""))
@@ -189,3 +189,43 @@ function overlay_root()
return od
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
+