mirror of
				https://github.com/HorlogeSkynet/archey4
				synced 2025-11-04 04:00:27 +01:00 
			
		
		
		
	It appears "broken" `PATH` environment variable (e.g. ending with a file instead of a directory) may lead to `NotADirectoryError` Python exception. Let's get bulletproof by extending most of subprocess calls expected exceptions to parent `OSError` (or at least by actually catching `NotADirectoryError` explicitly). > closes #164 Co-Authored-By: Michael Bromilow <developer@bromilow.uk>
		
			
				
	
	
		
			39 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
"""Simple class (acting as a singleton) to handle processes listing"""
 | 
						|
 | 
						|
import logging
 | 
						|
import typing
 | 
						|
from subprocess import PIPE, CalledProcessError, 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):
 | 
						|
        self._processes: typing.List[str]
 | 
						|
 | 
						|
        try:
 | 
						|
            ps_output = check_output(["ps", "-eo", "comm"], stderr=PIPE, universal_newlines=True)
 | 
						|
        except OSError as os_error:
 | 
						|
            self._processes = []
 | 
						|
            logging.warning("`ps` failed or `procps`/`procps-ng` isn't installed : %s", os_error)
 | 
						|
        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)
 |