-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdrush.py
64 lines (54 loc) · 2.05 KB
/
drush.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from twisted.conch.error import ConchError
from twisted.internet import reactor, defer
from twisted.internet.protocol import ProcessProtocol
from twisted.python import log
import json
from config import config
# Load drush settings from drupaldaemons.cnf
webroot = config.get('drush-settings', 'webroot')
drush_path = config.get('drush-settings', 'drushPath')
class DrushError(ConchError):
pass
class DrushProcessProtocol(ProcessProtocol):
"""Read string values from Drush"""
def __init__(self, command):
self.raw = ""
self.raw_error = ""
self.deferred = defer.Deferred()
self.command = command
def outReceived(self, data):
self.raw += data
def errReceived(self, data):
self.raw_error += data
def outConnectionLost(self):
self.data = self.raw.strip()
def processEnded(self, status):
if self.raw_error:
log.err("Errors reported from drush:")
for each in self.raw_error.split("\n"):
log.err(" " + each)
rc = status.value.exitCode
if self.data and rc == 0:
self.deferred.callback(self)
else:
if rc == 0:
err = DrushError("Failed to read from drush.")
else:
err = DrushError("Drush failed ({0})".format(rc))
self.deferred.errback(err)
def call(self, *args):
exec_args = (drush_path, "--root={0}".format(webroot), self.command) + args
reactor.spawnProcess(self, drush_path, exec_args, env = {"TERM":"dumb"})
return self.deferred
class DrushProcessProtocolBool(DrushProcessProtocol):
bool_map = {"true":True, "false":False}
def outConnectionLost(self):
self.data = self.raw.strip()
self.result = self.bool_map[self.data]
class DrushProcessProtocolJSON(DrushProcessProtocol):
"""Read JSON values from Drush."""
def outConnectionLost(self):
try:
self.data = json.loads(self.raw)
except ValueError:
log.err("Drush {0} returned bad JSON.".format(self.command))