0

So I'm trying to capture the output from drush via python. The main problem is that when drush fails in a task (e.g. unable to make a select) it gives an output in non standard format. I want to control this in order to put in on a loop and then if there's an error, continue with the next site.

This is output of the drush:

client_global_hostkeys_private_confirm: server gave bad signature for RSA key 0

In SqlCommands.php line 200:
                                                                                                                                        
  Query failed. Rerun with --debug to see any error message. ERROR 1146 (42S02) at line 1: Table 'main.users_field_data' doesn't exist  
                                                                                                                                        

This is the code that i'm trying to run:

import os
import subprocess
import json

def run_command(command):
    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    return iter(p.stdout.readline, b'')

getuids = os.popen('drush -- sql:query "SELECT GROUP_CONCAT(uid) FROM (SELECT DISTINCT uid FROM users_field_data LIMIT 5000) AS ids"')
outputuids=getuids.read()
valuein = "200"
if valuein in outputuids:
    print("Error found")
else:
    print("No error")

As you might already discover, the valuein variable is the text that I want to search, but no matter how, I always get no error. BUT if I make a variable and remove the carriage return from this output:

client_global_hostkeys_private_confirm: server gave bad signature for RSA key 0

In SqlCommands.php line 200:

Query failed. Rerun with --debug to see any error message. ERROR 1146 (42S02) at line 1: Table 'main.users_field_data' doesn't exist

The if condition finds the 200 on the text and prints Error Found.

Do you know how to achieve this? I tried replacing it but no result at all.

Any help would be appreciated.

  • maybe `drush` outputs its errors on `stderr` instead of `stdout`... maybe you haven't captured the text? and then you saw that it works if you copy and paste the text into a variable and use that instead of the `getuids.read()` result? – Anentropic May 03 '22 at 11:01
  • thanks, but I've tried also changing the stdout to stderr and also on the if reading directly the getuids.read() but got the same behavior. :( – user2965052 May 03 '22 at 11:06

0 Answers0