diff --git a/terminatorlib/util.py b/terminatorlib/util.py index 6bde7b95..b4d8db64 100755 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -18,6 +18,8 @@ import sys import gtk +import os +import pwd # set this to true to enable debugging output DEBUG = True @@ -39,11 +41,57 @@ def gerr(message = None): gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, message) dialog.run() -def has_ancestor(widget, type): +def has_ancestor(widget, wtype): """Walk up the family tree of widget to see if any ancestors are of type""" while widget: widget = widget.get_parent() - if isinstance(widget, type): + if isinstance(widget, wtype): return(True) return(False) +def path_lookup(command): + '''Find a command in our path''' + if os.path.isabs(command): + if os.path.isfile(command): + return(command) + else: + return(None) + elif command[:2] == './' and os.path.isfile(command): + dbg('path_lookup: Relative filename %s found in cwd' % command) + return(command) + + try: + paths = os.environ['PATH'].split(':') + if len(paths[0]) == 0: + raise(ValueError) + except (ValueError, NameError): + dbg('path_lookup: PATH not set in environment, using fallbacks') + paths = ['/usr/local/bin', '/usr/bin', '/bin'] + + dbg('path_lookup: Using %d paths: %s', (len(paths), paths)) + + for path in paths: + target = os.path.join(path, command) + if os.path.isfile(target): + dbg('path_lookup: found %s' % target) + return(target) + + dbg('path_lookup: Unable to locate %s' % command) + +def shell_lookup(): + """Find an appropriate shell for the user""" + shells = [os.getenv('SHELL'), pwd.getpwuid(os.getuid())[6], 'bash', + 'zsh', 'tcsh', 'ksh', 'csh', 'sh'] + + for shell in shells: + if shell is None: + continue + elif os.path.isfile(shell): + return(shell) + else: + rshell = path_lookup(shell) + if rshell is not None: + dbg('shell_lookup: Found %s at %s' % (shell, rshell)) + return(rshell) + dbg('shell_lookup: Unable to locate a shell') +