lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Hisham once stated:
>
> Of course one can answer "just be careful about what you install", or
> "there shouldn't be any module name conflicts", but since we don't
> have a canonical namespace hierarchy for modules, conflicts do happen
> (see the various require("json") modules out there for example) and I
> believe any procedures to help detect and handle/correct them are
> welcome. I've now come to believe that avoiding init.lua is one such
> procedure.

  This prompted me to write the following script (included below).  MIT
licence, and you use it like:

GenericPrompt% luawhich math io string coroutine gd cURL org.conman.table
package.loaded["math"]
package.loaded["io"]
package.loaded["string"]
./string.lua
package.loaded["coroutine"]
/usr/local/lib/lua/5.1/gd.so
/usr/local/lib/lua/5.1/cURL.so
package.loaded["org.conman.table"]
/usr/local/share/lua/5.1/org/conman/table.lua
GenericPrompt%

  Here, you can see that the internal string module is used, but there does
exist a "string.lua" in the current directory.  You can also see that
"org.conman.table" is loaded (don't worry---the script doesn't use that;
it's my environment that loads that particular module) but is also located
at "/usr/local/share/lua/5.1/org/conman/table.lua".  

  The code should work unmodified under Windows as well.  It uses only the
standard Lua modules and nothing is hardcoded (except the first line, which
makes it work under Unix, otherwise it's ignored).

  -spc

#!/usr/bin/env lua 
--
****************************************************************************
--
-- Copyright (C) 2012 by Sean Conner.
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to permit
-- persons to whom the Software is furnished to do so, subject to the   
-- following conditions:
--
-- The above copyright notice and this permission notice shall be included   
-- in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS   
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
-- OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- 
--
****************************************************************************

local package = require "package"
local string  = require "string"
local io      = require "io"
local os      = require "os"

local DIR  = package.config:sub(1,1)
local SEP  = package.config:sub(3,3)
local MARK = package.config:sub(5,5)

local SEGPATTERN = "([^" .. SEP .. "]*)%" .. SEP .. "*"

local function print_modules(module,path)
  local module = module:gsub("%.",DIR)
  for segment in path:gmatch(SEGPATTERN) do
    local file = segment:gsub(MARK,module)
    local f    = io.open(file,"r")
    if f then
      io.stdout:write(file,"\n")
      f:close()
    end
  end
end

if #arg == 0 then
  io.stderr:write("usage: ",arg[0]," module ... \n")
  os.exit(1)
end

for i = 1 , #arg do
  if package.loaded[arg[i]] then
    io.stdout:write(string.format("package.loaded[%q]\n",arg[i]))
  end

  if package.preload[arg[i]] then
    io.stdout:write(string.format("package.preload[%q]\n",arg[i]))
  end

  print_modules(arg[i],package.path) 
  print_modules(arg[i],package.cpath)
end

os.exit(0)