17 |
17 |
import json
|
18 |
18 |
import subprocess
|
19 |
19 |
import logging
|
|
20 |
import multiprocessing
|
20 |
21 |
|
21 |
22 |
from django.conf import settings
|
22 |
23 |
|
... | ... | |
28 |
29 |
|
29 |
30 |
|
30 |
31 |
def exec_phantom(data, script='do_login.js'):
|
31 |
|
phantom = subprocess.Popen([
|
32 |
|
settings.PHANTOM_JS_BINARY,
|
33 |
|
'--ignore-ssl-errors=yes', '--ssl-protocol=any',
|
34 |
|
os.path.join(settings.BASE_DIR, 'mandayejs', script)],
|
35 |
|
close_fds=True,
|
36 |
|
stdin=subprocess.PIPE,
|
37 |
|
stdout=subprocess.PIPE
|
38 |
|
)
|
|
32 |
result = None
|
|
33 |
waiting_time = getattr(settings, 'PHANTOM_JS_WAITING_TIME', 10)
|
|
34 |
|
|
35 |
def run(send_end):
|
|
36 |
phantom = subprocess.Popen([
|
|
37 |
settings.PHANTOM_JS_BINARY,
|
|
38 |
'--ignore-ssl-errors=yes', '--ssl-protocol=any',
|
|
39 |
os.path.join(settings.BASE_DIR, 'mandayejs', script)],
|
|
40 |
close_fds=True,
|
|
41 |
stdin=subprocess.PIPE,
|
|
42 |
stdout=subprocess.PIPE
|
|
43 |
)
|
|
44 |
stdout, stderr = phantom.communicate(json.dumps(data))
|
|
45 |
|
|
46 |
try:
|
|
47 |
result = json.loads(stdout)
|
|
48 |
except (ValueError,):
|
|
49 |
result = {"result": "failure, couldn't decode JSON"}
|
|
50 |
logger.error(stdout)
|
|
51 |
|
|
52 |
if result.get('stderr', None):
|
|
53 |
logger.warning(result['stderr'])
|
|
54 |
|
|
55 |
send_end.send(result)
|
|
56 |
|
|
57 |
recv_end, send_end = multiprocessing.Pipe(False)
|
|
58 |
process = multiprocessing.Process(target=run, args=(send_end,))
|
|
59 |
process.start()
|
|
60 |
process.join(waiting_time)
|
|
61 |
|
|
62 |
if process.is_alive():
|
|
63 |
process.terminate()
|
|
64 |
logger.error("Phantomjs process stalled with conetext : %s " % data.pop('locators'))
|
|
65 |
result = {'result': 'failure'}
|
|
66 |
else:
|
|
67 |
result = recv_end.recv()
|
39 |
68 |
|
40 |
|
stdout, stderr = phantom.communicate(json.dumps(data))
|
41 |
|
|
42 |
|
try:
|
43 |
|
result = json.loads(stdout)
|
44 |
|
except (ValueError,):
|
45 |
|
result = {"result": "failure, couldn't decode JSON"}
|
46 |
|
logger.error(stdout)
|
47 |
|
|
48 |
|
# only kill process if it's still running
|
49 |
|
if phantom.poll() is None:
|
50 |
|
phantom.terminate()
|
51 |
|
|
52 |
|
if result.get('stderr'):
|
53 |
|
logger.warning(result['stderr'])
|
54 |
69 |
return result
|
55 |
70 |
|
56 |
71 |
|
57 |
|
-
|