How to execute a (secure) bash shell command in setup.py?

I am using nunjucks to template an interface in a python project. Nunjucks templates must be precompiled into production. I do not use extensions or asynchronous filters in nunjucks templates. Instead of using grunt-task to listen for changes in my templates, I prefer to use the nunjucks-precompile command (offered through npm) to expand the entire templates directory in templates.js.

The idea is for the nunjucks-precompile --include ["\\.tmpl$"] path/to/templates > templates.js executed in the setup.py file so that I can simply execute our deployment scripts regularly.

I found to override setuptools and the distutils script argument may serve the right purpose, but I'm not sure if this is the easiest approach to execute.

Another approach is to use subprocess to execute the command directly in setup.py, but I was warned about this (rather, proactive IMHO). I really don't understand why not.

Any ideas? Affirmations? Confirmations?

Update (04/2015):. If you do not have the nunjucks-precompile , simply use the Node package manager to install nunjucks as follows:

 $ npm install nunjucks 
+1
source share
2 answers

Sorry for the quick response. Hope this helps someone on the air. I want to share this now that I have developed a solution that I am pleased with.

Here's a solution that is safe and based on a record by Peter Lamut . Note that this does not use shell = True in the subprocess call. You can get around the command line requirements in your python deployment system and also use this to obfuscate and package JS anyway.

 from setuptools import setup from setuptools.command.install import install import subprocess import os class CustomInstallCommand(install): """Custom install setup to help run shell commands (outside shell) before installation""" def run(self): dir_path = os.path.dirname(os.path.realpath(__file__)) template_path = os.path.join(dir_path, 'src/path/to/templates') templatejs_path = os.path.join(dir_path, 'src/path/to/templates.js') templatejs = subprocess.check_output([ 'nunjucks-precompile', '--include', '["\\.tmpl$"]', template_path ]) f = open(templatejs_path, 'w') f.write(templatejs) f.close() install.run(self) setup(cmdclass={'install': CustomInstallCommand}, ... ) 
+3
source

I think the link here encapsulates what you are trying to achieve.

0
source

Source: https://habr.com/ru/post/1265341/


All Articles