Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I get scons to invoke an external script?

I'm trying to use scons to build a latex document. 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. I've looked over the scons documentation but it is not immediately clear to me what I need to do.

What I wish to achieve is essentially what you would get with this makefile:

document.pdf:  table.tex
    pdflatex document.tex

table.tex:
    python table_generator.py

How can I express this in scons?

like image 497
saffsd Avatar asked Apr 08 '09 12:04

saffsd


2 Answers

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.

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? 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).

In addition, the output of Command() can be used as input to PDF() if desired. For clarity, I didn't do that.

like image 146
Hexagon Avatar answered Oct 22 '22 18:10

Hexagon


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.

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. 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.

like image 29
Eli Courtwright Avatar answered Oct 22 '22 19:10

Eli Courtwright