Skip to content

Daemon mode #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ python:

before_install:
- true && `base64 --decode <<< ZXhwb3J0IEJST1dTRVJTVEFDS19BQ0NFU1NfS0VZPUh5VmZydXJvb3dYb041eGhLZEs2Cg==`
- pip install psutil

script: python -m unittest discover
48 changes: 24 additions & 24 deletions browserstack/local.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import subprocess, os, time
import subprocess, os, time, json, psutil
from browserstack.local_binary import LocalBinary
from browserstack.bserrors import BrowserStackLocalError

Expand All @@ -17,12 +17,17 @@ def __xstr(self, key, value):
return ['-' + key, value]

def _generate_cmd(self):
cmd = [self.binary_path, '-logFile', self.local_logfile_path, self.key]
cmd = [self.binary_path, '-d', 'start', '-logFile', self.local_logfile_path, self.key]
for o in self.options.keys():
if self.options.get(o) is not None:
cmd = cmd + self.__xstr(o, self.options.get(o))
return cmd

def _generate_stop_cmd(self):
cmd = self._generate_cmd()
cmd[2] = 'stop'
return cmd

def start(self, **kwargs):
self.options = kwargs

Expand All @@ -43,34 +48,29 @@ def start(self, **kwargs):
if "onlyCommand" in kwargs and kwargs["onlyCommand"]:
return

self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE)
self.stderr = self.proc.stderr
self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = self.proc.communicate()

os.system('echo "" > "'+ self.local_logfile_path +'"')
with open(self.local_logfile_path, 'r') as local_logfile:
while True:
line = local_logfile.readline()
if 'Error:' in line.strip():
raise BrowserStackLocalError(line)
elif line.strip() == 'Press Ctrl-C to exit':
break
try:
if out:
data = json.loads(out.decode())
else:
data = json.loads(err.decode())

while True:
if self.isRunning():
break
time.sleep(1)
if data['state'] != "connected":
raise BrowserStackLocalError(data["message"])
else:
self.pid = data['pid']
except ValueError:
raise BrowserStackLocalError('Error parsing JSON output from daemon')

def isRunning(self):
if (hasattr(self, 'proc')):
return True if self.proc.poll() is None else False
return False
return hasattr(self, 'pid') and psutil.pid_exists(self.pid)

def stop(self):
try:
self.proc.terminate()
while True:
if not self.isRunning():
break
time.sleep(1)
proc = subprocess.Popen(self._generate_stop_cmd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
except Exception as e:
return
return
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
setup(
name = 'browserstack-local',
packages = ['browserstack'],
version = '0.3.1',
version = '1.0.0',
description = 'Python bindings for Browserstack Local',
author = 'BrowserStack',
author_email = '[email protected]',
Expand Down
20 changes: 15 additions & 5 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ def test_start_local(self):
self.local.start()
self.assertNotEqual(self.local.proc.pid, 0)

def test_running(self):
self.assertFalse(self.local.isRunning())
self.local.start()
self.assertTrue(self.local.isRunning())

def test_multiple(self):
self.assertFalse(self.local.isRunning())
self.local.start()
self.assertTrue(self.local.isRunning())
try:
self.local2 = Local(os.environ['BROWSERSTACK_ACCESS_KEY'])
self.local2.start()
except BrowserStackLocalError as e:
self.assertEqual(str(e), "Either another browserstack local client is running on your machine or some server is listening on port 45691")

def test_verbose(self):
self.local.start(v=True, onlyCommand=True)
self.assertIn('-v', self.local._generate_cmd())
Expand Down Expand Up @@ -64,8 +79,3 @@ def test_local_identifier(self):
self.local.start(localIdentifier='mytunnel', onlyCommand=True)
self.assertIn('-localIdentifier', self.local._generate_cmd())
self.assertIn('mytunnel', self.local._generate_cmd())

def test_running(self):
self.assertFalse(self.local.isRunning())
self.local.start()
self.assertTrue(self.local.isRunning())