astigmatik@gmail.com wrote:
On Dec 12, 2007 7:10 PM, Sébastien Boisgérault
<Sebastien.Boisgerault@ensmp.fr> wrote:
  
 For what I wanted to  do (get the stdout and stderr content in a single
string),
 my code was pretty naive and not portable (and as a matter of fact, this
kind
 of mistake is described pretty much everywhere on the web, to begin with
 in the standard C Python doc ;)).

 I switched a version based on "popen2.popen4" and now everything "just
works"
 for every Windows/Linux, CPython/Jython combination :)

 Cheers,

    

Can you share with us the working snippet that you have now? I'm not
very familiar with popen, so any example would be nice.
  
Sure, but you may be disappointed, as my use case is finally *very* simple.
The basic piece is in the read function:

def read(command, input=None):
    """
    Provide the command standard output and error merged into a single string
    """
    std_out_err, std_in = popen2.popen4(command)
    if input:
        std_in.write(input)
        std_in.close()
    return std_out_err.read()

I typically use this function to check the availability of some command-line
tools, such as 'jar' in the following example:

def CHECK_JAR(command="jar"):
    """
    jar -- Java archiver
    """
    jar = command
    usage = read(jar + " --help")

    if find_all(usage, "jar" ,"-c", "-t", "-x",  "-u"):
        # find_all is a helper function based on re.search

        return {"jar": jar}
    else:
        raise ConfigurationError("Java archiver not found.")

... or to check the availability of some Java libraries, such as Batik -- note that the check maybe run with Jython or CPython.

def CHECK_BATIK():
    """
    batik -- Batik SVG toolkit
    """

    packages = [ "org.apache.batik.dom.svg" ,
                 "org.apache.batik.util"    ,
                 "org.apache.batik.swing"   ]

    imports = "\n".join(["import %s" % package for package in packages])

    jython = config.get("jython") # get the path to jython script via a global config dict
    if jython is None:
        error = "can't test Batik availability: jython missing."
        raise ConfigurationError(error)
    else:
        output = read(jython + " -", imports)

    if not output == "": # uhu, successful imports should not produce any output
        raise ConfigurationError("Batik SVG toolkit not found.")

Cheers,

SB

Regards,
astigmatik

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users