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) ~= "string" then
45 error("IPKG: Invalid rawdata given")
48 rawdata = util.split(rawdata)
53 for k, line in pairs(rawdata) do
54 if line:sub(1, 1) ~= " " then
55 local split = util.split(line, ":", 1)
60 key = util.trim(split[1])
64 val = util.trim(split[2])
68 if key == "Package" then
71 elseif key == "Status" then
73 for i, j in pairs(luci.util.split(val, " ")) do
83 c[l] = c[l] .. "\n" .. line:sub(2)
90 -- Internal lookup function
91 local function _lookup(act, pkg)
92 local cmd = ipkg .. " " .. act
94 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
97 return _parselist(util.exec(cmd .. " 2>/dev/null"))
101 --- Return information about installed and available packages.
102 -- @param pkg Limit output to a (set of) packages
103 -- @return Table containing package information
105 return _lookup("info", pkg)
108 --- Return the package status of one or more packages.
109 -- @param pkg Limit output to a (set of) packages
110 -- @return Table containing package status information
112 return _lookup("status", pkg)
115 --- Install one or more packages.
116 -- @param ... List of packages to install
117 -- @return Boolean indicating the status of the action
118 -- @return IPKG return code
119 function install(...)
120 return _action("install", ...)
123 --- Determine whether a given package is installed.
124 -- @param pkg Package
126 function installed(pkg)
127 local p = status(pkg)[pkg]
128 return (p and p.Status and p.Status.installed)
131 --- Remove one or more packages.
132 -- @param ... List of packages to install
133 -- @return Boolean indicating the status of the action
134 -- @return IPKG return code
136 return _action("remove", ...)
139 --- Update package lists.
140 -- @return Boolean indicating the status of the action
141 -- @return IPKG return code
143 return _action("update")
146 --- Upgrades all installed packages.
147 -- @return Boolean indicating the status of the action
148 -- @return IPKG return code
150 return _action("upgrade")