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
Run mako templates as standalone executables #358
Conversation
this would need the console_scripts entrypoint to really be complete right? |
Hi! Lines 69 to 70 in c814024
|
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 :) |
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! |
If you prefer creating something like 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 |
What do you think now? What do I have to change in order to get this or some variant shippable? :) |
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 😄 |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might as well accept it once the implementation can be simplified.
00603db
to
64a31f2
Compare
I've now added test cases for shebang stripping and argument passing 😸 |
ae6f756
to
f2f328a
Compare
Pling plong :) Is there anything I should improve further? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still generally uncomfortable with this patch as I still dont see a good use case for mako templates as shell scripts
23af5dd
to
092d7a2
Compare
That way, mako can directly evaluate template files, or can be used as a file interpreter. When using `#!/usr/bin/env -S mako-render -s --` or `#!/usr/bin/env -S python3 -m mako -s --` as file shebang, a file can be templated directly, and behaves similar to a PHP script. With this commit, Python can basically be run as PyHP :) This is very useful for example for generating static config files, especially in combination with some `execfs` which executes files when they are read. That way one can even programatically customize the a precious `xorg.conf`.
I've now added the cli option for shifting commandline args, and I'm passing the remaining cli arguments as template variable |
# we do this so templates can handle further args on their own | ||
if options.shift_args: | ||
saved_sys_argv = sys.argv | ||
sys.argv = [options.input] + options.args |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey there -
what I want is:
- do not change sys.argv at all
- make a new template variable "sys_argv" with
[options.input] + options.args
, or whatever is most appropriate for argparse - users that want to use argparse in a template use
parser.parse_args(sys_argv)
that's it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay great, I'll implement that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll mark it resolved when the next patch is put up (with the issue resolved :) )
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. |
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 - thesys.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:run as
$ ./test.mako yay This is awesome! stuff: yay