Mercurial > repo
comparison share/lua/5.2/luarocks/path.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 --- LuaRocks-specific path handling functions. | |
3 -- All paths are configured in this module, making it a single | |
4 -- point where the layout of the local installation is defined in LuaRocks. | |
5 module("luarocks.path", package.seeall) | |
6 | |
7 local dir = require("luarocks.dir") | |
8 local cfg = require("luarocks.cfg") | |
9 local util = require("luarocks.util") | |
10 local deps = require("luarocks.deps") | |
11 | |
12 help_summary = "Return the currently configured package path." | |
13 help_arguments = "" | |
14 help = [[ | |
15 Returns the package path currently configured for this installation | |
16 of LuaRocks, formatted as shell commands to update LUA_PATH and | |
17 LUA_CPATH. (On Unix systems, you may run: eval `luarocks path`) | |
18 ]] | |
19 | |
20 --- Infer rockspec filename from a rock filename. | |
21 -- @param rock_name string: Pathname of a rock file. | |
22 -- @return string: Filename of the rockspec, without path. | |
23 function rockspec_name_from_rock(rock_name) | |
24 assert(type(rock_name) == "string") | |
25 local base_name = dir.base_name(rock_name) | |
26 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | |
27 end | |
28 | |
29 function rocks_dir(tree) | |
30 if type(tree) == "string" then | |
31 return dir.path(tree, "lib", "luarocks", "rocks") | |
32 else | |
33 assert(type(tree) == "table") | |
34 return tree.rocks_dir or dir.path(tree.root, "lib", "luarocks", "rocks") | |
35 end | |
36 end | |
37 | |
38 function root_dir(rocks_dir) | |
39 assert(type(rocks_dir) == "string") | |
40 | |
41 local suffix = dir.path("lib", "luarocks") | |
42 return rocks_dir:match("(.*)" .. suffix .. ".*$") | |
43 end | |
44 | |
45 function deploy_bin_dir(tree) | |
46 if type(tree) == "string" then | |
47 return dir.path(tree, "bin") | |
48 else | |
49 assert(type(tree) == "table") | |
50 return tree.bin_dir or dir.path(tree.root, "bin") | |
51 end | |
52 end | |
53 | |
54 function deploy_lua_dir(tree) | |
55 if type(tree) == "string" then | |
56 return dir.path(tree, cfg.lua_modules_path) | |
57 else | |
58 assert(type(tree) == "table") | |
59 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) | |
60 end | |
61 end | |
62 | |
63 function deploy_lib_dir(tree) | |
64 if type(tree) == "string" then | |
65 return dir.path(tree, cfg.lib_modules_path) | |
66 else | |
67 assert(type(tree) == "table") | |
68 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) | |
69 end | |
70 end | |
71 | |
72 function manifest_file(tree) | |
73 if type(tree) == "string" then | |
74 return dir.path(tree, "lib", "luarocks", "rocks", "manifest") | |
75 else | |
76 assert(type(tree) == "table") | |
77 return (tree.rocks_dir and dir.path(tree.rocks_dir, "manifest")) or dir.path(tree.root, "lib", "luarocks", "rocks", "manifest") | |
78 end | |
79 end | |
80 | |
81 --- Get the directory for all versions of a package in a tree. | |
82 -- @param name string: The package name. | |
83 -- @return string: The resulting path -- does not guarantee that | |
84 -- @param tree string or nil: If given, specifies the local tree to use. | |
85 -- the package (and by extension, the path) exists. | |
86 function versions_dir(name, tree) | |
87 assert(type(name) == "string") | |
88 tree = tree or cfg.root_dir | |
89 return dir.path(rocks_dir(tree), name) | |
90 end | |
91 | |
92 --- Get the local installation directory (prefix) for a package. | |
93 -- @param name string: The package name. | |
94 -- @param version string: The package version. | |
95 -- @param tree string or nil: If given, specifies the local tree to use. | |
96 -- @return string: The resulting path -- does not guarantee that | |
97 -- the package (and by extension, the path) exists. | |
98 function install_dir(name, version, tree) | |
99 assert(type(name) == "string") | |
100 assert(type(version) == "string") | |
101 tree = tree or cfg.root_dir | |
102 return dir.path(rocks_dir(tree), name, version) | |
103 end | |
104 | |
105 --- Get the local filename of the rockspec of an installed rock. | |
106 -- @param name string: The package name. | |
107 -- @param version string: The package version. | |
108 -- @param tree string or nil: If given, specifies the local tree to use. | |
109 -- @return string: The resulting path -- does not guarantee that | |
110 -- the package (and by extension, the file) exists. | |
111 function rockspec_file(name, version, tree) | |
112 assert(type(name) == "string") | |
113 assert(type(version) == "string") | |
114 tree = tree or cfg.root_dir | |
115 return dir.path(rocks_dir(tree), name, version, name.."-"..version..".rockspec") | |
116 end | |
117 | |
118 --- Get the local filename of the rock_manifest file of an installed rock. | |
119 -- @param name string: The package name. | |
120 -- @param version string: The package version. | |
121 -- @param tree string or nil: If given, specifies the local tree to use. | |
122 -- @return string: The resulting path -- does not guarantee that | |
123 -- the package (and by extension, the file) exists. | |
124 function rock_manifest_file(name, version, tree) | |
125 assert(type(name) == "string") | |
126 assert(type(version) == "string") | |
127 tree = tree or cfg.root_dir | |
128 return dir.path(rocks_dir(tree), name, version, "rock_manifest") | |
129 end | |
130 | |
131 --- Get the local installation directory for C libraries of a package. | |
132 -- @param name string: The package name. | |
133 -- @param version string: The package version. | |
134 -- @param tree string or nil: If given, specifies the local tree to use. | |
135 -- @return string: The resulting path -- does not guarantee that | |
136 -- the package (and by extension, the path) exists. | |
137 function lib_dir(name, version, tree) | |
138 assert(type(name) == "string") | |
139 assert(type(version) == "string") | |
140 tree = tree or cfg.root_dir | |
141 return dir.path(rocks_dir(tree), name, version, "lib") | |
142 end | |
143 | |
144 --- Get the local installation directory for Lua modules of a package. | |
145 -- @param name string: The package name. | |
146 -- @param version string: The package version. | |
147 -- @param tree string or nil: If given, specifies the local tree to use. | |
148 -- @return string: The resulting path -- does not guarantee that | |
149 -- the package (and by extension, the path) exists. | |
150 function lua_dir(name, version, tree) | |
151 assert(type(name) == "string") | |
152 assert(type(version) == "string") | |
153 tree = tree or cfg.root_dir | |
154 return dir.path(rocks_dir(tree), name, version, "lua") | |
155 end | |
156 | |
157 --- Get the local installation directory for documentation of a package. | |
158 -- @param name string: The package name. | |
159 -- @param version string: The package version. | |
160 -- @param tree string or nil: If given, specifies the local tree to use. | |
161 -- @return string: The resulting path -- does not guarantee that | |
162 -- the package (and by extension, the path) exists. | |
163 function doc_dir(name, version, tree) | |
164 assert(type(name) == "string") | |
165 assert(type(version) == "string") | |
166 tree = tree or cfg.root_dir | |
167 return dir.path(rocks_dir(tree), name, version, "doc") | |
168 end | |
169 | |
170 --- Get the local installation directory for configuration files of a package. | |
171 -- @param name string: The package name. | |
172 -- @param version string: The package version. | |
173 -- @param tree string or nil: If given, specifies the local tree to use. | |
174 -- @return string: The resulting path -- does not guarantee that | |
175 -- the package (and by extension, the path) exists. | |
176 function conf_dir(name, version, tree) | |
177 assert(type(name) == "string") | |
178 assert(type(version) == "string") | |
179 tree = tree or cfg.root_dir | |
180 return dir.path(rocks_dir(tree), name, version, "conf") | |
181 end | |
182 | |
183 --- Get the local installation directory for command-line scripts | |
184 -- of a package. | |
185 -- @param name string: The package name. | |
186 -- @param version string: The package version. | |
187 -- @param tree string or nil: If given, specifies the local tree to use. | |
188 -- @return string: The resulting path -- does not guarantee that | |
189 -- the package (and by extension, the path) exists. | |
190 function bin_dir(name, version, tree) | |
191 assert(type(name) == "string") | |
192 assert(type(version) == "string") | |
193 tree = tree or cfg.root_dir | |
194 return dir.path(rocks_dir(tree), name, version, "bin") | |
195 end | |
196 | |
197 --- Extract name, version and arch of a rock filename, | |
198 -- or name, version and "rockspec" from a rockspec name. | |
199 -- @param file_name string: pathname of a rock or rockspec | |
200 -- @return (string, string, string) or nil: name, version and arch | |
201 -- or nil if name could not be parsed | |
202 function parse_name(file_name) | |
203 assert(type(file_name) == "string") | |
204 if file_name:match("%.rock$") then | |
205 return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") | |
206 else | |
207 return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") | |
208 end | |
209 end | |
210 | |
211 --- Make a rockspec or rock URL. | |
212 -- @param pathname string: Base URL or pathname. | |
213 -- @param name string: Package name. | |
214 -- @param version string: Package version. | |
215 -- @param arch string: Architecture identifier, or "rockspec" or "installed". | |
216 -- @return string: A URL or pathname following LuaRocks naming conventions. | |
217 function make_url(pathname, name, version, arch) | |
218 assert(type(pathname) == "string") | |
219 assert(type(name) == "string") | |
220 assert(type(version) == "string") | |
221 assert(type(arch) == "string") | |
222 | |
223 local filename = name.."-"..version | |
224 if arch == "installed" then | |
225 filename = dir.path(name, version, filename..".rockspec") | |
226 elseif arch == "rockspec" then | |
227 filename = filename..".rockspec" | |
228 else | |
229 filename = filename.."."..arch..".rock" | |
230 end | |
231 return dir.path(pathname, filename) | |
232 end | |
233 | |
234 --- Convert a pathname to a module identifier. | |
235 -- In Unix, for example, a path "foo/bar/baz.lua" is converted to | |
236 -- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo". | |
237 -- @param file string: Pathname of module | |
238 -- @return string: The module identifier, or nil if given path is | |
239 -- not a conformant module path (the function does not check if the | |
240 -- path actually exists). | |
241 function path_to_module(file) | |
242 assert(type(file) == "string") | |
243 | |
244 local name = file:match("(.*)%."..cfg.lua_extension.."$") | |
245 if name then | |
246 name = name:gsub(dir.separator, ".") | |
247 local init = name:match("(.*)%.init$") | |
248 if init then | |
249 name = init | |
250 end | |
251 else | |
252 name = file:match("(.*)%."..cfg.lib_extension.."$") | |
253 if name then | |
254 name = name:gsub(dir.separator, ".") | |
255 end | |
256 end | |
257 if not name then name = file end | |
258 name = name:gsub("^%.+", ""):gsub("%.+$", "") | |
259 return name | |
260 end | |
261 | |
262 --- Obtain the directory name where a module should be stored. | |
263 -- For example, on Unix, "foo.bar.baz" will return "foo/bar". | |
264 -- @param mod string: A module name in Lua dot-separated format. | |
265 -- @return string: A directory name using the platform's separator. | |
266 function module_to_path(mod) | |
267 assert(type(mod) == "string") | |
268 return (mod:gsub("[^.]*$", ""):gsub("%.", dir.separator)) | |
269 end | |
270 | |
271 --- Set up path-related variables for a given rock. | |
272 -- Create a "variables" table in the rockspec table, containing | |
273 -- adjusted variables according to the configuration file. | |
274 -- @param rockspec table: The rockspec table. | |
275 function configure_paths(rockspec) | |
276 assert(type(rockspec) == "table") | |
277 local vars = {} | |
278 for k,v in pairs(cfg.variables) do | |
279 vars[k] = v | |
280 end | |
281 local name, version = rockspec.name, rockspec.version | |
282 vars.PREFIX = install_dir(name, version) | |
283 vars.LUADIR = lua_dir(name, version) | |
284 vars.LIBDIR = lib_dir(name, version) | |
285 vars.CONFDIR = conf_dir(name, version) | |
286 vars.BINDIR = bin_dir(name, version) | |
287 vars.DOCDIR = doc_dir(name, version) | |
288 rockspec.variables = vars | |
289 end | |
290 | |
291 function versioned_name(file, prefix, name, version) | |
292 assert(type(file) == "string") | |
293 assert(type(name) == "string") | |
294 assert(type(version) == "string") | |
295 | |
296 local rest = file:sub(#prefix+1):gsub("^/*", "") | |
297 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_") | |
298 return dir.path(prefix, name_version.."-"..rest) | |
299 end | |
300 | |
301 function use_tree(tree) | |
302 cfg.root_dir = tree | |
303 cfg.rocks_dir = rocks_dir(tree) | |
304 cfg.deploy_bin_dir = deploy_bin_dir(tree) | |
305 cfg.deploy_lua_dir = deploy_lua_dir(tree) | |
306 cfg.deploy_lib_dir = deploy_lib_dir(tree) | |
307 end | |
308 | |
309 function map_trees(deps_mode, fn, ...) | |
310 local result = {} | |
311 if deps_mode == "one" then | |
312 table.insert(result, (fn(cfg.root_dir, ...)) or 0) | |
313 elseif deps_mode == "all" or deps_mode == "order" then | |
314 local use = false | |
315 if deps_mode == "all" then | |
316 use = true | |
317 end | |
318 for _, tree in ipairs(cfg.rocks_trees) do | |
319 if dir.normalize(tree) == dir.normalize(cfg.root_dir) then | |
320 use = true | |
321 end | |
322 if use then | |
323 table.insert(result, (fn(tree, ...)) or 0) | |
324 end | |
325 end | |
326 end | |
327 return result | |
328 end | |
329 | |
330 --- Return the pathname of the file that would be loaded for a module, indexed. | |
331 -- @param module_name string: module name (eg. "socket.core") | |
332 -- @param name string: name of the package (eg. "luasocket") | |
333 -- @param version string: version number (eg. "2.0.2-1") | |
334 -- @param tree string: repository path (eg. "/usr/local") | |
335 -- @param i number: the index, 1 if version is the current default, > 1 otherwise. | |
336 -- This is done this way for use by select_module in luarocks.loader. | |
337 -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") | |
338 function which_i(module_name, name, version, tree, i) | |
339 local deploy_dir | |
340 if module_name:match("%.lua$") then | |
341 deploy_dir = deploy_lua_dir(tree) | |
342 module_name = dir.path(deploy_dir, module_name) | |
343 else | |
344 deploy_dir = deploy_lib_dir(tree) | |
345 module_name = dir.path(deploy_dir, module_name) | |
346 end | |
347 if i > 1 then | |
348 module_name = versioned_name(module_name, deploy_dir, name, version) | |
349 end | |
350 return module_name | |
351 end | |
352 | |
353 --- Return the pathname of the file that would be loaded for a module, | |
354 -- returning the versioned pathname if given version is not the default version | |
355 -- in the given manifest. | |
356 -- @param module_name string: module name (eg. "socket.core") | |
357 -- @param name string: name of the package (eg. "luasocket") | |
358 -- @param version string: version number (eg. "2.0.2-1") | |
359 -- @param tree string: repository path (eg. "/usr/local") | |
360 -- @param manifest table: the manifest table for the tree. | |
361 -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") | |
362 function which(module_name, filename, name, version, tree, manifest) | |
363 local versions = manifest.modules[module_name] | |
364 assert(versions) | |
365 for i, name_version in ipairs(versions) do | |
366 if name_version == name.."/"..version then | |
367 return which_i(filename, name, version, tree, i):gsub("//", "/") | |
368 end | |
369 end | |
370 assert(false) | |
371 end | |
372 | |
373 --- Driver function for "path" command. | |
374 -- @return boolean This function always succeeds. | |
375 function run(...) | |
376 local flags = util.parse_flags(...) | |
377 local deps_mode = deps.get_deps_mode(flags) | |
378 | |
379 util.printout(cfg.export_lua_path:format(util.remove_path_dupes(package.path, ';'))) | |
380 util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(package.cpath, ';'))) | |
381 if flags["bin"] then | |
382 local bin_dirs = map_trees(deps_mode, deploy_bin_dir) | |
383 table.insert(bin_dirs, 1, os.getenv("PATH")) | |
384 util.printout(cfg.export_path:format(util.remove_path_dupes(table.concat(bin_dirs, cfg.export_path_separator), cfg.export_path_separator))) | |
385 end | |
386 return true | |
387 end | |
388 |