Skip to content

Commit

Permalink
updates #13
Browse files Browse the repository at this point in the history
modified to use psutil instead of relying on os.environ
not sure yet if it will work in windows
  • Loading branch information
Adam Kaufman committed Mar 2, 2015
1 parent 028a10c commit 9168044
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
6 changes: 6 additions & 0 deletions fencepy/helpers.py
Expand Up @@ -7,6 +7,7 @@
import os
import fnmatch
import platform
import psutil
import subprocess
import sys
from contextlib import contextmanager
Expand Down Expand Up @@ -123,3 +124,8 @@ def str2bool(value):
elif value.lower() in ('false', 'f', 'no', 'n', '0'):
return False
raise ValueError('{0} is not an acceptable boolean value'.format(value))


def get_shell():
"""Get the name of the running shell according to psutil"""
return psutil.Process(psutil.Process(os.getpid()).ppid()).name()
20 changes: 10 additions & 10 deletions fencepy/main.py
Expand Up @@ -9,11 +9,11 @@
import shutil
import sys
from . import plugins
from .helpers import getoutputoserror, findpybin, str2bool, py2, pyversionstr
from .helpers import getoutputoserror, findpybin, str2bool, py2, pyversionstr, get_shell

if py2():
try:
from ConfigParser import SafeConfigParser
else:
except ImportError:
from configparser import SafeConfigParser

from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -164,13 +164,13 @@ def _activate(args):
return 1

# unix-based shells
if 'SHELL' in os.environ.keys():
if os.environ['SHELL'].endswith('fish'):
apath = os.path.join(vdir, 'bin', 'activate.fish')
elif os.environ['SHELL'].endswith('csh'):
apath = os.path.join(vdir, 'bin', 'activate.csh')
else:
apath = os.path.join(vdir, 'bin', 'activate')
shell = get_shell()
if shell == 'fish':
apath = os.path.join(vdir, 'bin', 'activate.fish')
elif shell == 'csh':
apath = os.path.join(vdir, 'bin', 'activate.csh')
elif shell.endswith('sh'):
apath = os.path.join(vdir, 'bin', 'activate')

# windows -- get-help will always raise the OSError, but we can still use it for this
else:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -17,7 +17,8 @@
'fencepy-%s.%s=fencepy:fence' % sys.version_info[:2]]},
test_suite='tests',
install_requires=[
'virtualenv>=1.11'
'virtualenv>=1.11',
'psutil>=2.2.1'
],
classifiers=[
'Development Status :: 3 - Alpha',
Expand Down
21 changes: 5 additions & 16 deletions tests/test_fencepy.py
Expand Up @@ -135,28 +135,17 @@ def test_erase_does_not_exist(self):
self.assertEqual(ret, 1, 'there should be nothing to erase')
self.assertFalse(os.path.exists(self.default_args['virtualenv_dir']))

# This can only be used to test for unix-base shells. There is
# no way to reliably test for .bat and .ps1 because the derivation
# method depends on functions being available on the path
def _test_activate(self, shell, script):
# Since the shell is derived from pid, this can only be generically tested
# Full coverage remains elusive
def test_activate(self):
self.test_create_plain()
os.environ['SHELL'] = shell
tempout = StringIO()
with redirected(out=tempout):
ret = self._fence('-a')
output = tempout.getvalue()
output = tempout.getvalue()
self.assertEqual(ret, 0, 'activate printing failed')
self.assertTrue(self.default_args['virtualenv_dir'] in output)
self.assertTrue(script in output)

def test_activate_fish(self):
self._test_activate('fish', 'activate.fish')

def test_activate_csh(self):
self._test_activate('csh', 'activate.csh')

def test_activate_bash(self):
self._test_activate('bash', 'activate')
self.assertTrue('activate' in output)

def test_multiple_modes(self):
with raises(RuntimeError):
Expand Down

0 comments on commit 9168044

Please sign in to comment.