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 util = require "luci.util"
25 --- LuCI IPKG/OPKG call abstraction library
26 module "luci.model.ipkg"
29 -- Internal action function
30 local function _action(cmd, ...)
33 for k, v in pairs(arg) do
34 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
37 local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
38 local r = os.execute(c)
42 -- Internal parser function
43 local function _parselist(rawdata)
44 if type(rawdata) ~= "function" then
45 error("IPKG: Invalid rawdata given")
52 for line in rawdata do
53 if line:sub(1, 1) ~= " " then
54 local key, val = line:match("(.-): ?(.*)%s*")
57 if key == "Package" then
60 elseif key == "Status" then
62 for j in val:gmatch("([^ ]+)") do
72 c[l] = c[l] .. "\n" .. line
79 -- Internal lookup function
80 local function _lookup(act, pkg)
81 local cmd = ipkg .. " " .. act
83 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
86 return _parselist(util.execi(cmd .. " 2>/dev/null"))
90 --- Return information about installed and available packages.
91 -- @param pkg Limit output to a (set of) packages
92 -- @return Table containing package information
94 return _lookup("info", pkg)
97 --- Return the package status of one or more packages.
98 -- @param pkg Limit output to a (set of) packages
99 -- @return Table containing package status information
101 return _lookup("status", pkg)
104 --- Install one or more packages.
105 -- @param ... List of packages to install
106 -- @return Boolean indicating the status of the action
107 -- @return IPKG return code
108 function install(...)
109 return _action("install", ...)
112 --- Determine whether a given package is installed.
113 -- @param pkg Package
115 function installed(pkg)
116 local p = status(pkg)[pkg]
117 return (p and p.Status and p.Status.installed)
120 --- Remove one or more packages.
121 -- @param ... List of packages to install
122 -- @return Boolean indicating the status of the action
123 -- @return IPKG return code
125 return _action("remove", ...)
128 --- Update package lists.
129 -- @return Boolean indicating the status of the action
130 -- @return IPKG return code
132 return _action("update")
135 --- Upgrades all installed packages.
136 -- @return Boolean indicating the status of the action
137 -- @return IPKG return code
139 return _action("upgrade")