From 6dd9c82347261b41363fbde479ce79f8f97ad9a2 Mon Sep 17 00:00:00 2001 From: Stephen Boddy Date: Thu, 13 Aug 2015 22:47:15 +0200 Subject: [PATCH] Add fallback to psutils to discover the cwd of a terminal (Heon Jeong) --- terminatorlib/cwd.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/terminatorlib/cwd.py b/terminatorlib/cwd.py index 7b17d84b..8cb6c197 100755 --- a/terminatorlib/cwd.py +++ b/terminatorlib/cwd.py @@ -17,6 +17,13 @@ import os import pwd from util import dbg, err +try: + import psutil + psutil_avail = True +except (ImportError): + dbg('psutil not found') + psutil_avail = False + def get_default_cwd(): """Determine a reasonable default cwd""" cwd = os.getcwd() @@ -47,6 +54,8 @@ def get_pid_cwd(): elif system == 'SunOS': dbg('Using SunOS get_pid_cwd') func = sunos_get_pid_cwd + elif psutil_avail: + func = psutil_cwd else: dbg('Unable to determine a get_pid_cwd for OS: %s' % system) @@ -71,4 +80,8 @@ def sunos_get_pid_cwd(pid): """Determine the cwd for a given PID on SunOS kernels""" return(proc_get_pid_cwd(pid, '/proc/%s/path/cwd')) +def psutil_cwd(pid): + """Determine the cwd using psutil which also supports Darwin""" + return psutil.Process(pid).as_dict()['cwd'] + # vim: set expandtab ts=4 sw=4: