user3802162
user3802162

Reputation: 23

Trying to get value of PATH variable from module file while reading the module file in lua

I am trying to read value of PATH variable given in a module file while reading the module file in a lua script. I am not sure whether this can be done using some function in lua, as I am pretty new to lua.

Module file (netcdf) only a part of the module file is given below -

set application netcdf
set version 4.1.1

set machine kgb
set app_base /sw/$machine/$application/$version

module-whatis "Sets up environment to use serial netcdf"
if [ module-info mode whatis ] {
  break
}


  #vvvvv If if not a library, remove this part vvvvv
  if [ is-loaded intel ] {
    set app_build "centos6.2_intel12"
  } elseif [ is-loaded gcc ] {
    set app_build "centos6.2_gnu4.4.6"
    break
  } elseif [ is-loaded pgi ] {
    set app_build "centos6.2_pgi12.3"
    break
  } else {
    puts stderr "You must have a programming environment loaded to use this module"
    break
  }
  #^^^^^ If if not a library, remove this part ^^^^^

  # This assumes something like --prefix=$SW_BLDDIR
  set app_path $app_base/$app_build

  setenv NETCDF_DIR "$app_path"
  setenv NETCDF_INCLUDE "$app_path/include"
  setenv NETCDF_LIB "$app_path/lib"
  #setenv NETCDF_LINK "-I${FOO_INCLUDE} -L${FOO_LIB} -lfoo"

  prepend-path PATH             "$app_path/bin"
  prepend-path LD_LIBRARY_PATH  "$app_path/lib"

I am reading this file, is there any way to get all the three possible combination values of PATH that may be used irrespective of the environment user/system is having i.e.

- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_intel12/bin
- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_gnu4.4.6/bin
- PATH = /sw/kgb/netcdf/4.1.1/centos6.2_pgi12.3/bin

The code that I have written only reads the line but it is difficult to arrange the values in the variable and get the desired PATH.

Lua Code -

-- reading module file 
local mfile = v.file
local lines = lines_from(mfile)

-- print all line numbers and their contents 
for k ,v in pairs(lines)do
   print('line['..k..']',v)
end

-- see if file exists 
function file_exists(file)
   local f = io.open(file,"rb")
   if f then f:close() end
   return f~= nil
end

-- get all lines from a file, returns an empty 
-- list/table if the file does not exists
function lines_from(file)
   if not file_exists(file) then return {} end
   local lines = {} 
   for line in io.lines(file) do
     if (string.match(line, 'set') or string.match(line,'prepend'))then  
       lines[#lines+1] = line
     end
   end
   return lines
end 

Output that I get just show the lines of interest that I need, but getting all the possible value of the PATH is still far away from my reach, any help would be appreciated!

-K

Upvotes: 1

Views: 459

Answers (1)

Oliver
Oliver

Reputation: 29523

Really depends how much you know about the non-Lua file:

  1. at one end of spectrum the file never changes except for the actual values of variables (so all branching and sets and setenvs remain at same place in same order, just the string literals changes);
  2. at the other end, the file can change completely except that somewhere in the file the PATH is set.

For #1 case, you can quite easily use Lua's pattern matching capabilities to find all the sets of interest for app_build and from that you can determine the 3 values of PATH (no need to parse for the other variables since the structure doesn't change you can assume it in your Lua code logic).

For #2 case, you have two options:

  1. You build a parser for the scripting language (bash?): you could use Lua's LPEG library for that, but that is a pretty steep endeavor, unlikely feasible for you.
  2. You use Lua to copy the bash file to a new file but you append some bash commands to output PATH to stdout, then you use io.popen to run the temporary script using bash and capture its output. You then use Lua's pattern matching capabilities to find the value of PATH. Your Lua script would do this io.popen 3 times, once for each of the 3 options. This is not likely to work based on the comment in the script that the script can only work from within a programming environment.

So in practice for #2 you are likely SOL, and for #1 you are on the right track. If the latter is your case, but you are having trouble getting all variables, post comment and I will add an example to my answer.

Upvotes: 0

Related Questions