cornsnake.util_proc
Running processes and opening Windows Explorer at a specified directory.
1""" 2Running processes and opening Windows Explorer at a specified directory. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_proc.html) 5""" 6 7import os 8import subprocess 9 10from . import config 11from . import util_log 12from . import util_os 13 14logger = util_log.getLogger(__name__) 15 16 17def _proc_print(message: str) -> None: 18 """ 19 Logs the message using the logger and prints it if config.IS_VERBOSE is True. 20 21 Args: 22 message (str): The message to log and possibly print. 23 """ 24 logger.info(message) 25 if config.IS_VERBOSE: 26 print(message) 27 28 29def _proc_print_debug(message: str) -> None: 30 """ 31 Logs the message at debug level using the logger and prints it if config.IS_VERBOSE is True. 32 33 Args: 34 message (str): The message to log and possibly print. 35 """ 36 logger.debug(message) 37 if config.IS_VERBOSE: 38 print(message) 39 40 41def is_process_running(process_name: str) -> bool: 42 """ 43 Checks if the given process (identified by program filename) is running. 44 45 Args: 46 process_name (str): The filename of the process. For example 'notepad.exe'. 47 """ 48 if util_os.is_windows(): 49 progs = str(subprocess.check_output("tasklist")) 50 return process_name in progs 51 else: 52 try: 53 progs = str(subprocess.check_output(["pgrep", process_name])) 54 return process_name in progs 55 except subprocess.CalledProcessError: 56 return False 57 58 59def run_process_and_get_output( 60 path_to_proc: str, 61 arguments: list[str], 62 working_directory: str, 63 output_errors: bool = True, 64) -> str: 65 """ 66 Executes a process with specified arguments and working directory, capturing the output. 67 68 Args: 69 path_to_proc (str): The path to the process to execute. 70 arguments (list): List of arguments for the process. 71 working_directory (str): The working directory for the process. 72 output_errors (bool): Flag to output errors. Default is True. 73 74 Returns: 75 str: The standard output of the process. 76 77 Raises: 78 RuntimeError: If the process returns a non-zero exit code. 79 """ 80 _proc_print_debug( 81 f"EXECUTING: '{path_to_proc} {arguments}' - at {working_directory}" 82 ) 83 84 pipes = subprocess.Popen( 85 [path_to_proc] + arguments, 86 stdout=subprocess.PIPE, 87 stderr=subprocess.PIPE, 88 cwd=working_directory, 89 ) 90 std_out, std_err = pipes.communicate() 91 92 std_out_str = "" 93 if isinstance(std_out, str): 94 std_out_str = std_out 95 else: 96 std_out_str = std_out.decode() 97 98 std_err_str = "" 99 if isinstance(std_err, str): 100 std_err_str = std_err 101 else: 102 std_err_str = std_err.decode() 103 104 if pipes.returncode != 0: 105 # an error happened! 106 err_msg = f"{std_err_str.strip()}. Code: {pipes.returncode}" 107 if output_errors: 108 print(err_msg) 109 else: 110 logger.debug(err_msg) 111 raise RuntimeError(err_msg, pipes.returncode) 112 elif len(std_err_str): 113 # return code is 0 (no error), but we may want to 114 # do something with the info on std_err 115 if output_errors: 116 print(std_err_str) 117 else: 118 logger.debug(std_err_str) 119 120 _proc_print_debug(f" >>> {std_out_str}") 121 122 return std_out_str 123 124 125def open_windows_explorer_at(path_to_dir: str) -> None: 126 """ 127 Opens Windows Explorer or Mac Finder at the specified directory if the OS is Windows. 128 129 Args: 130 path_to_dir (str): The path to the directory to open in Windows Explorer. 131 """ 132 if util_os.is_windows(): 133 _proc_print(f"Opening Explorer at {path_to_dir}") 134 os.startfile(path_to_dir) 135 return 136 if util_os.is_mac(): 137 _proc_print(f"Opening Finder at {path_to_dir}") 138 os.system(f"open {path_to_dir}") 139 return 140 else: 141 _proc_print(f"NOT opening {path_to_dir} - not Windows OS and not Mac")
42def is_process_running(process_name: str) -> bool: 43 """ 44 Checks if the given process (identified by program filename) is running. 45 46 Args: 47 process_name (str): The filename of the process. For example 'notepad.exe'. 48 """ 49 if util_os.is_windows(): 50 progs = str(subprocess.check_output("tasklist")) 51 return process_name in progs 52 else: 53 try: 54 progs = str(subprocess.check_output(["pgrep", process_name])) 55 return process_name in progs 56 except subprocess.CalledProcessError: 57 return False
Checks if the given process (identified by program filename) is running.
Args: process_name (str): The filename of the process. For example 'notepad.exe'.
60def run_process_and_get_output( 61 path_to_proc: str, 62 arguments: list[str], 63 working_directory: str, 64 output_errors: bool = True, 65) -> str: 66 """ 67 Executes a process with specified arguments and working directory, capturing the output. 68 69 Args: 70 path_to_proc (str): The path to the process to execute. 71 arguments (list): List of arguments for the process. 72 working_directory (str): The working directory for the process. 73 output_errors (bool): Flag to output errors. Default is True. 74 75 Returns: 76 str: The standard output of the process. 77 78 Raises: 79 RuntimeError: If the process returns a non-zero exit code. 80 """ 81 _proc_print_debug( 82 f"EXECUTING: '{path_to_proc} {arguments}' - at {working_directory}" 83 ) 84 85 pipes = subprocess.Popen( 86 [path_to_proc] + arguments, 87 stdout=subprocess.PIPE, 88 stderr=subprocess.PIPE, 89 cwd=working_directory, 90 ) 91 std_out, std_err = pipes.communicate() 92 93 std_out_str = "" 94 if isinstance(std_out, str): 95 std_out_str = std_out 96 else: 97 std_out_str = std_out.decode() 98 99 std_err_str = "" 100 if isinstance(std_err, str): 101 std_err_str = std_err 102 else: 103 std_err_str = std_err.decode() 104 105 if pipes.returncode != 0: 106 # an error happened! 107 err_msg = f"{std_err_str.strip()}. Code: {pipes.returncode}" 108 if output_errors: 109 print(err_msg) 110 else: 111 logger.debug(err_msg) 112 raise RuntimeError(err_msg, pipes.returncode) 113 elif len(std_err_str): 114 # return code is 0 (no error), but we may want to 115 # do something with the info on std_err 116 if output_errors: 117 print(std_err_str) 118 else: 119 logger.debug(std_err_str) 120 121 _proc_print_debug(f" >>> {std_out_str}") 122 123 return std_out_str
Executes a process with specified arguments and working directory, capturing the output.
Args: path_to_proc (str): The path to the process to execute. arguments (list): List of arguments for the process. working_directory (str): The working directory for the process. output_errors (bool): Flag to output errors. Default is True.
Returns: str: The standard output of the process.
Raises: RuntimeError: If the process returns a non-zero exit code.
126def open_windows_explorer_at(path_to_dir: str) -> None: 127 """ 128 Opens Windows Explorer or Mac Finder at the specified directory if the OS is Windows. 129 130 Args: 131 path_to_dir (str): The path to the directory to open in Windows Explorer. 132 """ 133 if util_os.is_windows(): 134 _proc_print(f"Opening Explorer at {path_to_dir}") 135 os.startfile(path_to_dir) 136 return 137 if util_os.is_mac(): 138 _proc_print(f"Opening Finder at {path_to_dir}") 139 os.system(f"open {path_to_dir}") 140 return 141 else: 142 _proc_print(f"NOT opening {path_to_dir} - not Windows OS and not Mac")
Opens Windows Explorer or Mac Finder at the specified directory if the OS is Windows.
Args: path_to_dir (str): The path to the directory to open in Windows Explorer.