Skip to content

Commit

Permalink
Merge pull request #7 from sot/py3
Browse files Browse the repository at this point in the history
Python 3 compatibility
  • Loading branch information
taldcroft authored Jan 20, 2017
2 parents 0db8675 + 48d8676 commit 25fa247
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
11 changes: 11 additions & 0 deletions Ska/Shell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .shell import *

__version__ = '3.3.1'


def test(*args, **kwargs):
'''
Run py.test unit tests.
'''
import testr
return testr.test(*args, **kwargs)
15 changes: 9 additions & 6 deletions Ska/Shell.py → Ska/Shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import signal
import subprocess

__version__ = '0.3.1'
import six


class ShellError(Exception):
Expand Down Expand Up @@ -92,7 +92,8 @@ def _setup_bash_shell(logfile):

os.environ['PS1'] = prompt1
os.environ['PS2'] = prompt2
shell = pexpect.spawn('/bin/bash --noprofile --norc --noediting', timeout=1e8)
spawn = pexpect.spawn if six.PY2 else pexpect.spawnu
shell = spawn('/bin/bash --noprofile --norc --noediting', timeout=1e8)
shell.logfile_read = logfile
shell.expect(r'.+')

Expand All @@ -109,7 +110,8 @@ def _setup_tcsh_shell(logfile):
# line that needs to be skipped.
pexpect.spawn.sendline_expect = _sendline_expect_func(re_prompt, n_skip=2)

shell = pexpect.spawn('/bin/tcsh -f', timeout=1e8)
spawn = pexpect.spawn if six.PY2 else pexpect.spawnu
shell = spawn('/bin/tcsh -f', timeout=1e8)

shell.sendline('set prompt="{}"'.format(prompt))
shell.expect(re_prompt)
Expand Down Expand Up @@ -427,7 +429,8 @@ def run(self, cmd, timeout=None, catch=None, shell=None):
self.exitstatus = None

try:
self.process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=stderr, shell=shell)
self.process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=stderr, shell=shell,
universal_newlines=True)

prev_alarm_handler = signal.signal(signal.SIGALRM,
Spawn._timeout_handler(self.process.pid, timeout))
Expand All @@ -439,13 +442,13 @@ def run(self, cmd, timeout=None, catch=None, shell=None):

signal.signal(signal.SIGALRM, prev_alarm_handler)

except RunTimeoutError, e:
except RunTimeoutError as e:
if catch:
self._write('Warning - RunTimeoutError: %s\n' % e)
else:
raise

except OSError, e:
except OSError as e:
if catch:
self._write('Warning - OSError: %s\n' % e)
else:
Expand Down
Empty file added Ska/Shell/tests/__init__.py
Empty file.
48 changes: 24 additions & 24 deletions test.py → Ska/Shell/tests/test_shell.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import os
import unittest
from StringIO import StringIO
import pytest
from six.moves import cStringIO as StringIO
from Ska.Shell import (Spawn, RunTimeoutError, bash, tcsh, getenv, importenv,
tcsh_shell, bash_shell)

outfile = 'ska_shell_test.dat'


class TestSpawn(unittest.TestCase):
def setUp(self):
class TestSpawn:
def setup(self):
self.f = StringIO()
self.g = StringIO()

def test_ok(self):
spawn = Spawn(stdout=self.f)
spawn.run(['echo', 'hello world'])
self.assertEqual(spawn.exitstatus, 0)
self.assertEqual(spawn.outlines, ['hello world\n'])
self.assertEqual(self.f.getvalue(), 'hello world\n')
assert spawn.exitstatus == 0
assert spawn.outlines == ['hello world\n']
assert self.f.getvalue() == 'hello world\n'

def test_os_error(self):
spawn = Spawn(stdout=None)
self.assertRaises(OSError, spawn.run, 'bad command')
self.assertEqual(spawn.exitstatus, None)
with pytest.raises(OSError):
spawn.run('bad command')
assert spawn.exitstatus == None

def test_timeout_error(self):
spawn = Spawn(shell=True, timeout=1, stdout=None)
self.assertRaises(RunTimeoutError, spawn.run, 'sleep 5')
self.assertEqual(spawn.exitstatus, None)
with pytest.raises(RunTimeoutError):
spawn.run('sleep 5')
assert spawn.exitstatus == None

def test_grab_stderr(self):
f = open(outfile, 'w')
spawn = Spawn(stderr=f, stdout=None)
def test_grab_stderr(self, tmpdir):
tmp = tmpdir.join("test.out")
spawn = Spawn(stderr=tmp.open('w'), stdout=None)
spawn.run('perl -e "print STDERR 123456"', shell=True)
f.close()
f = open(outfile)
self.assertEqual(f.read(), '123456')
self.assertEqual(spawn.exitstatus, 0)
os.unlink(outfile)

assert tmp.read() == '123456'
assert spawn.exitstatus == 0

def test_multi_stdout(self):
spawn = Spawn(stdout=[self.f, self.g])
spawn.run('perl -e "print 123456"', shell=True)
self.assertEqual(self.f.getvalue(), '123456')
self.assertEqual(self.g.getvalue(), '123456')
self.assertEqual(spawn.exitstatus, 0)
assert self.f.getvalue() == '123456'
assert self.g.getvalue() == '123456'
assert spawn.exitstatus == 0

def test_shell_error(self):
# With shell=True you don't get an OSError
spawn = Spawn(shell=True, stdout=None)
spawn.run('sadfasdfasdf')
self.assertNotEqual(spawn.exitstatus, 0)
self.assertNotEqual(spawn.exitstatus, None)
assert spawn.exitstatus != 0
assert spawn.exitstatus is not None


class TestBash:
Expand Down
12 changes: 7 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from setuptools import setup

from Ska.Shell import __version__
try:
from testr.setup_helper import cmdclass
except ImportError:
cmdclass = {}

setup(name='Ska.Shell',
author='Tom Aldcroft',
description='Various shell utilities',
author_email='taldcroft@cfa.harvard.edu',
py_modules=['Ska.Shell'],
test_suite='test',
version=__version__,
zip_safe=False,
packages=['Ska'],
package_dir={'Ska': 'Ska'},
package_data={}
packages=['Ska', 'Ska.Shell', 'Ska.Shell.tests'],
tests_require=['pytest'],
cmdclass=cmdclass,
)

0 comments on commit 25fa247

Please sign in to comment.