libs/ipkg: give caller more control over filter pattern
[project/luci.git] / libs / ipkg / luasrc / model / ipkg.lua
index 4c2716a..d0d9788 100644 (file)
 --[[
-LuCI - IPKG wrapper library
+LuCI - Lua Configuration Interface
 
-Description:
-Wrapper for the ipkg Package manager
+(c) 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
+(c) 2008 Steven Barth <steven@midlink.org>
 
-Any return value of false or nil can be interpreted as an error
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
 
-FileId:
 $Id$
+]]--
 
-License:
-Copyright 2008 Steven Barth <steven@midlink.org>
+local os   = require "os"
+local io   = require "io"
+local fs   = require "nixio.fs"
+local util = require "luci.util"
 
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at 
+local type  = type
+local pairs = pairs
+local error = error
+local table = table
 
-       http://www.apache.org/licenses/LICENSE-2.0 
+local ipkg = "opkg --force-removal-of-dependent-packages --force-overwrite"
+local icfg = "/etc/opkg.conf"
 
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
+--- LuCI OPKG call abstraction library
+module "luci.model.ipkg"
 
-]]--
-module("luci.model.ipkg", package.seeall)
-require("luci.util")
-require("luci.fs")
 
-ipkg = luci.fs.access("/bin/opkg") and "opkg" or "ipkg"
+-- Internal action function
+local function _action(cmd, ...)
+       local pkg = ""
+       for k, v in pairs({...}) do
+               pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
+       end
+
+       local c = "%s %s %s >/tmp/opkg.stdout 2>/tmp/opkg.stderr" %{ ipkg, cmd, pkg }
+       local r = os.execute(c)
+       local e = fs.readfile("/tmp/opkg.stderr")
+       local o = fs.readfile("/tmp/opkg.stdout")
+
+       fs.unlink("/tmp/opkg.stderr")
+       fs.unlink("/tmp/opkg.stdout")
+
+       return r, o or "", e or ""
+end
+
+-- Internal parser function
+local function _parselist(rawdata)
+       if type(rawdata) ~= "function" then
+               error("OPKG: Invalid rawdata given")
+       end
+
+       local data = {}
+       local c = {}
+       local l = nil
+
+       for line in rawdata do
+               if line:sub(1, 1) ~= " " then
+                       local key, val = line:match("(.-): ?(.*)%s*")
+
+                       if key and val then
+                               if key == "Package" then
+                                       c = {Package = val}
+                                       data[val] = c
+                               elseif key == "Status" then
+                                       c.Status = {}
+                                       for j in val:gmatch("([^ ]+)") do
+                                               c.Status[j] = true
+                                       end
+                               else
+                                       c[key] = val
+                               end
+                               l = key
+                       end
+               else
+                       -- Multi-line field
+                       c[l] = c[l] .. "\n" .. line
+               end
+       end
+
+       return data
+end
+
+-- Internal lookup function
+local function _lookup(act, pkg)
+       local cmd = ipkg .. " " .. act
+       if pkg then
+               cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
+       end
+
+       -- OPKG sometimes kills the whole machine because it sucks
+       -- Therefore we have to use a sucky approach too and use
+       -- tmpfiles instead of directly reading the output
+       local tmpfile = os.tmpname()
+       os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
 
--- Returns repository information
+       local data = _parselist(io.lines(tmpfile))
+       os.remove(tmpfile)
+       return data
+end
+
+
+--- Return information about installed and available packages.
+-- @param pkg Limit output to a (set of) packages
+-- @return Table containing package information
 function info(pkg)
        return _lookup("info", pkg)
 end
 
--- Returns a table with status information
+--- Return the package status of one or more packages.
+-- @param pkg Limit output to a (set of) packages
+-- @return Table containing package status information
 function status(pkg)
        return _lookup("status", pkg)
 end
 
--- Installs packages
+--- Install one or more packages.
+-- @param ... List of packages to install
+-- @return Boolean indicating the status of the action
+-- @return OPKG return code, STDOUT and STDERR
 function install(...)
        return _action("install", ...)
 end
 
--- Returns whether a package is installed
-function installed(pkg, ...)
-       local p = status(...)[pkg]
+--- Determine whether a given package is installed.
+-- @param pkg Package
+-- @return Boolean
+function installed(pkg)
+       local p = status(pkg)[pkg]
        return (p and p.Status and p.Status.installed)
 end
 
