Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Hi,

Is there any robust and convenient methods for me to obtain the lua
script's (absolute/real) dirname and file name from within the script
itself?

Regards
--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Daniel Roßberg
"debug.getinfo(1, "S").source" is probably what you are looking for.

The robustness depends on the environment. You don't even need a file system for running Lua.

Regards,
    Daniel

Am Di., 21. Jan. 2020 um 08:20 Uhr schrieb Hongyi Zhao <[hidden email]>:
Hi,

Is there any robust and convenient methods for me to obtain the lua
script's (absolute/real) dirname and file name from within the script
itself?

Regards
--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Daniel Roßberg <[hidden email]> 于2020年1月21日周二 下午8:03写道:
>
> "debug.getinfo(1, "S").source" is probably what you are looking for.
>
> The robustness depends on the environment. You don't even need a file system for running Lua.

What do mean by saying that ``You don't even need a file system for
running Lua.''?  Could you please give a more concrete example for
this case?

>
> Regards,
>     Daniel
>
> Am Di., 21. Jan. 2020 um 08:20 Uhr schrieb Hongyi Zhao <[hidden email]>:
>>
>> Hi,
>>
>> Is there any robust and convenient methods for me to obtain the lua
>> script's (absolute/real) dirname and file name from within the script
>> itself?
>>
>> Regards
>> --
>> Hongsheng Zhao <[hidden email]>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Javier Guerra Giraldez
On Tue, 21 Jan 2020 at 10:06, Hongyi Zhao <[hidden email]> wrote:
> What do mean by saying that ``You don't even need a file system for
> running Lua.''?  Could you please give a more concrete example for
> this case?

https://www.google.com/search?q=lua+esp8266

a very popular chip running Lua without a filesystem


--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

nobody
In reply to this post by Hongyi Zhao
On 21/01/2020 16.05, Hongyi Zhao wrote:
> What do mean by saying that ``You don't even need a file system for
> running Lua.''?  Could you please give a more concrete example for
> this case?

