4

I have a python function that makes a subprocess call to a shell script that outputs 'true' or 'false'. I'm storing the output from subprocess.communicate() and trying to do return output == 'true' but it returns False every time. I'm not too familiar with python, but reading about string comparisons says you can compare strings using ==, !=, etc.

Here's the code:

def verifydeployment(application):
    from subprocess import Popen, PIPE
    import socket, time

    # Loop until jboss is up.  After 90 seconds the script stops looping; this
    # causes twiddle to be unsuccessful and deployment is considered 'failed'.
    begin = time.time()
    while True:
        try:
            socket.create_connection(('localhost', 8080))
            break
        except socket.error, msg:
            if (time.time() - begin) > 90:
                break
            else:
                continue

    time.sleep(15)  # sleep for 15 seconds to allow JMX to initialize

    twiddle = os.path.join(JBOSS_DIR, 'bin', 'twiddle.sh')
    url = 'file:' + os.path.join(JBOSS_DIR, 'server', 'default', 'deploy', os.path.basename(application))

    p = Popen([twiddle, 'invoke', 'jboss.system:service=MainDeployer', 'isDeployed', url], stdout=PIPE)
    isdeployed = p.communicate()[0]

    print type(isdeployed)
    print type('true')
    print isdeployed
    return isdeployed == 'true'

The output is:

<type 'str'> # type(isdeployed)
<type 'str'> # type('true')
true         # isdeployed

but False is always returned. I also tried return str(isdeployed) == 'true'.

1
  • You're sure there's no new line after 'true'? Maybe try isdeployed.strip() == 'true' Commented Mar 25, 2010 at 15:32

2 Answers 2

8

Are you sure that there isn't a terminating line feed character, making your string contain "true\n"? That seems likely.

You could try return isdeployed.startswith("true"), or some stripping.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, there is. That was a simple one and it's been bugging me for a while. Thanks!
6

Have you tried to call

isdeployed.strip()

before the comparision

1 Comment

I didn't notice the newline. I'll use the strip() function. thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.