简体   繁体   English

如何让scons调用外部脚本?

[英]How to I get scons to invoke an external script?

I'm trying to use scons to build a latex document. 我正在尝试使用scons来构建一个乳胶文档。 In particular, I want to get scons to invoke a python program that generates a file containing a table that is \\input{} into the main document. 特别是,我想让scons调用一个python程序,该程序生成一个包含一个表为\\ input {}的文件到主文档中。 I've looked over the scons documentation but it is not immediately clear to me what I need to do. 我查看了scons文档,但我不清楚我需要做什么。

What I wish to achieve is essentially what you would get with this makefile: 我希望实现的基本上是你用这个makefile得到的东西:

document.pdf:  table.tex
    pdflatex document.tex

table.tex:
    python table_generator.py

How can I express this in scons? 我如何在scons中表达这一点?

Something along these lines should do - 沿着这些方向应该做的事情 -

env.Command ('document.tex', '', 'python table_generator.py')
env.PDF ('document.pdf', 'document.tex')

It declares that 'document.tex' is generated by calling the Python script, and requests a PDF document to be created from this generatd 'document.tex' file. 它声明'document.tex'是通过调用Python脚本生成的,并请求从这个generatd'document.tex'文件创建一个PDF文档。

Note that this is in spirit only. 请注意,这只是精神上的。 It may require some tweaking. 这可能需要一些调整。 In particular, I'm not certain what kind of semantics you would want for the generation of 'document.tex' - should it be generated every time? 特别是,我不确定你想要生成'document.tex'需要什么样的语义 - 它是否应该每次生成? Only when it doesn't exist? 只有它不存在? When some other file changes? 当其他一些文件发生变化? (you would want to add this dependency as the second argument to Command() that case). (您希望将此依赖项添加为Command()的第二个参数)。

In addition, the output of Command() can be used as input to PDF() if desired. 此外,如果需要,Command()的输出可以用作PDF()的输入。 For clarity, I didn't do that. 为清楚起见,我没有这样做。

In this simple case, the easiest way is to just use the subprocess module 在这个简单的例子中,最简单的方法是使用子进程模块

from subprocess import call
call("python table_generator.py")
call("pdflatex document.tex")

Regardless of where in your SConstruct file these lines are placed, they will happen before any of the compiling and linking performed by SCons. 无论SConstruct文件放在哪里放置这些行,它们都会在SCons执行任何编译和链接之前发生。

The downside is that these commands will be executed every time you run SCons, rather than only when the files have changed, which is what would happen in your example Makefile. 缺点是每次运行SCons时都会执行这些命令,而不是仅在文件发生更改时执行,这就是示例Makefile中会发生的情况。 So if those commands take a long time to run, this wouldn't be a good solution. 因此,如果这些命令需要很长时间才能运行,这将不是一个好的解决方案。

If you really need to only run these commands when the files have changed, look at the SCons manual section Writing Your Own Builders . 如果您确实只需要在文件更改时运行这些命令,请查看SCons手册部分编写自己的构建器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM