Skip to content

Commit

Permalink
Merge pull request #6 from alexcouper/add_sync_method
Browse files Browse the repository at this point in the history
Add sync argument; Bump to 0.6
  • Loading branch information
alexcouper committed May 30, 2016
2 parents ac7ef07 + 506746a commit 9393f66
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -1,3 +1,8 @@
0.6
---

Add `sync` argument to be able to run bash in non-blocking form

0.5
---

Expand Down
7 changes: 6 additions & 1 deletion bash/__init__.py
Expand Up @@ -18,10 +18,15 @@ def __init__(self, *args, **kwargs):
self.stdout = None
self.bash(*args, **kwargs)

def bash(self, cmd, env=None, stdout=PIPE, stderr=PIPE, timeout=None):
def bash(self, cmd, env=None, stdout=PIPE, stderr=PIPE, timeout=None, sync=True):
self.p = Popen(
cmd, shell=True, stdout=stdout, stdin=PIPE, stderr=stderr, env=env
)
if sync:
self.sync(timeout)
return self

def sync(self, timeout=None):
kwargs = {'input': self.stdout}
if timeout:
kwargs['timeout'] = timeout
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -8,7 +8,7 @@
setup(
name='bash',
description='Bash for Python',
version='0.5',
version='0.6',
long_description=long_description,
author='Alex Couper',
author_email='info@alexcouper.com',
Expand Down
10 changes: 10 additions & 0 deletions tests.py
@@ -1,3 +1,4 @@
from datetime import datetime
import unittest

try:
Expand Down Expand Up @@ -56,3 +57,12 @@ def test_timeout_works(self):
if not bash_module.SUBPROCESS_HAS_TIMEOUT:
raise unittest.SkipTest()
self.assertRaises(TimeoutExpired, bash, 'sleep 2; echo 1', timeout=1)

def test_sync_false_does_not_wait(self):
t1 = datetime.now()
b = bash('sleep 0.5; echo 1', sync=False)
t2 = datetime.now()

self.assertTrue((t2-t1).total_seconds() < 0.5)
b.sync()
self.assertEqual(b.stdout, b'1\n')
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py32,py34
envlist = py27,py32,py34,py35


[testenv]
Expand Down

0 comments on commit 9393f66

Please sign in to comment.