def cast_str_2_list(s_input: str, keep_empty_list_items: bool = True) -> List[Any]: """ >>> cast_str_2_list('a, "x, y" , b') ['a', 'x, y', 'b'] >>> cast_str_2_list('a') ['a'] >>> cast_str_2_list('a, b') ['a', 'b'] >>> cast_str_2_list('a, b') ['a', 'b'] >>> cast_str_2_list('a, , b') ['a', '', 'b'] >>> cast_str_2_list('a, , b', keep_empty_list_items=False) ['a', 'b'] >>> cast_str_2_list('"a, , b"') ['a, , b'] >>> cast_str_2_list('a, x y , b') ['a', 'x y', 'b'] >>> cast_str_2_list('a, "x, y" , b') ['a', 'x, y', 'b'] >>> cast_str_2_list('a, "x,y" , b') ['a', 'x,y', 'b'] """ items = lib_csv.cast_csv_2_list(csv_str=s_input) items = lib_list.ls_strip_elements(items) if not keep_empty_list_items: items = lib_list.ls_del_empty_elements(items) return items
def prepend_path_to_wine_registry_path(path_to_add: Union[str, pathlib.WindowsPath], wine_prefix: Union[str, pathlib.Path] = configmagick_linux.get_path_home_dir_current_user() / '.wine', username: str = configmagick_linux.get_current_username()) -> None: """ >>> install_wine_machine.create_wine_test_prefixes() >>> old_path = get_wine_registry_path(wine_prefix='wine_test_32') >>> prepend_path_to_wine_registry_path(path_to_add='c:\\\\test',wine_prefix='wine_test_32') >>> assert get_wine_registry_path(wine_prefix='wine_test_32').startswith('c:\\\\test;') >>> write_wine_registry_path(path=old_path, wine_prefix='wine_test_32') >>> assert get_wine_registry_path(wine_prefix='wine_test_32') == old_path """ wine_prefix = get_and_check_wine_prefix(wine_prefix=wine_prefix, username=username) current_wine_registry_path = get_wine_registry_path(wine_prefix=wine_prefix, username=username) s_path_to_add = str(path_to_add).strip() l_current_wine_registry_path = current_wine_registry_path.split(';') l_current_wine_registry_path = lib_list.ls_strip_elements(l_current_wine_registry_path) # the path must not end with \\ , because if we set it this might escape the last " !!! # like : wine reg add "..." /t "REG_EXPAND_SZ" /v "PATH" /d "c:\test\" /f leads to : /bin/sh: 1: Syntax error: Unterminated quoted string l_current_wine_registry_path = lib_list.ls_rstrip_elements(l_current_wine_registry_path, '\\') l_current_wine_registry_path = lib_list.del_double_elements(l_current_wine_registry_path) if not lib_list.is_list_element_fnmatching(ls_elements=l_current_wine_registry_path, s_fnmatch_searchpattern=s_path_to_add): l_current_wine_registry_path = [s_path_to_add] + l_current_wine_registry_path new_wine_registry_path = ';'.join(l_current_wine_registry_path) write_wine_registry_path(path=new_wine_registry_path, wine_prefix=wine_prefix, username=username)
def get_l_commandline_from_psutil_process( process: psutil.Process) -> List[str]: """ if there are blanks in the parameters, psutil.cmdline does not work correctly on linux, even if they are '\x00' separated see Error Report for PSUTIL : https://github.com/giampaolo/psutil/issues/1179 sometimes the parameters are separated with blanks, in that case we use shlex in Linux for instance postgrey, or some other scripts started with systemd services that happens also on some windows programs >>> # test the "good" commandline, '\x00' terminated and '\x00' separated >>> import getpass >>> import importlib >>> import importlib.util >>> if lib_platform.is_platform_linux: ... process = subprocess.Popen(['nano', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['nano', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() ... save_actual_directory = str(pathlib.Path().cwd().absolute()) ... test_directory = lib_path.get_test_directory_path('lib_shell', test_directory_name='tests') ... os.chdir(str(test_directory)) ... # for travis we need to be owner - otherwise we get permission error ... lib_path.make_test_directory_and_subdirs_fully_accessible_by_current_user(test_directory) ... process = subprocess.Popen(['./test test/test test.sh', './test test/some_parameter', 'p1', 'p2']) ... psutil_process=psutil.Process(process.pid) ... expected = ['/bin/bash', './test test/test test.sh', './test test/some_parameter', 'p1', 'p2'] ... assert get_l_commandline_from_psutil_process(psutil_process) == expected ... psutil_process.kill() ... os.chdir(save_actual_directory) ... elif lib_platform.is_platform_darwin: ... process = subprocess.Popen(['open', '-a', 'TextEdit', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['open', '-a', 'TextEdit', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() ... else: ... process = subprocess.Popen(['notepad', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['notepad', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() """ if lib_platform.is_platform_linux: with open(f'/proc/{process.pid}/cmdline', mode='r') as proc_commandline: l_commands = proc_commandline.read().split('\x00') else: l_commands = process.cmdline() l_commands = lib_list.ls_strip_elements(l_commands) l_commands = lib_list.ls_del_empty_elements(l_commands) if len(l_commands) == 1: # pragma: no cover s_command = l_commands[0] # for the case the command executable contains blank, the part after the blank would be interpreted as parameter # for instance "/home/user/test test.sh parameter1 parameter2" if lib_platform.is_platform_linux: s_command = get_quoted_command(s_command, process) l_commands = lib_shell_shlex.shlex_split_multi_platform( s_command) # pragma: no cover return l_commands
def delete_empty_lines(text: str) -> str: ls_lines = text.split('\n') ls_lines = lib_list.ls_strip_elements(ls_lines) ls_lines = lib_list.ls_del_empty_elements(ls_lines) text_result = '\n'.join(ls_lines) return text_result