(trunk-1617) Add fallback to psutils to discover the cwd of a terminal (Heon Jeong)

This commit is contained in:
Stephen Boddy 2015-09-01 23:05:24 +02:00
parent ac53de6afb
commit a65ce100f7
1 changed files with 13 additions and 0 deletions

View File

@ -17,6 +17,13 @@ import os
import pwd import pwd
from util import dbg, err from util import dbg, err
try:
import psutil
psutil_avail = True
except (ImportError):
dbg('psutil not found')
psutil_avail = False
def get_default_cwd(): def get_default_cwd():
"""Determine a reasonable default cwd""" """Determine a reasonable default cwd"""
cwd = os.getcwd() cwd = os.getcwd()
@ -47,6 +54,8 @@ def get_pid_cwd():
elif system == 'SunOS': elif system == 'SunOS':
dbg('Using SunOS get_pid_cwd') dbg('Using SunOS get_pid_cwd')
func = sunos_get_pid_cwd func = sunos_get_pid_cwd
elif psutil_avail:
func = psutil_cwd
else: else:
dbg('Unable to determine a get_pid_cwd for OS: %s' % system) 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""" """Determine the cwd for a given PID on SunOS kernels"""
return(proc_get_pid_cwd(pid, '/proc/%s/path/cwd')) 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: # vim: set expandtab ts=4 sw=4: