archey4/archey/processes.py
Samuel FORESTIER f0b82e4efd [CORE] Makes Archey not hard-depends on procps[-ng] package
> Archey won't crash anymore if `ps` could not be found (or its implementation is not supported).
> Please note that without it, many entries may not work as expected.
2021-03-12 10:44:50 +01:00

39 lines
1.3 KiB
Python

"""Simple class (acting as a singleton) to handle processes listing"""
import logging
from subprocess import CalledProcessError, PIPE, check_output
from archey.singleton import Singleton
class Processes(metaclass=Singleton):
"""At startup, instantiate this class to populate a list of running processes"""
def __init__(self):
try:
ps_output = check_output(
['ps', '-eo', 'comm'],
stderr=PIPE, universal_newlines=True
)
except FileNotFoundError:
self._processes = []
logging.warning("`procps` (or `procps-ng`) couldn't be found on your system.")
except CalledProcessError as process_error:
self._processes = []
logging.warning(
"This implementation of `ps` might not be supported : %s", process_error.stderr
)
else:
# Discard first heading line here.
self._processes = ps_output.splitlines()[1:]
@property
def list(self) -> tuple:
"""Simple getter to retrieve (am immutable copy of) the processes list"""
return tuple(self._processes)
@property
def number(self) -> int:
"""Simple getter to retrieve the number of stored processes"""
return len(self._processes)