--- Removes packages
+--- Remove one or more packages.
+-- @param ... List of packages to install
+-- @return Boolean indicating the status of the action
+-- @return OPKG return code, STDOUT and STDERR
 function remove(...)
        return _action("remove", ...)
 end
 
--- Updates package lists
+--- Update package lists.
+-- @return Boolean indicating the status of the action
+-- @return OPKG return code, STDOUT and STDERR
 function update()
        return _action("update")
 end
 
--- Upgrades installed packages
+--- Upgrades all installed packages.
+-- @return Boolean indicating the status of the action
+-- @return OPKG return code, STDOUT and STDERR
 function upgrade()
        return _action("upgrade")
 end
 
+-- List helper
+function _list(action, pat, cb)
+       local fd = io.popen(ipkg .. " " .. action ..
+               (pat and (" '%s'" % pat:gsub("'", "")) or "")) -- .. " | grep -vE '^ '")
 
--- Internal action function
-function _action(cmd, ...)
-       local pkg = ""
-       arg.n = nil
-       for k, v in pairs(arg) do
-               pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
+       if fd then
+               local name, version, desc
+               while true do
+                       local line = fd:read("*l")
+                       if not line then break end
+
+                       if line:sub(1,1) ~= " " then
+                               name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
+
+                               if not name then
+                                       name, version = line:match("^(.-) %- (.+)")
+                                       desc = ""
+                               end
+
+                               cb(name, version, desc)
+
+                               name    = nil
+                               version = nil
+                               desc    = nil
+                       end
+               end
+
+               fd:close()
        end
-       
-       local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
-       local r = os.execute(c)
-       return (r == 0), r      
 end
 
--- Internal lookup function
-function _lookup(act, pkg)
-       local cmd = ipkg .. " " .. act
-       if pkg then
-               cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
-       end
-       
-       return _parselist(luci.util.exec(cmd .. " 2>/dev/null"))
+--- List all packages known to opkg.
+-- @param pat  Only find packages matching this pattern, nil lists all packages
+-- @param cb   Callback function invoked for each package, receives name, version and description as arguments
+-- @return     nothing
+function list_all(pat, cb)
+       _list("list", pat, cb)
 end
 
--- Internal parser function
-function _parselist(rawdata)   
-       if type(rawdata) ~= "string" then
-               error("IPKG: Invalid rawdata given")
-       end
-       
-       rawdata = luci.util.split(rawdata) 
-       local data = {}
-       local c = {}
-       local l = nil
-       
-       for k, line in pairs(rawdata) do
-               if line:sub(1, 1) ~= " " then
-                       local split = luci.util.split(line, ":", 1)
-                       local key = nil
-                       local val = nil
-                       
-                       if split[1] then
-                               key = luci.util.trim(split[1])
-                       end
-                       
-                       if split[2] then
-                               val = luci.util.trim(split[2])
-                       end
-                       
-                       if key and val then
-                               if key == "Package" then
-                                       c = {Package = val}
-                                       data[val] = c
-                               elseif key == "Status" then
-                                       c.Status = {}
-                                       for i, j in pairs(luci.util.split(val, " ")) do
-                                               c.Status[j] = true
-                                       end
-                               else
-                                       c[key] = val
+--- List installed packages.
+-- @param pat  Only find packages matching this pattern, nil lists all packages
+-- @param cb   Callback function invoked for each package, receives name, version and description as arguments
+-- @return     nothing
+function list_installed(pat, cb)
+       _list("list_installed", pat, cb)
+end
+
+--- Determines the overlay root used by opkg.
+-- @return             String containing the directory path of the overlay root.
+function overlay_root()
+       local od = "/"
+       local fd = io.open(icfg, "r")
+
+       if fd then
+               local ln
+
+               repeat
+                       ln = fd:read("*l")
+                       if ln and ln:match("^%s*option%s+overlay_root%s+") then
+                               od = ln:match("^%s*option%s+overlay_root%s+(%S+)")
+
+                               local s = fs.stat(od)
+                               if not s or s.type ~= "dir" then
+                                       od = "/"
                                end
-                               l = key
+
+                               break
                        end
-               else
-                       -- Multi-line field
-                       c[l] = c[l] .. "\n" .. line:sub(2)
-               end
+               until not ln
+
+               fd:close()
        end
-       
-       return data
-end
\ No newline at end of file
+
+       return od
+end