[Scons-users] Getting the result of an action?

Russell E. Owen rowen at uw.edu
Thu Sep 25 15:16:40 EDT 2014


Thank you both for your helpful answers.

For the record, this works (thanks to Jim Bosch for pointing me to the 
source code for TryAction):
        result = context.TryAction(SCons.Script.Action(r"$CC --version > 
$TARGET"))
        ccVersDumpOK, ccVersDump = result[0:2]
        if ccVersDumpOK:
            ....

I'm glad to know that subprocess works. I'll probably stick to 
TryAction, now that I have it running.

By the way, subprocess.Popen can be shortened with convenience function 
check_output:
    try:
        output = subprocess.check_output((gcc_path, "--version"))
        ....
    except Exception: # or catch subprocess.CalledProcessError to get 
more info about the failure
        raise Exception("No GCC at...


-- Russell

In article 
<B5DA56DED4F17E4499165B4F45BF14DFE31C5CAB at IRVEX007.corp.blizzard.net>,
 Brian Fitzgerald via Scons-users <scons-users at scons.org> wrote:

> The comment from Gary is how we do it in our build - we need to know the 
> exact version of GCC, so we run this as part of the build setup:
> 
> def GccVersion(env, gcc_path):
>   # Get its version
>   output, error = subprocess.Popen(
>       args="%s --version" % gcc_path, stdin=subprocess.PIPE,
>       stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
>       shell=True).communicate()
>   if len(output) == 0:
>     raise Exception("No GCC at %s" % gcc_path)
> 
>   env['BNET_GCC_VERSION_FULL'] = (output.splitlines())[0]
>   match = re.search('^\S+\s+\(GCC\)\s+(\S\S\S).+$', 
>   env['BNET_GCC_VERSION_FULL'])
>   if not match:
>    raise Exception("Could not parse GCC version from %s" % output)
> 
>   env['BNET_GCC_VERSION'] = match.group(1)
> 
> Ignore all the parts custom to us. We have some parts of the build 
> conditional on GCC version, so we need to be able to feed it to the rest of 
> the system, not just the C++ compile. It's a little gross, but it's wrapped 
> in a function so the rest of the code just sees something clean ?
> 
> From: Scons-users [mailto:scons-users-bounces at scons.org] On Behalf Of Gary 
> Oberbrunner
> Sent: Thursday, September 25, 2014 9:24 AM
> To: SCons users mailing list
> Subject: Re: [Scons-users] Getting the result of an action?
> 
> 
> 
> On Thu, Sep 25, 2014 at 11:21 AM, Russell E. Owen 
> <rowen at uw.edu<mailto:rowen at uw.edu>> wrote:
> As part of configuring some environment variables in an SConstruct file
> I want to run the command
> 
> $CC --version
> 
> and parse the output (or raise an exception if it fails). Is there
> something like SCons.Script.Action(action) that I can use? That
> particular command seems to only return the exit code, which isn't what
> I need in this case.
> 
> Alternatively, is it safe to use the subprocess module for this?
> 
> Yes, and yes. :-)  You can use a Configure context to do this, analogous to 
> how GNU Autoconf would work.  That's the fancy way. But for simple things 
> it's often simpler just to use subprocess and parse the output.  A SConstruct 
> is just a python script.
> 
> --
> Gary
> ---------------------------------------------------------------------
> _______________________________________________
> Scons-users mailing list
> Scons-users at scons.org
> https://pairlist4.pair.net/mailman/listinfo/scons-users



More information about the Scons-users mailing list