607c3638ed8dae32827292b05e8571a57a8e53a5
[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 local table = table
24
25 local ipkg = "opkg"
26
27 --- LuCI IPKG/OPKG call abstraction library
28 module "luci.model.ipkg"
29
30
31 -- Internal action function
32 local function _action(cmd, ...)
33 local pkg = ""
34 for k, v in pairs({...}) 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
150
151 -- List helper
152 function _list(action, pat, cb)
153 local fd = io.popen(ipkg .. " " .. action .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
154 if fd then
155 local name, version, desc
156 while true do
157 local line = fd:read("*l")
158 if not line then break end
159
160 if line:sub(1,1) ~= " " then
161 name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
162
163 if not name then
164 name, version = line:match("^(.-) %- (.+)")
165 desc = ""
166 end
167
168 cb(name, version, desc)
169
170 name = nil
171 version = nil
172 desc = nil
173 end
174 end
175
176 fd:close()
177 end
178 end
179
180 --- List all packages known to opkg.
181 -- @param pat Only find packages matching this pattern, nil lists all packages
182 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
183 -- @return nothing
184 function list_all(pat, cb)
185 _list("list", pat, cb)
186 end
187
188 --- List installed packages.
189 -- @param pat Only find packages matching this pattern, nil lists all packages
190 -- @param cb Callback function invoked for each package, receives name, version and description as arguments
191 -- @return nothing
192 function list_installed(pat, cb)
193 _list("list_installed", pat, cb)
194 end