Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpm --eval "%{lua:rpm.interactive()}" does not immediately print the output #1215

Closed
hroncok opened this issue May 12, 2020 · 18 comments
Closed

Comments

@hroncok
Copy link
Contributor

hroncok commented May 12, 2020

I'd liek to use the interactive Lua console, however, it doesn't seem to be very... interactive.

$ rpm --eval "%{lua:rpm.interactive()}"

RPM Interactive Lua 5.3 Interpreter
>

So far so good...

> print"a"
> 

Nothing...

> print"a"
> print"a"
> print"a"
> print"a"
>

Nothing...

> ^D
aaaaa

Is there a way to receive the output as I put the input?

@mlschroe
Copy link
Contributor

This happens because you're in a macro expansion, so all the output is collected and returned to the macro engine.

@hroncok
Copy link
Contributor Author

hroncok commented May 13, 2020 via email

@hroncok
Copy link
Contributor Author

hroncok commented May 13, 2020

This gets the job done:

from ctypes import cdll, c_char_p
librpmio = cdll.LoadLibrary("librpmio.so.9")
librpmio.rpmluaRunScript(None, c_char_p(b"rpm.interactive()"), None)

@pmatilai
Copy link
Member

Ouch 🤣

The interactive interpreter was added years before my time, but as far as I can recall, you're the first person ever to try actually using it. There may be some resident fauna there... 🪲 🐛

@hroncok
Copy link
Contributor Author

hroncok commented May 14, 2020

I have it all figured out. Blog post coming. Would you consider adding something like the above to rpm itself?

@pmatilai
Copy link
Member

If by "something like the above" you mean something that allows actual interactive experience, sure 😅

@hroncok
Copy link
Contributor Author

hroncok commented May 14, 2020

I meant something a bit more sophisticated. I have so far:

#!/usr/bin/python3
import sys
from ctypes import cdll, c_char_p


librpmio = cdll.LoadLibrary("librpmio.so.9")

adjust_path = b"""
if os.getenv("LUA_PATH") then
    package.path =  os.getenv("LUA_PATH") .. ";" .. package.path
end
"""

# first argument is an "rpmlua" pointer, but uses global one when NULL
# second argument is code
# third argument is "name", used in errors, reasonable default when NULL
librpmio.rpmluaRunScript(None, c_char_p(adjust_path), None)

if len(sys.argv) > 1:
    sys.argv[-1] = '/dev/stdin' if sys.argv[-1] == '-' else sys.argv[-1]
    # first argument as above, second argument is path
    librpmio.rpmluaRunScriptFile(None, c_char_p(sys.argv[-1].encode("utf-8")))
else:
    librpmio.rpmluaRunScript(None, c_char_p(b"rpm.interactive()"), None)

@voxik
Copy link
Contributor

voxik commented May 14, 2020

I think it could be enough if the documentation give more reasonable example such as:

rpm.expand("%{echo:some message}")

Which provides expected output:

$ rpm --eval "%{lua:rpm.interactive()}"

RPM Interactive Lua 5.3 Interpreter
> rpm.expand("%{echo:some message}")
some message
> rpm.expand("%{echo:%{_bindir}}")
/usr/bin
>

Or IDK what else one could be interested.

@voxik
Copy link
Contributor

voxik commented May 14, 2020

Or if there was a way to flush to the output the content collected by the macro engine ....

@pmatilai
Copy link
Member

Adding a native way to flush the output shouldn't be hard. This is code that for all practical purposes nobody has touched in 16 years so it's not surprising if its a bit rusty and squeaky...

@hroncok
Copy link
Contributor Author

hroncok commented May 14, 2020

Here is my blog post about how to get a nicer console https://eng.hroncok.cz/2020/05/14/ilua-rpm-console

@pmatilai
Copy link
Member

Wonderfully weird stuff 😄

@pmatilai
Copy link
Member

Just a word of caution: rpmluaRunScript() and rpmluaRunScriptFile() are not considered public API and are not available in the public headers on C side, although the symbols are accessible in the ABI. So they are subject to change without further notice, although the likelihood of that happening doesn't seem that great, they've been exactly the way are since their inception 16 years ago...

@hroncok
Copy link
Contributor Author

hroncok commented Jun 4, 2020

My script doesn't seem to see system defined macros. Do I need to call something explicitly to load them?

$ rpmlua 

RPM Interactive Lua 5.3 Interpreter
> print(rpm.expand("%python3"))
%python3
> rpm.define("test b")
> print(rpm.expand("%test"))
b
> 


$ rpm --eval "%{lua:rpm.interactive()}"

RPM Interactive Lua 5.3 Interpreter
> print(rpm.expand("%python3"))
> 
/usr/bin/python3

@hroncok
Copy link
Contributor Author

hroncok commented Jun 13, 2020

This gets the job done:

librpm = cdll.LoadLibrary("librpm.so.9")

# Load general configuration (such as macros defined in standard places)
# Second argument is target platform, NULL is the default
librpm.rpmReadConfigFiles(librpm.rpmcliRcfile, None)

@hroncok
Copy link
Contributor Author

hroncok commented Jun 18, 2020

Would you accept a pull request that that adds a rpmlua executable and makes it behave more or less like my Python script?

Note that my C skills are rusty and my autotools skills are almost nonexistent, so I will probably need some guidance.

pmatilai added a commit to pmatilai/rpm that referenced this issue Apr 12, 2021
Interactive should mean interactive, as in getting the results right
away and not when exiting the process. The interactive mode hasn't
gotten much attention along the way...

Fixes: rpm-software-management#1215
@hroncok
Copy link
Contributor Author

hroncok commented Apr 16, 2021

Thanks.

@pmatilai
Copy link
Member

Would you accept a pull request that that adds a rpmlua executable and makes it behave more or less like my Python script?

Added in #1867 , have fun 😄

I've readline history working locally too, just need to massage the autofoo stuff to acceptable state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants