Optimized IPKG info parser
[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 util = require "luci.util"
18
19 local type  = type
20 local pairs = pairs
21 local error = error
22
23 local ipkg = "opkg"
24
25 --- LuCI IPKG/OPKG call abstraction library
26 module "luci.model.ipkg"
27
28
29 -- Internal action function
30 local function _action(cmd, ...)
31         local pkg = ""
32         arg.n = nil
33         for k, v in pairs(arg) do
34                 pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
35         end
36         
37         local c = ipkg.." "..cmd.." "..pkg.." >/dev/null 2>&1"
38         local r = os.execute(c)
39         return (r == 0), r      
40 end
41
42 -- Internal parser function
43 local function _parselist(rawdata)      
44         if type(rawdata) ~= "function" then
45                 error("IPKG: Invalid rawdata given")
46         end
47         
48         local data = {}
49         local c = {}
50         local l = nil
51         
52         for line in rawdata do
53                 if line:sub(1, 1) ~= " " then
54                         local key, val = line:match("(.-): ?(.*)%s*")
55                         
56                         if key and val then
57                                 if key == "Package" then
58                                         c = {Package = val}
59                                         data[val] = c
60                                 elseif key == "Status" then
61                                         c.Status = {}
62                                         for j in val:gmatch("([^ ]+)") do
63                                                 c.Status[j] = true
64                                         end
65                                 else
66                                         c[key] = val
67                                 end
68                                 l = key
69                         end
70                 else
71                         -- Multi-line field
72                         c[l] = c[l] .. "\n" .. line
73                 end
74         end
75         
76         return data
77 end
78
79 -- Internal lookup function
80 local function _lookup(act, pkg)
81         local cmd = ipkg .. " " .. act
82         if pkg then
83                 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
84         end
85         
86         return _parselist(util.execi(cmd .. " 2>/dev/null"))
87 end
88
89
90 --- Return information about installed and available packages.
91 -- @param pkg Limit output to a (set of) packages
92 -- @return Table containing package information
93 function info(pkg)
94         return _lookup("info", pkg)
95 end
96
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
100 function status(pkg)
101         return _lookup("status", pkg)
102 end
103
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", ...)
110 end
111
112 --- Determine whether a given package is installed.
113 -- @param pkg Package
114 -- @return Boolean
115 function installed(pkg)
116         local p = status(pkg)[pkg]
117         return (p and p.Status and p.Status.installed)
118 end
119
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
124 function remove(...)
125         return _action("remove", ...)
126 end
127
128 --- Update package lists.
129 -- @return Boolean indicating the status of the action
130 -- @return IPKG return code
131 function update()
132         return _action("update")
133 end
134
135 --- Upgrades all installed packages.
136 -- @return Boolean indicating the status of the action
137 -- @return IPKG return code
138 function upgrade()
139         return _action("upgrade")
140 end
141