如何在 setup.py 中执行(安全的)bash shell 命令? [英] How to execute a (safe) bash shell command within setup.py?

查看:81
本文介绍了如何在 setup.py 中执行(安全的)bash shell 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nunjucks 在 python 项目中模板化前端.Nunjucks 模板必须在生产中预编译.我不在 nunjucks 模板中使用扩展或异步过滤器.与其使用 grunt-task 来监听模板的变化,我更喜欢使用 nunjucks-precompile 命令(通过 npm 提供)将整个模板目录扫描到 templates.js 中.

I use nunjucks for templating the frontend in a python project. Nunjucks templates must be precompiled in production. I don't use extensions or asynchronous filters in the nunjucks templates. Rather than use grunt-task to listen for changes to my templates, I prefer to use the nunjucks-precompile command (offered via npm) to sweep the entire templates directory into templates.js.

这个想法是让 nunjucks-precompile --include ["\\.tmpl$"] path/to/templates >templates.js 命令在 setup.py 中执行,因此我可以简单地搭载我们的部署程序脚本的常规执行.

The idea is to have the nunjucks-precompile --include ["\\.tmpl$"] path/to/templates > templates.js command execute within setup.py so I can simply piggyback our deployer scripts' regular execution.

我找到了 设置工具覆盖一个 distutils 脚本论证可能有正确的目的,但我不确定这是否是最简单的执行方法.

I found a setuptools override and a distutils scripts argument might serve the right purpose, but I'm not so sure either is the simplest approach to execution.

另一种方法是使用 subprocess 直接在 setup.py 中执行命令,但我已经被警告不要这样做(而是先发制人,恕我直言).我真的不明白为什么不.

Another approach is to use subprocess to execute the command directly within setup.py, but I've been cautioned against this (rather preemptively IMHO). I don't really deeply understand why not.

有什么想法吗?肯定?确认?

Any ideas? Affirmations? Confirmations?

更新 (04/2015): - 如果您没有可用的 nunjucks-precompile 命令,只需使用节点包管理器安装 nunjucks,如下所示:

Update (04/2015): - If you don't have the nunjucks-precompile command available, simply use Node Package Manager to install nunjucks like so:

$ npm install nunjucks

推荐答案

恕我直言.我希望这可以帮助以太坊中的某个人.既然我已经找到了一个令我满意的解决方案,我想分享一下.

Pardon the quick self-answer. I hope this helps someone out there in the ether. I want to share this now that I've worked out a solution I'm satisfied with.

这是一个安全的解决方案,基于 彼得·拉穆特 (Peter Lamut) 的文章.请注意,这在子进程调用中使用 shell=True.您可以绕过 Python 部署系统上的 grunt-task 要求,也可以使用它进行混淆和 JS 打包.

Here's a solution that's safe and based on Peter Lamut's write-up. Note that this does not use shell=True in the subprocess invocation. You may bypass grunt-task requirements on your python deployment system and also use this for obfuscation and JS packaging all the same.

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},
      ...
     )

这篇关于如何在 setup.py 中执行(安全的)bash shell 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