8000 WIP: No pexpect by javierggt · Pull Request #30 · sot/ska_shell · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

WIP: No pexpect #30

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
32 changes: 31 additions & 1 deletion ska_shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,38 @@ def getenv(cmdstr, shell='bash', importenv=False, env=None):

:returns: Dict of environment vars update produced by ``cmdstr``
"""
environ = dict(os.environ)
if env is not None:
environ.update(env)

p = subprocess.run(["which", shell], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.returncode:
raise Exception(f'Failed to find "{shell}" shell: {p.stderr.decode()}')
shell = p.stdout.decode().strip()
p = subprocess.run(
f"{cmdstr} && printenv",
shell=True,
executable=shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=environ,
)
if p.returncode:
raise Exception(f"Failed to get environment: {p.stderr.decode().strip()}")
newenv = _parse_keyvals(p.stdout.decode().split("\n"))

# Update os.environ based on changes to environment made by cmdstr
deltaenv = dict()
if importenv:
expected_diff_set = set(('PS1', 'PS2', '_', 'SHLVL')) if shell == 'bash' else set()
currenv = dict(os.environ)
_fix_paths(newenv)
for key in set(newenv) - expected_diff_set:
if key not in currenv or currenv[key] != newenv[key]:
deltaenv[key] = newenv[key]
if importenv:
os.environ.update(deltaenv)

outlines, newenv = run_shell(cmdstr, shell=shell, importenv=importenv, env=env, getenv=True)
return newenv


Expand Down
31 changes: 31 additions & 0 deletions ska_shell/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
tcsh_shell, bash_shell)

HAS_HEAD_CIAO = os.path.exists('/soft/ciao/bin/ciao.sh')
HAS_HEAD_ASCDS = os.path.exists('/home/ascds/.ascrc')

outfile = 'ska_shell_test.dat'

Expand Down Expand Up @@ -73,9 +74,23 @@ def test_bash_shell(self):
def test_env(self):
envs = getenv('export TEST_ENV_VARA="hello"')
assert envs['TEST_ENV_VARA'] == 'hello'
assert "TEST_ENV_VARA" not in os.environ

outlines = bash('echo $TEST_ENV_VARA', env=envs)
assert outlines == ['hello']

envs = getenv("echo", env={"TEST_ENV_VARA": "one"}, shell='bash')
assert envs['TEST_ENV_VARA'] == 'one'
assert "TEST_ENV_VARA" not in os.environ

envs = getenv("echo", env={"TEST_ENV_VARA": "two"}, shell='bash', importenv=True)
assert envs['TEST_ENV_VARA'] == 'two'
assert os.environ["TEST_ENV_VARA"] == 'two'

envs = getenv('export TEST_ENV_VARA="hello"', shell='bash', importenv=True)
assert envs['TEST_ENV_VARA'] == 'hello'
assert os.environ["TEST_ENV_VARA"] == 'hello'

def test_promptenv(self):
# Confirm that a messed up PS1 won't be a problem
# If the user messes with the prompts during use of the shell, all bets are off
Expand Down Expand Up @@ -124,9 +139,23 @@ def test_tcsh_shell(self):
def test_env(self):
envs = getenv('setenv TEST_ENV_VAR2 "hello"', shell='tcsh')
assert envs['TEST_ENV_VAR2'] == 'hello'
assert "TEST_ENV_VAR2" not in os.environ

outlines = tcsh('echo $TEST_ENV_VAR2', env=envs)
assert outlines == ['hello']

envs = getenv("echo", env={"TEST_ENV_VAR2": "one"}, shell='tcsh')
assert envs['TEST_ENV_VAR2'] == 'one'
assert "TEST_ENV_VAR2" not in os.environ

envs = getenv("echo", env={"TEST_ENV_VAR2": "two"}, shell='tcsh', importenv=True)
assert envs['TEST_ENV_VAR2'] == 'two'
assert os.environ["TEST_ENV_VAR2"] == 'two'

envs = getenv('setenv TEST_ENV_VAR2 "hello"', shell='tcsh', importenv=True)
assert envs['TEST_ENV_VAR2'] == 'hello'
assert os.environ["TEST_ENV_VAR2"] == 'hello'

def test_importenv(self):
importenv('setenv TEST_ENV_VAR3 "hello"', env={'TEST_ENV_VAR4': 'world'}, shell='tcsh')
assert os.environ['TEST_ENV_VAR3'] == 'hello'
Expand All @@ -144,12 +173,14 @@ def test_logfile(self, tmpdir):
assert outlines[3] == 'line2'
assert outlines[4].startswith('Tcsh')

@pytest.mark.skipif('not HAS_HEAD_ASCDS', reason='Test requires /home/ascds/.ascrc')
def test_ascds(self):
envs = getenv('source /home/ascds/.ascrc -r release', shell='tcsh')
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)]

@pytest.mark.skipif('not HAS_HEAD_CIAO', reason='Test requires /soft/ciao/bin/ciao.sh')
def test_ciao(self):
envs = getenv('source /soft/ciao/bin/ciao.csh', shell='tcsh')
test_script = ['printenv {}'.format(name) for name in sorted(envs)]
Expand Down
0