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

Add new rpmExpandThisMacro() public method #1414

Merged
merged 7 commits into from Oct 30, 2020

Conversation

mlschroe
Copy link
Contributor

No description provided.

@mlschroe
Copy link
Contributor Author

This needs to be connected to python/lua as well. How should the interface look like?

@pmatilai
Copy link
Member

I can pick up the Lua side once this is merged, no need to redo all that. As for Python, the bindings wrt macros are really primitive as it is and nobody is complaining so I dunno if anybody cares. At any rate, I don't consider that a requirement for merging.

@pmatilai
Copy link
Member

...except that's exactly where it starts getting trickier: from Lua the argv is differently allocated and shouldn't be freed in freeArgs() and at least in my version there was "fun" with me->opts processing retriggering the option processing when it shouldn't have, etc. But many details are different here, I'll rebase the Lua work on top of this and see how it looks like now.

@pmatilai
Copy link
Member

pmatilai commented Oct 26, 2020

Ah, this takes a copy of the argv so the allocation business is different from what I had. https://github.com/pmatilai/rpm/tree/luamt-2 is a quick "port" on top of this and the basic testcases pass as-is, but only very briefly tested.

@pmatilai
Copy link
Member

Oh and BTW, there's actually an existing use-case for this in the code-base: this is exactly what runCall() in build/rpmfc.c wants.

@Conan-Kudo
Copy link
Member

@pmatilai spectool was rewritten to use RPM Python bindings, so I imagine at least it would matter for that tool.

@pmatilai
Copy link
Member

spectool was rewritten to use RPM Python bindings, so I imagine at least it would matter for that tool.

Hardly. There's precisely one rpm.expandMacro() in the entire new spectool code, and that doesn't involve parametric macros or anything else advanced, it's merely to expand %_sourcedir.

@mlschroe
Copy link
Contributor Author

I wonder if we should make the interface more sane. I currently made it similar to rpmExpandMacro(), but that was probably a bad idea. The interface is currently return 1 on success and -1 on failure, put string in the obuf pointer.
But the straightforward interface would be return string on success, NULL on failure.

@pmatilai
Copy link
Member

Heh, I almost commented on the return code thing, before noticing that it was actually to be same as rpmExpandMacro(). There was a kind of a reason for the return code to be like that, basically to leave room for returning more information in the future (but I don't recall if there was an actual plan as that what exactly).

Just had a closer look at the actual patches, my only real complaint is the amount duplication of code between expandMacro() and expandThisMacro(). That's the main difference between your version and mine, and avoids the recursion issues I ran into (so difference between working and non-working 😁 ), but... maybe some of the entry/exit mb checks could be further refactored to use common code?

@pmatilai
Copy link
Member

Okay, just for the reference, I suggested considering using an error code instead of NULL: #34 but as to how that turned into -1/1 I plead innocent :D

The documented "return negative on error" would allow changing it to return 0 on success like the inner calls do (so you could just pass the inner rc on), I certainly would not mind...

@pmatilai
Copy link
Member

Yet another BTW: I don't think it would be unreasonable to expect rpmExpandThisMacro() callers to supply the macro name as the first argument of the argv (similar to the exec() family of functions) so we wouldn't need to copy that internally just because - the caller knows the name and has to build the argv anyway...
With that you could technically even drop the "n" argument, but whether that'd make the interface too weird I dunno.

@mlschroe
Copy link
Contributor Author

So you're suggesting that we remove the argvFree() call in the freeArgs() function?

@mlschroe
Copy link
Contributor Author

I'll update the patch set after lunch.

@Conan-Kudo
Copy link
Member

spectool was rewritten to use RPM Python bindings, so I imagine at least it would matter for that tool.

Hardly. There's precisely one rpm.expandMacro() in the entire new spectool code, and that doesn't involve parametric macros or anything else advanced, it's merely to expand %_sourcedir.

That's true, however, it is reasonable to assume that other programs written in Python would need to do this, especially as parametric macros become more common. RPMLint does this in some places too. I've written code to use rpm.expandMacro() for personal tools too...

@pmatilai
Copy link
Member

pmatilai commented Oct 27, 2020

@Conan-Kudo , the point is that Python and macros reside on different planes of existence entirely. In the embedded Lua realm, you are a macro most of the time, whereas Python only sometimes casually observes the value of one from the outside. Nobody is forbidding folks from adding more sophisticated macro bindings for Python but in reality the use-cases don't seem to be there.

@mlschroe
Copy link
Contributor Author

Ok, I factored out the common code. I'm not super happy about the naming, though. Comment welcome ;)

@pmatilai
Copy link
Member

pmatilai commented Oct 28, 2020

Names are annoying... for a theme that's used elsewhere in rpm too, mbInit() and mbFini() maybe?
Also seems to me that the mbAllocBuf() calls could be moved to the start/init-part too, in which case Init seems even more appropriate. But yeah much nicer this way, regardless of names.

This gets rid of the weird and unneeded ownership transfer and
makes the args seetup more symmetric.
This eleminates duplicate code.
This expands the maco with the specified name. Argument expandsion
for parametric macros can be turned on with the RPMEXPAND_EXPAND_ARGS
flag.
@pmatilai
Copy link
Member

Oh, sorry I missed that last push from yesterday. Looks fine now, except that the last commit message still talks about mbStartExpansion() and mbFinishExpansion().

Add a mbInit() function that allocates the buffer, increases the
expansion depth and stashes some values into a MacroExpansionData area.
Add a mbFini() function that decreases the depth again,
restores the values and optionally prints the expansion result.
@pmatilai
Copy link
Member

Thanks!

@pmatilai pmatilai merged commit 98b71f7 into rpm-software-management:master Oct 30, 2020
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

Successfully merging this pull request may close these issues.

None yet

3 participants