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 Dirk Laurie once stated:
> Op Vr. 12 Apr. 2019 om 17:27 het Luiz Henrique de Figueiredo
> <lhf@tecgraf.puc-rio.br> geskryf:
> >
> > > So why aren't the paths from where the modules are loaded, simply kept in a table mapped to the module name?
> >
> > How would that help scripts that need to run in several environments?
> > The paths are almost certainly not portable.
> 
> In my particular case, knowing whether the found file is Lua code is
> already enough.
> 
> If package.found[modname] contains the result stored in *name after
>  findloader(L, name);
> (line 606, loadlib.c)
> then my application could issue a message (and/or write it to a log file) saying
> 
> Module "complex" found as /usr/local/share/lua/5.3/complex.lua

  I have a script (attached) that when run from the command line will tell
you where a module is located:

[spc]lucy:~>luawhich io
package.loaded["io"]
[spc]lucy:~>luawhich org.conman.syslog
/home/spc/.luarocks/lib/lua/5.3/org/conman/syslog.so
/usr/local/lib/lua/5.3/org/conman/syslog.so
[spc]lucy:~>

  It even works to a degree with my custom Lua interpreter at work (which
contains all the modules we need in the executable):

[spc]dynamic-147:~>kslua luawhich org.conman.syslog
package.preload["org.conman.syslog"]
/usr/local/lib/lua/5.1/org/conman/syslog.so
[spc]dynamic-147:~>kslua luawhich sim
package.preload["sim"]
[spc]dynamic-147:~>

  Here we can see org.conman.syslog is found in package.preloaded[] (since
it's a) in my kslua program [1] and b) written in C).  I also include a
bunch of modules written in Lua but because of the whay they work and how
they're processed (they're comporessed [2] in the executable), I had to add
a searcher (loader in Lua 5.1)---this isn't known by my program though:

[spc]dynamic-147:~>kslua luawhich org.conman.date
/usr/local/share/lua/5.1/org/conman/date.lua
[spc]dynamic-147:~>

  This script could possibly be adapted as a module.

  -spc (It's an idea ... )

[1]	Kitchen Sink Lua

[2]	Using libz.  There's a few modules that are quite large.
#!/usr/bin/env lua
-- ****************************************************************************
--
-- Copyright (C) 2012 by Sean Conner
--
-- This program is free software: you can redistribute it and/or modify it
-- under the terms of the GNU General Public License as published by the
-- Free Software Foundation, either version 3 of the License, or (at your
-- option) any later version.
--
-- This program is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
-- Public License for more details.
--
-- You should have received a copy of the GNU General Public License along
-- with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- ****************************************************************************

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 .. "]+"

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)