libs/ipkg: use -force-defaults to ensure non-interactive execution
[project/luci.git] / libs / ipkg / luasrc / model / ipkg.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local os = require "os"
17 local io = require "io"
18 local util = require "luci.util"
19
20 local type = type
21 local pairs = pairs
22 local error = error
23
24 local ipkg = "opkg -force-defaults"
25
26 --- LuCI IPKG/OPKG call abstraction library
27 module "luci.model.ipkg"
28
29
30 -- Internal action function
31 local function _action(cmd, ...)
32 local pkg = ""
33 arg.n = nil
34 for k, v in pairs(arg) do
35 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
36 end
37
38 local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
39 local r = os.execute(c)
40 return (r == 0), r
41 end
42
43 -- Internal parser function
44 local function _parselist(rawdata)
45 if type(rawdata) ~= "function" then
46 error("IPKG: Invalid rawdata given")
47 end
48
49 local data = {}
50 local c = {}
51 local l = nil
52
53 for line in rawdata do
54 if line:sub(1, 1) ~= " " then
55 local key, val = line:match("(.-): ?(.*)%s*")
56
57 if key and val then
58 if key == "Package" then
59 c = {Package = val}
60 data[val] = c
61 elseif key == "Status" then
62 c.Status = {}
63 for j in val:gmatch("([^ ]+)") do
64 c.Status[j] = true
65 end
66 else
67 c[key] = val
68 end
69 l = key
70 end
71 else
72 -- Multi-line field
73 c[l] = c[l] .. "\n" .. line
74 end
75 end
76
77 return data
78 end
79
80 -- Internal lookup function
81 local function _lookup(act, pkg)
82 local cmd = ipkg .. " " .. act
83 if pkg then
84 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
85 end
86
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))
92
93 local data = _parselist(io.lines(tmpfile))
94 os.remove(tmpfile)
95 return data
96 end
97
98
99 --- Return information about installed and available packages.
100 -- @param pkg Limit output to a (set of) packages
101 -- @return Table containing package information
102 function info(pkg)
103 return _lookup("info", pkg)
104 end
105
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
109 function status(pkg)
110 return _lookup("status", pkg)
111 end
112
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", ...)
119 end
120
121 --- Determine whether a given package is installed.
122 -- @param pkg Package
123 -- @return Boolean
124 function installed(pkg)
125 local p = status(pkg)[pkg]
126 return (p and p.Status and p.Status.installed)
127 end
128
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
133 function remove(...)
134 return _action("remove", ...)
135 end
136
137 --- Update package lists.
138 -- @return Boolean indicating the status of the action
139 -- @return IPKG return code
140 function update()
141 return _action("update")
142 end
143
144 --- Upgrades all installed packages.
145 -- @return Boolean indicating the status of the action
146 -- @return IPKG return code
147 function upgrade()
148 return _action("upgrade")
149 end