Skip to content

Commit

Permalink
Merge pull request #15 from sot/fix-catalina
Browse files Browse the repository at this point in the history
Fix bash support on catalina + modernize
  • Loading branch information
jeanconn authored Jan 9, 2020
2 parents 6c345e2 + 78532fd commit abebe8d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions .git_archival.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref-names: $Format:%D$
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git_archival.txt export-subst
4 changes: 3 additions & 1 deletion Ska/Shell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import ska_helpers

from .shell import *

__version__ = '3.3.4'
__version__ = ska_helpers.get_version(__package__)


def test(*args, **kwargs):
Expand Down
11 changes: 6 additions & 5 deletions Ska/Shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import os
import sys
import signal
import platform
import subprocess

import six


class ShellError(Exception):
pass
Expand Down Expand Up @@ -93,10 +92,12 @@ def _setup_bash_shell(logfile):

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

return shell, re_prompt

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

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

shell.sendline('set prompt="{}"'.format(prompt))
Expand Down
21 changes: 11 additions & 10 deletions Ska/Shell/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def test_os_error(self):
spawn = Spawn(stdout=None)
with pytest.raises(OSError):
spawn.run('bad command')
assert spawn.exitstatus == None
assert spawn.exitstatus is None

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

def test_grab_stderr(self, tmpdir):
tmp = tmpdir.join("test.out")
Expand Down Expand Up @@ -90,10 +90,15 @@ def test_logfile(self):
cmd = 'echo line1; echo line2'
bash(cmd, logfile=logfile)
outlines = logfile.getvalue().splitlines()
assert outlines[0].endswith(cmd)
assert outlines[1] == 'line1'
assert outlines[2] == 'line2'
assert outlines[3].startswith('Bash')

# Note that starting bash may generate cruft at the beginning (e.g. the
# annoying message on catalina that zsh is now the default shell). So
# the tests reference expected output from the end of the log not the
# beginning.
assert outlines[-4].endswith(cmd)
assert outlines[-3] == 'line1'
assert outlines[-2] == 'line2'
assert outlines[-1].startswith('Bash')

@pytest.mark.skipif('not HAS_HEAD_CIAO', reason='Test requires /soft/ciao/bin/ciao.sh')
def test_ciao(self):
Expand Down Expand Up @@ -147,7 +152,3 @@ def test_ciao(self):
test_script = ['printenv {}'.format(name) for name in sorted(envs)]
outlines = tcsh('\n'.join(test_script), env=envs)
assert outlines == [envs[name] for name in sorted(envs)]


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from setuptools import setup

from Ska.Shell import __version__
try:
from testr.setup_helper import cmdclass
except ImportError:
Expand All @@ -11,7 +10,8 @@
author='Tom Aldcroft',
description='Various shell utilities',
author_email='taldcroft@cfa.harvard.edu',
version=__version__,
use_scm_version=True,
setup_requires=['setuptools_scm', 'setuptools_scm_git_archive'],
zip_safe=False,
packages=['Ska', 'Ska.Shell', 'Ska.Shell.tests'],
tests_require=['pytest'],
Expand Down

0 comments on commit abebe8d

Please sign in to comment.