Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking scons from a Python script

Tags:

python

scons

I'm new to scons and Python. I was wondering if there's a way to invoke scons from within a python script.

My python script accepts from the user, a list of directories where the code to be compiled together is located (in addition to doing some other non-trivial things). It also generates a string which is to be used as a name of the executable created by scons.

I want to pass this information from my python script to scons, and then invoke scons. Is there a simple way to do this?

I can think of the following possibilities:

  • use subprocess.call("scons"...) I'm not sure if scons accepts all the info I need to pass as command line arguments
  • have the python script write into a file. Have the SConscript parse the file and get the information passed.
like image 629
Neha Karanjkar Avatar asked Mar 06 '13 14:03

Neha Karanjkar


2 Answers

Well, I guess it's possible in theory. The scons executable itself is just a python script and all it does is execute SCons.Script.main() after modifying the sys.path variable. But you would probably have to start digging deep into the source to really understand how to make it do things you want.

A much cleaner solution is calling your script from the SConscript file, which is probably the intended usage and should prove much easier.

like image 127
none_00 Avatar answered Sep 21 '22 07:09

none_00


Ive done this using the subprocess.call() python function to encapsulate the somewhat long and complex command line args needed for the particular project I was working on. There are those that argued that the invocation should have been simpler so as not to need to encapsulate it, but that's a different subject :)

One option to consider instead of using command line arguments is to use the SCONSFLAGS environment variable as mentioned here. Personally, I prefer not to use this option.

All the options you need can be passed in as command line options. A good way to customize and handle SCons command line args is with the SCons AddOption() function. Additionally, you can get simple variable=value variables by using the SCons ARGUMENTS dictionary. Other related SCons command line functions are GetOption() and SetOption().

Regarding passing in a string for the executable name: SCons may not like this. At the very least, consider you execute your script once with a particular executable string, then a second time with a different one, then you want to clean, you may end up leaving executables around if SCons isnt executed with the same executable name.

like image 27
Brady Avatar answered Sep 24 '22 07:09

Brady