drush-ops / drush

Drush is a command-line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those who spend their working hours hacking away at the command prompt.
https://www.drush.org
2.34k stars 1.08k forks source link

Calling drush vget --format=json inside a process without env vars returns PHP and not JSON! #2025

Open davidfischer-ch opened 8 years ago

davidfischer-ch commented 8 years ago

I am writing a Python 3 script to automated the deployment of a Drupal website. The procedure calls drush vget --format=json to retrieve the website variables and iterate over it.

I tested this script successfully but then when the script is called by cron the scripts failed to decode the output of drush. In fact I discovered that in that case the output is not a valid JSON string but a PHP representation of the array (key value)!

I tested the command interactively using a python interactive shell and I discovered that when the environment variables (env) is empty then the drush command effectively behaves differently.

>>> import json, subprocess
>>> json.loads(subprocess.check_output(['drush', '-p', '-r', '/home/www/sandbox/drupal', 'vget', '--format=json'], env={}).decode('utf-8'))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

VS

>>> import json, subprocess
>>> json.loads(subprocess.check_output(['drush', '-p', '-r', '/home/www/sandbox/drupal', 'vget', '--format=json']).decode('utf-8'))
{...} <- this is a python dict with the variables
davidfischer-ch commented 8 years ago

I found that drush needs PATH to be set. Drush probably fail to find libraries / command line tools when PATH is set to a bare minimal value (empty or what cron set). I think Drush should fail with a message in such a case instead of returning an unexpected value.