2 LuCI - Lua Configuration Interface
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 local os = require "os"
17 local io = require "io"
18 local util = require "luci.util"
24 local ipkg = "opkg -force-defaults"
26 --- LuCI IPKG/OPKG call abstraction library
27 module "luci.model.ipkg"
30 -- Internal action function
31 local function _action(cmd, ...)
34 for k, v in pairs(arg) do
35 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
38 local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
39 local r = os.execute(c)
43 -- Internal parser function
44 local function _parselist(rawdata)
45 if type(rawdata) ~= "function" then
46 error("IPKG: Invalid rawdata given")
53 for line in rawdata do
54 if line:sub(1, 1) ~= " " then
55 local key, val = line:match("(.-): ?(.*)%s*")
58 if key == "Package" then
61 elseif key == "Status" then
63 for j in val:gmatch("([^ ]+)") do
73 c[l] = c[l] .. "\n" .. line
80 -- Internal lookup function
81 local function _lookup(act, pkg)
82 local cmd = ipkg .. " " .. act
84 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
87 -- IPKG sometimes kills the whole machine because it sucks
88 -- Therefore we have to use a sucky approach too and use
89 -- tmpfiles instead of directly reading the output
90 local tmpfile = os.tmpname()
91 os.execute(cmd .. (" >%s 2>/dev/null" % tmpfile))
93 local data = _parselist(io.lines(tmpfile))
99 --- Return information about installed and available packages.
100 -- @param pkg Limit output to a (set of) packages
101 -- @return Table containing package information
103 return _lookup("info", pkg)
106 --- Return the package status of one or more packages.
107 -- @param pkg Limit output to a (set of) packages
108 -- @return Table containing package status information
110 return _lookup("status", pkg)
113 --- Install one or more packages.
114 -- @param ... List of packages to install
115 -- @return Boolean indicating the status of the action
116 -- @return IPKG return code
117 function install(...)
118 return _action("install", ...)
121 --- Determine whether a given package is installed.
122 -- @param pkg Package
124 function installed(pkg)
125 local p = status(pkg)[pkg]
126 return (p and p.Status and p.Status.installed)
129 --- Remove one or more packages.
130 -- @param ... List of packages to install
131 -- @return Boolean indicating the status of the action
132 -- @return IPKG return code
134 return _action("remove", ...)
137 --- Update package lists.
138 -- @return Boolean indicating the status of the action
139 -- @return IPKG return code
141 return _action("update")
144 --- Upgrades all installed packages.
145 -- @return Boolean indicating the status of the action
146 -- @return IPKG return code
148 return _action("upgrade")