mako icon indicating copy to clipboard operation
mako copied to clipboard

Run mako templates as standalone executables

Open TheJJ opened this issue 2 years ago • 11 comments

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

When using either

  • #!/usr/bin/env -S python3 -m mako -s -a --
  • #!/usr/bin/env -S mako-render -s -a -- as shebang, a file can be templated directly, and behaves similar to a PHP script. With this commit, Python can be run as PyHP :)

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

TheJJ avatar Mar 20 '22 20:03 TheJJ

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

zzzeek avatar Mar 20 '22 23:03 zzzeek

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

TheJJ avatar Mar 21 '22 12:03 TheJJ

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 :)

zzzeek avatar Mar 23 '22 20:03 zzzeek

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!

bourke avatar Mar 23 '22 21:03 bourke

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 avatar Mar 25 '22 13:03 TheJJ

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

TheJJ avatar Apr 09 '22 20:04 TheJJ

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 !

bourke avatar Apr 09 '22 23:04 bourke

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 avatar Apr 10 '22 09:04 TheJJ

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

TheJJ avatar Apr 16 '22 23:04 TheJJ

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

TheJJ avatar May 07 '22 17:05 TheJJ

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

TheJJ avatar May 29 '22 15:05 TheJJ