You can add loader functions to `package.preload`.  On a match,
`require` will just get & run that function instead of searching the
file system.  (So if you know what modules your code needs, you can
preload all of them.  And then require doesn't need to hit the file system.)

(IIRC, for C libraries, you'd add `package.loaded["foo"] = luaopen_foo`
and for Lua files, you can just say `package.loaded["foo"] =
loadfile("/path/to/foo.lua")`, or alternatively `load [[(contents of
that file)]]`. From C, you'd put the file's contents in a string /
header so that it's linked into the program and then use lua_loadstring.)

When creating a Lua state from your program, you just do the preload
stuff before running code in the state, and then your code can still
look like normal Lua code that's fetching stuff from the file system
with require, only it doesn't involve the file system.  (And then you
can shim / remove the actual file system access functions and it still
works when there's no file system / maybe even no operating system.)

-- nobody

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

aryajur
In reply to this post by Hongyi Zhao
When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:

for k,v in pairs(arg) do
print(k,v)
end

When I run it like:
D:\>lua d:\commandArgs.lua
0       d:\commandArgs.lua
-1      lua

When I do:
D:\>lua commandArgs.lua
0       commandArgs.lua
-1      lua

This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:

cmd = os.execute("pwd") and "pwd" or "cd"
f = io.popen(cmd,"r")
path = f:read("*a")
f:close()

Milind


On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <[hidden email]> wrote:
Hi,

Is there any robust and convenient methods for me to obtain the lua
script's (absolute/real) dirname and file name from within the script
itself?

Regards
--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Milind Gupta <[hidden email]> 于2020年1月22日周三 上午4:08写道:

>
> When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:
>
> for k,v in pairs(arg) do
> print(k,v)
> end
>
> When I run it like:
> D:\>lua d:\commandArgs.lua
> 0       d:\commandArgs.lua
> -1      lua
>
> When I do:
> D:\>lua commandArgs.lua
> 0       commandArgs.lua
> -1      lua
>
> This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:
>
> cmd = os.execute("pwd") and "pwd" or "cd"
> f = io.popen(cmd,"r")
> path = f:read("*a")
> f:close()

Thanks a lot, it will do the trick.  But I've another question on the
above code, see me following testings:

Run the following script under terminal:

obtain-script-path.lua
-------
cmd = os.execute("pwd") and "pwd" or "cd"
f = io.popen(cmd,"r")
path = f:read("*a")
f:close()

print(path)
------

Will give me the following output:

$ lua obtain-script-path.lua
/home/werner/Public/Lmod
/home/werner/Public/Lmod

As you can see, the script print the path twice, but I only used one
print command.  How to disable the other one output line?

Regards



>
> Milind
>
>
> On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <[hidden email]> wrote:
>>
>> Hi,
>>
>> Is there any robust and convenient methods for me to obtain the lua
>> script's (absolute/real) dirname and file name from within the script
>> itself?
>>
>> Regards
>> --
>> Hongsheng Zhao <[hidden email]>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Sean Conner
It was thus said that the Great Hongyi Zhao once stated:
>
> Run the following script under terminal:
>
> obtain-script-path.lua
> -------
> cmd = os.execute("pwd") and "pwd" or "cd"
        ^^^^^^^^^^^^^^^^

This is excuting the command "pwd", and the output will go to stdout.

> f = io.popen(cmd,"r")
      ^^^^^^^^^^^^^^^^

This is also executing the command "pwd", only the output will be piped to
Lua.

> path = f:read("*a")
> f:close()
>
> print(path)
> ------
>
> Will give me the following output:
>
> $ lua obtain-script-path.lua
> /home/werner/Public/Lmod
> /home/werner/Public/Lmod
>
> As you can see, the script print the path twice, but I only used one
> print command.  How to disable the other one output line?

  You can either just set cwd to "pwd", or

        os.execute("pwd >/dev/null") -- adjust according to OS

But that sill executes the command twice.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Sean Conner <[hidden email]> 于2020年1月22日周三 上午10:54写道:

>
> It was thus said that the Great Hongyi Zhao once stated:
> >
> > Run the following script under terminal:
> >
> > obtain-script-path.lua
> > -------
> > cmd = os.execute("pwd") and "pwd" or "cd"
>         ^^^^^^^^^^^^^^^^
>
> This is excuting the command "pwd", and the output will go to stdout.
>
> > f = io.popen(cmd,"r")
>       ^^^^^^^^^^^^^^^^
>
> This is also executing the command "pwd", only the output will be piped to
> Lua.
>
> > path = f:read("*a")
> > f:close()
> >
> > print(path)
> > ------
> >
> > Will give me the following output:
> >
> > $ lua obtain-script-path.lua
> > /home/werner/Public/Lmod
> > /home/werner/Public/Lmod
> >
> > As you can see, the script print the path twice, but I only used one
> > print command.  How to disable the other one output line?
>
>   You can either just set cwd to "pwd", or
                                         ^^^^^
   Do you mean `cmd' here?

>
>         os.execute("pwd >/dev/null") -- adjust according to OS
>
> But that sill executes the command twice.

If so, why you still suggest this method?

Regards
>
>   -spc
>
>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Sean Conner
It was thus said that the Great Hongyi Zhao once stated:
> Sean Conner <[hidden email]> 于2020年1月22日周三 上午10:54写道:
> >
> >   You can either just set cwd to "pwd", or
>                                          ^^^^^
>    Do you mean `cmd' here?

  Yes, sorry about the typo.

> >
> >         os.execute("pwd >/dev/null") -- adjust according to OS
> >
> > But that sill executes the command twice.
>
> If so, why you still suggest this method?

  I made the assumption that you were doing os.execute("pwd") to see if the
program exists and can be executed.  

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
In reply to this post by aryajur
Milind Gupta <[hidden email]> 于2020年1月22日周三 上午4:08写道:

>
> When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:
>
> for k,v in pairs(arg) do
> print(k,v)
> end
>
> When I run it like:
> D:\>lua d:\commandArgs.lua
> 0       d:\commandArgs.lua
> -1      lua
>
> When I do:
> D:\>lua commandArgs.lua
> 0       commandArgs.lua
> -1      lua
>
> This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:
>
> cmd = os.execute("pwd") and "pwd" or "cd"

The issue with method is:

`pwd' will give the work dir which maybe or may not the dir the lua
script resides in.

But I want to obtain the dir which is the lua script resides in.

Regards

> f = io.popen(cmd,"r")
> path = f:read("*a")
> f:close()
>
> Milind
>
>
> On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <[hidden email]> wrote:
>>
>> Hi,
>>
>> Is there any robust and convenient methods for me to obtain the lua
>> script's (absolute/real) dirname and file name from within the script
>> itself?
>>
>> Regards
>> --
>> Hongsheng Zhao <[hidden email]>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

aryajur
If the Lua script resides in a different directory from the current directory where the lua interpreter started the script the path should be part of arg[0]. The path may be relative to current path or absolute. os.execute/io.popen method should give the current path. Combining this information should give the path of the script.

On Wed, Jan 22, 2020 at 7:40 AM Hongyi Zhao <[hidden email]> wrote:
Milind Gupta <[hidden email]> 于2020年1月22日周三 上午4:08写道:
>
> When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:
>
> for k,v in pairs(arg) do
> print(k,v)
> end
>
> When I run it like:
> D:\>lua d:\commandArgs.lua
> 0       d:\commandArgs.lua
> -1      lua
>
> When I do:
> D:\>lua commandArgs.lua
> 0       commandArgs.lua
> -1      lua
>
> This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:
>
> cmd = os.execute("pwd") and "pwd" or "cd"

The issue with method is:

`pwd' will give the work dir which maybe or may not the dir the lua
script resides in.

But I want to obtain the dir which is the lua script resides in.

Regards

> f = io.popen(cmd,"r")
> path = f:read("*a")
> f:close()
>
> Milind
>
>
> On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <[hidden email]> wrote:
>>
>> Hi,
>>
>> Is there any robust and convenient methods for me to obtain the lua
>> script's (absolute/real) dirname and file name from within the script
>> itself?
>>
>> Regards
>> --
>> Hongsheng Zhao <[hidden email]>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Viacheslav Usov
In reply to this post by Hongyi Zhao
On Tue, Jan 21, 2020 at 8:20 AM Hongyi Zhao <[hidden email]> wrote:

> Is there any robust and convenient methods for me to obtain the lua script's (absolute/real) dirname and file name from within the script itself?

Your question is not well defined. Lua can load and execute a string that resides entirely in memory without ever been stored in a file - is that a script? A Lua chunk can load and run another chunk, etc, some of which may or may not file based - what is "the script" in this case? Unless you indicate exactly what your "script" is, you are not going to get an unambiguous answer.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Viacheslav Usov <[hidden email]> 于2020年1月23日周四 上午1:48写道:
>
> On Tue, Jan 21, 2020 at 8:20 AM Hongyi Zhao <[hidden email]> wrote:
>
> > Is there any robust and convenient methods for me to obtain the lua script's (absolute/real) dirname and file name from within the script itself?
>
> Your question is not well defined. Lua can load and execute a string that resides entirely in memory without ever been stored in a file - is that a script? A Lua chunk can load and run another chunk, etc, some of which may or may not file based - what is "the script" in this case? Unless you indicate exactly what your "script" is, you are not going to get an unambiguous answer.

Sorry for my ambiguity in dicptions,  I only indicate the real file
based lua scripts.

Regards

>
> Cheers,
> V.



--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
In reply to this post by aryajur
Milind Gupta <[hidden email]> 于2020年1月23日周四 上午1:07写道:
>
> If the Lua script resides in a different directory from the current directory where the lua interpreter started the script the path should be part of arg[0]. The path may be relative to current path or absolute. os.execute/io.popen method should give the current path. Combining this information should give the path of the script.

Thanks, this is a valuable notes.

Regards

>
> On Wed, Jan 22, 2020 at 7:40 AM Hongyi Zhao <[hidden email]> wrote:
>>
>> Milind Gupta <[hidden email]> 于2020年1月22日周三 上午4:08写道:
>> >
>> > When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:
>> >
>> > for k,v in pairs(arg) do
>> > print(k,v)
>> > end
>> >
>> > When I run it like:
>> > D:\>lua d:\commandArgs.lua
>> > 0       d:\commandArgs.lua
>> > -1      lua
>> >
>> > When I do:
>> > D:\>lua commandArgs.lua
>> > 0       commandArgs.lua
>> > -1      lua
>> >
>> > This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:
>> >
>> > cmd = os.execute("pwd") and "pwd" or "cd"
>>
>> The issue with method is:
>>
>> `pwd' will give the work dir which maybe or may not the dir the lua
>> script resides in.
>>
>> But I want to obtain the dir which is the lua script resides in.
>>
>> Regards
>>
>> > f = io.popen(cmd,"r")
>> > path = f:read("*a")
>> > f:close()
>> >
>> > Milind
>> >
>> >
>> > On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <[hidden email]> wrote:
>> >>
>> >> Hi,
>> >>
>> >> Is there any robust and convenient methods for me to obtain the lua
>> >> script's (absolute/real) dirname and file name from within the script
>> >> itself?
>> >>
>> >> Regards
>> >> --
>> >> Hongsheng Zhao <[hidden email]>
>> >> Institute of Semiconductors, Chinese Academy of Sciences
>> >> GnuPG DSA: 0xD108493
>> >>
>>
>>
>> --
>> Hongsheng Zhao <[hidden email]>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

pocomane
In reply to this post by Hongyi Zhao
On Tue, Jan 21, 2020 at 8:20 AM Hongyi Zhao <[hidden email]> wrote:
> Is there any robust and convenient methods for me to obtain the lua
> script's (absolute/real) dirname and file name from within the script
> itself?

TL;DR. You can just go to the last snippet of code.

The question could be split in the following:
1) What I mean for "Scirpt file"
2) How get info about where it is
3) How extract the absolute path
4) Split name and directory

The 1) seems obvious but in lua you can execute code from hard disk,
memory, you can get it from network or from a usb or rs232 device. So
let's just restrict to cases where "Script file" make sense:

a) The "Main script file", i.e. the one you pass to the lua standalone
interpreter from the command line
b) Any other file loaded by the main one through `require`
c) A string passed to `load` with a third parameter that is a proper
file reference

These are all the ways you have to actually execute lua code. Note
that an embedding application that calls lua, falls in the case c) as
well as a lua C modules calling lua code.

The info 2) is in `arg[0]` just for the case a). However, for all the
cases you can use

```
path = debug.getinfo(1,"S").source:sub(2)
```

but remember that in c) what is returned is what you passed to `load`
as third parameter (is up to you to specify correctly where you took
the information).

However the info returned can be a RELATIVE path, e.g.:
- in a) could return `./main.lua` if you launched the script with `lua
./main.lua`
- in b) `package.path='./module/?.lua' require 'mymodule' ` could
return './module/mymodule.lua'

Sadly, if you really need the abolute path 3), you have to rely on
os-dependent tricks. For linux you can use popen and realpath:

```
absolute = io.popen("realpath '"..path.."'", 'r'):read('a')
absolute = absolute:gsub('[\n\r]*$','') -- needed to handle popen result
```

This give you the absolute path to the file, so for the step 4) you
need a bit of string parsing to split path and name, e.g.
`absolute:match('^(.*/)([^/]-)$')`.

So putting all togheter:

```
local fullpath = debug.getinfo(1,"S").source:sub(2)
fullpath = io.popen("realpath '"..fullpath.."'", 'r'):read('a')
fullpath = fullpath:gsub('[\n\r]*$','')

local dirname, filename = fullpath:match('^(.*/)([^/]-)$')
dirname = dirname or ''
filename = filename or fullpath
```

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Sean Conner
It was thus said that the Great pocomane once stated:
>
> Sadly, if you really need the abolute path 3), you have to rely on
> os-dependent tricks. For linux you can use popen and realpath:

  Sadly, realpath seems to be a Debian thing, not a Linux thing in general.

  -spc (Then again, who uses anything other than Debian?)


Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Sean Conner
It was thus said that the Great Sean Conner once stated:
> It was thus said that the Great pocomane once stated:
> >
> > Sadly, if you really need the abolute path 3), you have to rely on
> > os-dependent tricks. For linux you can use popen and realpath:
>
>   Sadly, realpath seems to be a Debian thing, not a Linux thing in general.

  Huh ... I can't seem to locate when the actual command realpath was first
included in Linux.  The information I found made it seem to be a Debian-only
thing (I don't run Debian) but other information seems to contradict that.
Only my main Linux system at home doesn't have the program realpath (but the
function realpath() exists in libc).

  -spc (So ... it may or may not exist on any given Linux system, is what
        I'm trying to say ... )

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Jeff Pohlmeyer
> It was thus said that the Great Sean Conner once stated:
> > It was thus said that the Great pocomane once stated:
> >   Sadly, realpath seems to be a Debian thing, not a Linux thing in general.

"readlink -f" might be more common, and should have similar results.

 - Jeff

Reply | Threaded
Open this post in threaded view
|

Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.

Hongyi Zhao
Jeff Pohlmeyer <[hidden email]> 于2020年1月30日周四 下午5:08写道:
>
> > It was thus said that the Great Sean Conner once stated:
> > > It was thus said that the Great pocomane once stated:
> > >   Sadly, realpath seems to be a Debian thing, not a Linux thing in general.
>
> "readlink -f" might be more common, and should have similar results.

No correct, both of them are components of GNU coreutils:

$ realpath --version | head -1
realpath (GNU coreutils) 8.30
$ readlink --version | head -1
readlink (GNU coreutils) 8.30

$ lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 19.10
Release:    19.10
Codename:    eoan



>
>  - Jeff
>


--
Hongsheng Zhao <[hidden email]>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493

12