Grant Zukel
Grant Zukel

Reputation: 1181

Subprocess shell not passing yes with | to the bash script I'm executing

I'm trying to execute a script via Python. This script runs a toolset called review board tools based on the arguments passed into the script.

When I run the command:

yes | bash review 'feature-signature' 'PR_Reviewers' 'myrepo' 'myuser' 'mypassword' 'update' 'development' 'PR user' 'PR summary https://myjira.atlassian.net/browse/NEB-4257' 'https://myjira.atlassian.net/browse/NEB-4257' 'PR description'

The shell gets caught because rbtools asks the users on update if they want to update the review requests when the update is run.

When I run this command exactly as is in my terminal minus the obfuscation for my credentials and actual information it succeeds and finishes the update with no issues. It pipes the yes | into the script and auto answers the yes prompt.

The execution only hangs in Python.

The bash command review is a script I wrote to call review board tools I have tried putting the yes | in that script as well to no avail.

#!/usr/bin/env bash

BRANCH_NAME=$1
REVIEWERS=$2
REPO=$3
rb_user=$4
rb_password=$5
action=$6
base_branch=$7

#added these options
submit_as=$8
summary=$9
jira_id=$10
desc=$11

echo "Jira Link: $jira_id"
echo "Desc: $desc"
echo "Repo: $repo"
echo "Submit as: $submit_as"
echo "Summary $summary"
echo "Branch name $BRANCH_NAME"


cd $REPO
git checkout $BRANCH_NAME

if [ "$action" == "post" ]; then

    rbt post --repository=$REPO \
                   --tracking-branch="origin/$BRANCH_NAME" \
                   --parent="origin/$base_branch" \
                   --branch="$BRANCH_NAME" \
                   --guess-fields=auto \
                   --target-groups=$REVIEWERS \
                   --publish \
                   --server="http://reviews.domain" \
                   --username="$rb_user" \
                   --password=$rb_password \
                   --submit-as="$submit_as" \
                   --bugs-closed="$jira_id" \
                   --description="$summary" \
                   -d
else
   yes | rbt post --update --repository=$REPO \
                   --tracking-branch="origin/$BRANCH_NAME" \
                   --parent="origin/$base_branch" \
                   --branch="$BRANCH_NAME" \
                   --guess-fields="auto" \
                   --target-groups="$REVIEWERS" \
                   --publish \
                   --server="http://reviews.domain" \
                   --username="$rb_user" \
                   --password="$rb_password" \
                   --submit-as="$submit_as" \
                   --bugs-closed="$jira_id" \
                   --description="$summary" \
                   -d
fi

cd ../
exit 0

My Python code that executes the subprocess command is:

def run_command(command):
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    out, err = p.communicate()
    if err:
        if "bash" in command:
            return err
        else:
            print err
    return out

I don't understand why when I execute this command from Python the script hangs and gets stuck on the update, but when I run the exact command I print out in my Python script and run it manually in terminal it doesn't hang and completes with no issue.

I understand what's happening with the Python as p.communicate is waiting for the process to exist. The weird part is I get an email and the review requests get updated but my script never continues. Thus, it would see as though the yes is being piped into the command and its running no issues, but Python hangs like it didn't run or exit properly.

Unfortunately, rbtools doesn't have an auto yes function and thus I have to pipe the yes with a shell.

Any ideas on how to resolve this issue?

Upvotes: 0

Views: 385

Answers (0)

Related Questions