Cleanup and documentation
[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) ~= "string" then
45                 error("IPKG: Invalid rawdata given")
46         end
47         
48         rawdata = util.split(rawdata) 
49         local data = {}
50         local c = {}
51         local l = nil
52         
53         for k, line in pairs(rawdata) do
54                 if line:sub(1, 1) ~= " " then
55                         local split = util.split(line, ":", 1)
56                         local key = nil
57                         local val = nil
58                         
59                         if split[1] then
60                                 key = util.trim(split[1])
61                         end
62                         
63                         if split[2] then
64                                 val = util.trim(split[2])
65                         end
66                         
67                         if key and val then
68                                 if key == "Package" then
69                                         c = {Package = val}
70                                         data[val] = c
71                                 elseif key == "Status" then
72                                         c.Status = {}
73                                         for i, j in pairs(luci.util.split(val, " ")) do
74                                                 c.Status[j] = true
75                                         end
76                                 else
77                                         c[key] = val
78                                 end
79                                 l = key
80                         end
81                 else
82                         -- Multi-line field
83                         c[l] = c[l] .. "\n" .. line:sub(2)
84                 end
85         end
86         
87         return data
88 end
89
90 -- Internal lookup function
91 local function _lookup(act, pkg)
92         local cmd = ipkg .. " " .. act
93         if pkg then
94                 cmd = cmd .. " '" .. pkg:gsub("'", "") .. "'"
95         end
96         
97         return _parselist(util.exec(cmd .. " 2>/dev/null"))
98 end
99
100
101 --- Return information about installed and available packages.
102 -- @param pkg Limit output to a (set of) packages
103 -- @return Table containing package information
104 function info(pkg)
105         return _lookup("info", pkg)
106 end
107
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
111 function status(pkg)
112         return _lookup("status", pkg)
113 end
114
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", ...)
121 end
122
123 --- Determine whether a given package is installed.
124 -- @param pkg Package
125 -- @return Boolean
126 function installed(pkg)
127         local p = status(pkg)[pkg]
128         return (p and p.Status and p.Status.installed)
129 end
130
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
135 function remove(...)
136         return _action("remove", ...)
137 end
138
139 --- Update package lists.
140 -- @return Boolean indicating the status of the action
141 -- @return IPKG return code
142 function update()
143         return _action("update")
144 end
145
146 --- Upgrades all installed packages.
147 -- @return Boolean indicating the status of the action
148 -- @return IPKG return code
149 function upgrade()
150         return _action("upgrade")
151 end
152