Skip to content

Commit

Permalink
rely on python psutil to get the cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrose committed May 15, 2020
1 parent 2db730f commit 40ba659
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 147 deletions.
55 changes: 2 additions & 53 deletions terminatorlib/cwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@
import platform
import os
import pwd
import psutil
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"""
try:
Expand All @@ -35,51 +29,6 @@ def get_default_cwd():

def get_pid_cwd():
"""Determine an appropriate cwd function for the OS we are running on"""

func = lambda pid: None
system = platform.system()

if system == 'Linux':
dbg('Using Linux get_pid_cwd')
func = linux_get_pid_cwd
elif system == 'FreeBSD':
try:
from . import freebsd
func = freebsd.get_process_cwd
dbg('Using FreeBSD get_pid_cwd')
except (OSError, NotImplementedError, ImportError):
dbg('FreeBSD version too old for 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)

return(func)

def proc_get_pid_cwd(pid, path):
"""Extract the cwd of a PID from proc, given the PID and the /proc path to
insert it into, e.g. /proc/%s/cwd"""
try:
cwd = os.path.realpath(path % pid)
except Exception as ex:
err('Unable to get cwd for PID %s: %s' % (pid, ex))
cwd = '/'

return(cwd)

def linux_get_pid_cwd(pid):
"""Determine the cwd for a given PID on Linux kernels"""
return(proc_get_pid_cwd(pid, '/proc/%s/cwd'))

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']
return psutil.Process().as_dict()['cwd']

# vim: set expandtab ts=4 sw=4:
94 changes: 0 additions & 94 deletions terminatorlib/freebsd.py

This file was deleted.

0 comments on commit 40ba659

Please sign in to comment.