comparison share/lua/5.2/luarocks/repos.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Functions for managing the repository on disk.
3 module("luarocks.repos", package.seeall)
4
5 local fs = require("luarocks.fs")
6 local path = require("luarocks.path")
7 local cfg = require("luarocks.cfg")
8 local util = require("luarocks.util")
9 local dir = require("luarocks.dir")
10 local manif = require("luarocks.manif")
11 local deps = require("luarocks.deps")
12
13 --- Get all installed versions of a package.
14 -- @param name string: a package name.
15 -- @return table or nil: An array of strings listing installed
16 -- versions of a package, or nil if none is available.
17 local function get_installed_versions(name)
18 assert(type(name) == "string")
19
20 local dirs = fs.list_dir(path.versions_dir(name))
21 return (dirs and #dirs > 0) and dirs or nil
22 end
23
24 --- Check if a package exists in a local repository.
25 -- Version numbers are compared as exact string comparison.
26 -- @param name string: name of package
27 -- @param version string: package version in string format
28 -- @return boolean: true if a package is installed,
29 -- false otherwise.
30 function is_installed(name, version)
31 assert(type(name) == "string")
32 assert(type(version) == "string")
33
34 return fs.is_dir(path.install_dir(name, version))
35 end
36
37 local function recurse_rock_manifest_tree(file_tree, action)
38 assert(type(file_tree) == "table")
39 assert(type(action) == "function")
40 local function do_recurse_rock_manifest_tree(tree, parent_path, parent_module)
41
42 for file, sub in pairs(tree) do
43 if type(sub) == "table" then
44 local ok, err = do_recurse_rock_manifest_tree(sub, parent_path..file.."/", parent_module..file..".")
45 if not ok then return nil, err end
46 else
47 local ok, err = action(parent_path, parent_module, file)
48 if not ok then return nil, err end
49 end
50 end
51 return true
52 end
53 return do_recurse_rock_manifest_tree(file_tree, "", "")
54 end
55
56 local function store_package_data(result, name, file_tree)
57 if not file_tree then return end
58 return recurse_rock_manifest_tree(file_tree,
59 function(parent_path, parent_module, file)
60 local pathname = parent_path..file
61 result[path.path_to_module(pathname)] = pathname
62 return true
63 end
64 )
65 end
66
67 --- Obtain a list of modules within an installed package.
68 -- @param package string: The package name; for example "luasocket"
69 -- @param version string: The exact version number including revision;
70 -- for example "2.0.1-1".
71 -- @return table: A table of modules where keys are module identifiers
72 -- in "foo.bar" format and values are pathnames in architecture-dependent
73 -- "foo/bar.so" format. If no modules are found or if package or version
74 -- are invalid, an empty table is returned.
75 function package_modules(package, version)
76 assert(type(package) == "string")
77 assert(type(version) == "string")
78
79 local result = {}
80 local rock_manifest = manif.load_rock_manifest(package, version)
81 store_package_data(result, package, rock_manifest.lib)
82 store_package_data(result, package, rock_manifest.lua)
83 return result
84 end
85
86 --- Obtain a list of command-line scripts within an installed package.
87 -- @param package string: The package name; for example "luasocket"
88 -- @param version string: The exact version number including revision;
89 -- for example "2.0.1-1".
90 -- @return table: A table of items where keys are command names
91 -- as strings and values are pathnames in architecture-dependent
92 -- ".../bin/foo" format. If no modules are found or if package or version
93 -- are invalid, an empty table is returned.
94 function package_commands(package, version)
95 assert(type(package) == "string")
96 assert(type(version) == "string")
97
98 local result = {}
99 local rock_manifest = manif.load_rock_manifest(package, version)
100 store_package_data(result, package, rock_manifest.bin)
101 return result
102 end
103
104
105 --- Check if a rock contains binary executables.
106 -- @param name string: name of an installed rock
107 -- @param version string: version of an installed rock
108 -- @return boolean: returns true if rock contains platform-specific
109 -- binary executables, or false if it is a pure-Lua rock.
110 function has_binaries(name, version)
111 assert(type(name) == "string")
112 assert(type(version) == "string")
113
114 local rock_manifest = manif.load_rock_manifest(name, version)
115 if rock_manifest.bin then
116 for name, md5 in pairs(rock_manifest.bin) do
117 -- TODO verify that it is the same file. If it isn't, find the actual command.
118 if fs.is_actual_binary(dir.path(cfg.deploy_bin_dir, name)) then
119 return true
120 end
121 end
122 end
123 return false
124 end
125
126 function run_hook(rockspec, hook_name)
127 assert(type(rockspec) == "table")
128 assert(type(hook_name) == "string")
129
130 local hooks = rockspec.hooks
131 if not hooks then
132 return true
133 end
134 if not hooks.substituted_variables then
135 util.variable_substitutions(hooks, rockspec.variables)
136 hooks.substituted_variables = true
137 end
138 local hook = hooks[hook_name]
139 if hook then
140 util.printout(hook)
141 if not fs.execute(hook) then
142 return nil, "Failed running "..hook_name.." hook."
143 end
144 end
145 return true
146 end
147
148 local function install_binary(source, target)
149 assert(type(source) == "string")
150 assert(type(target) == "string")
151
152 local match = source:match("%.lua$")
153 local file, ok, err
154 if not match then
155 file = io.open(source)
156 end
157 if match or (file and file:read():match("^#!.*lua.*")) then
158 ok, err = fs.wrap_script(source, target)
159 else
160 ok, err = fs.copy_binary(source, target)
161 end
162 if file then file:close() end
163 return ok, err
164 end
165
166 local function resolve_conflict(target, deploy_dir, name, version)
167 local cname, cversion = manif.find_current_provider(target)
168 if not cname then
169 return nil, cversion
170 end
171 if name ~= cname or deps.compare_versions(version, cversion) then
172 local versioned = path.versioned_name(target, deploy_dir, cname, cversion)
173 fs.make_dir(dir.dir_name(versioned))
174 fs.move(target, versioned)
175 return target
176 else
177 return path.versioned_name(target, deploy_dir, name, version)
178 end
179 end
180
181 function should_wrap_bin_scripts(rockspec)
182 assert(type(rockspec) == "table")
183
184 if cfg.wrap_bin_scripts ~= nil then
185 return cfg.wrap_bin_scripts
186 end
187 if rockspec.deploy and rockspec.deploy.wrap_bin_scripts == false then
188 return false
189 end
190 return true
191 end
192
193 function deploy_files(name, version, wrap_bin_scripts)
194 assert(type(name) == "string")
195 assert(type(version) == "string")
196 assert(type(wrap_bin_scripts) == "boolean")
197
198 local function deploy_file_tree(file_tree, source_dir, deploy_dir, move_fn)
199 if not move_fn then
200 move_fn = fs.move
201 end
202 return recurse_rock_manifest_tree(file_tree,
203 function(parent_path, parent_module, file)
204 local source = dir.path(source_dir, parent_path, file)
205 local target = dir.path(deploy_dir, parent_path, file)
206 local ok, err
207 if fs.exists(target) then
208 local new_target, err = resolve_conflict(target, deploy_dir, name, version)
209 if err == "untracked" then
210 fs.delete(target)
211 elseif err then
212 return nil, err.." Cannot install new version."
213 else
214 target = new_target
215 end
216 end
217 fs.make_dir(dir.dir_name(target))
218 ok, err = move_fn(source, target)
219 fs.remove_dir_tree_if_empty(dir.dir_name(source))
220 if not ok then return nil, err end
221 return true
222 end
223 )
224 end
225
226 local rock_manifest = manif.load_rock_manifest(name, version)
227
228 local ok, err = true
229 if rock_manifest.bin then
230 local move_bin_fn = wrap_bin_scripts and install_binary or fs.copy_binary
231 ok, err = deploy_file_tree(rock_manifest.bin, path.bin_dir(name, version), cfg.deploy_bin_dir, move_bin_fn)
232 end
233 if ok and rock_manifest.lua then
234 ok, err = deploy_file_tree(rock_manifest.lua, path.lua_dir(name, version), cfg.deploy_lua_dir)
235 end
236 if ok and rock_manifest.lib then
237 ok, err = deploy_file_tree(rock_manifest.lib, path.lib_dir(name, version), cfg.deploy_lib_dir)
238 end
239 return ok, err
240 end
241
242 --- Delete a package from the local repository.
243 -- Version numbers are compared as exact string comparison.
244 -- @param name string: name of package
245 -- @param version string: package version in string format
246 -- @param quick boolean: do not try to fix the versioned name
247 -- of another version that provides the same module that
248 -- was deleted. This is used during 'purge', as every module
249 -- will be eventually deleted.
250 function delete_version(name, version, quick)
251 assert(type(name) == "string")
252 assert(type(version) == "string")
253
254 local function delete_deployed_file_tree(file_tree, deploy_dir)
255 return recurse_rock_manifest_tree(file_tree,
256 function(parent_path, parent_module, file)
257 local target = dir.path(deploy_dir, parent_path, file)
258 local versioned = path.versioned_name(target, deploy_dir, name, version)
259 if fs.exists(versioned) then
260 local ok = fs.delete(versioned)
261 fs.remove_dir_tree_if_empty(dir.dir_name(versioned))
262 if not ok then return nil, "Failed deleting "..versioned end
263 else
264 local ok = fs.delete(target)
265 if not quick then
266 local next_name, next_version = manif.find_next_provider(target)
267 if next_name then
268 local versioned = path.versioned_name(target, deploy_dir, next_name, next_version)
269 fs.move(versioned, target)
270 fs.remove_dir_tree_if_empty(dir.dir_name(versioned))
271 end
272 end
273 fs.remove_dir_tree_if_empty(dir.dir_name(target))
274 if not ok then return nil, "Failed deleting "..target end
275 end
276 return true
277 end
278 )
279 end
280
281 local rock_manifest = manif.load_rock_manifest(name, version)
282 if not rock_manifest then
283 return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?"
284 end
285
286 local ok, err = true
287 if rock_manifest.bin then
288 ok, err = delete_deployed_file_tree(rock_manifest.bin, cfg.deploy_bin_dir)
289 end
290 if ok and rock_manifest.lua then
291 ok, err = delete_deployed_file_tree(rock_manifest.lua, cfg.deploy_lua_dir)
292 end
293 if ok and rock_manifest.lib then
294 ok, err = delete_deployed_file_tree(rock_manifest.lib, cfg.deploy_lib_dir)
295 end
296 if err then return nil, err end
297
298 fs.delete(path.install_dir(name, version))
299 if not get_installed_versions(name) then
300 fs.delete(dir.path(cfg.rocks_dir, name))
301 end
302 return true
303 end