Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rely on python psutil to get the cwd #83

Merged
merged 2 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 4 additions & 55 deletions terminatorlib/cwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@
>>> cwd.__class__.__name__
'str'
>>> func = get_pid_cwd()
>>> func.__class__.__name__
'function'
>>> cwd.__class__.__name__
'str'

"""

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.