sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
353 stars 60 forks source link

Run mako templates as standalone executables #358

Closed TheJJ closed 11 months ago

TheJJ commented 2 years ago

That way, mako can directly evaluate template files and can be used as a file interpreter.

When using either

This is possible because -s or --strip-shebang strips away the shebang line in the input template. Program arguments get modified so you can even run ./awesome.mako --my-argument test --rolf

-a shifts the template argv - the sys.argv is replaced by the cmdline args to the template execution. It can then use regular argparse for parsing further arguments.

One can use this new feature to dynamically generate static config files, for example. Or generate arbitary files when using something like execfs.

So this could be a simple example for an executable test.mako file:

#!/usr/bin/mako-render -s --
This is awesome!
<%
# hehe like pyhp :)
import argparse
cli = argparse.ArgumentParser()
cli.add_argument("stuff")
# argparsing also works as expected for an executable:
args = cli.parse_args()
%>
stuff: ${args.stuff}

run as

$ ./test.mako yay
This is awesome!

stuff: yay
zzzeek commented 2 years ago

this would need the console_scripts entrypoint to really be complete right?

TheJJ commented 2 years ago

Hi! I think the installation of /usr/bin/mako-render using console_scripts is already implemented here: https://github.com/sqlalchemy/mako/blob/c81402434e8a277b0134262ad485debcffa20280/setup.cfg#L69-L70

zzzeek commented 2 years ago

OK, looked at this for another two minutes.

mako-render still seems like a weird name for something that's using the .mako file as an executable. maybe it's better as mako-invoke, and it strips the shebang in all cases then. a little less awkard maybe.

also. this is...a php thing? why are we doing this? php is sort of both a scripting language and a templating language, and it's also not really linked to anything else that acts like a container language for it, so I can see why that might exist.

but mako, it's just templating, and it's well embeddable into Python. if you want python scripting, .py already does all this. if you want a .py script with embedded mako templates, that not hard either.

i dunno just seems weird to have ".mako" as chmod 775 :)

bourke commented 2 years ago

I could see myself using this functionality for certain tasks, but I don't think it belongs in core Mako — it feels more like one of the utilities I'd use alongside, where maintainability is less of a concern. For one thing, stripping shebangs asks for a lot of testing, and some of that would need to be Windows-specific (which is certainly possible, but unpleasant). I also find the concept of ".mako" files as chmod 775 to be surprising, and I imagine other users would too.

Interesting approach, though!

TheJJ commented 2 years ago

If you prefer creating something like /usr/bin/mako-exec, I can create it :) As mako-render is like 95% of what I need, i thought it would be better to add it there.

This feature can be used to easily create, for example, configuration files for tools that can invoke binaries as input, or in combination with a fuse-filesystem (something like execfs) which executes files when reading them, one can finally auto-generate config files that have no scripting features otherwise (e.g. xorg-config, sway-config, ...).

So I'd love to see some support upstream in mako, because maintaining a shebang-compatible mako launcher works too, but seems kinda redundant when mako-render is nearly there :smile:

TheJJ commented 2 years ago

What do you think now? What do I have to change in order to get this or some variant shippable? :)

bourke commented 2 years ago

I'm -1 on this, I'm afraid.

This is absolutely something Mako can be used for (and as you've demonstrated, good for), but in my view it's not what Mako is designed for. "Support upstream" for an outlier use case (no matter how interesting) is more than I'd want to take on.

If you build a shebang-compatible Mako launcher, I'd use it; I just don't want to maintain it 😄

@zzzeek may feel otherwise, though... paging @zzzeek !

TheJJ commented 2 years ago

If it's a better way, I can also update the mako-parser with an option to ignore shebangs, then the whole input pipeline can stay the same but the parser will filter out the shebang line.

TheJJ commented 2 years ago

I've now added test cases for shebang stripping and argument passing :smile_cat:

TheJJ commented 2 years ago

Pling plong :) Is there anything I should improve further?

TheJJ commented 2 years ago

I've now added the cli option for shifting commandline args, and I'm passing the remaining cli arguments as template variable template_argv.

zzzeek commented 11 months ago

We dont really do much with Mako these days and I think this use case can be accomplished outside of Mako without changing mako itself, so closing.