Runs the specified command as a subprocess. By default the call is non-blocking. The function will immediately return without waiting for the process to complete running. You can use the returned Popen object to communicate with the subprocess and get the results. Returns a subproce
(command, *args, wait=False, cwd=None, pipe_output=False, merge_stderr_with_stdout=True, stdin=None)
| 23133 | |
| 23134 | |
| 23135 | def execute_command_subprocess(command, *args, wait=False, cwd=None, pipe_output=False, merge_stderr_with_stdout=True, stdin=None): |
| 23136 | """ |
| 23137 | Runs the specified command as a subprocess. |
| 23138 | By default the call is non-blocking. |
| 23139 | The function will immediately return without waiting for the process to complete running. You can use the returned Popen object to communicate with the subprocess and get the results. |
| 23140 | Returns a subprocess Popen object. |
| 23141 | |
| 23142 | :param command: The command/file to execute. What you would type at a console to run a program or shell command. |
| 23143 | :type command: (str) |
| 23144 | :param *args: Variable number of arguments that are passed to the program being started as command line parms |
| 23145 | :type *args: (Any) |
| 23146 | :param wait: If True then wait for the subprocess to finish |
| 23147 | :type wait: (bool) |
| 23148 | :param cwd: Working directory to use when executing the subprocess |
| 23149 | :type cwd: (str)) |
| 23150 | :param pipe_output: If True then output from the subprocess will be piped. You MUST empty the pipe by calling execute_get_results or your subprocess will block until no longer full |
| 23151 | :type pipe_output: (bool) |
| 23152 | :param merge_stderr_with_stdout: If True then output from the subprocess stderr will be merged with stdout. The result is ALL output will be on stdout. |
| 23153 | :type merge_stderr_with_stdout: (bool) |
| 23154 | :param stdin: Value passed to the Popen call. Defaults to subprocess.DEVNULL so that the pyinstaller created executable work correctly |
| 23155 | :type stdin: (bool) |
| 23156 | :return: Popen object |
| 23157 | :rtype: (subprocess.Popen) |
| 23158 | """ |
| 23159 | if stdin is None: |
| 23160 | stdin = subprocess.DEVNULL |
| 23161 | if command == '' or command is None: |
| 23162 | return None |
| 23163 | try: |
| 23164 | if args is not None: |
| 23165 | expanded_args = ' '.join(args) |
| 23166 | # print('executing subprocess command:',command, 'args:',expanded_args) |
| 23167 | if command[0] != '"' and ' ' in command and (r'/' in command or '\\' in command): |
| 23168 | command = '"' + command + '"' |
| 23169 | # print('calling popen with:', command +' '+ expanded_args) |
| 23170 | # sp = subprocess.Popen(command +' '+ expanded_args, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=cwd) |
| 23171 | if pipe_output: |
| 23172 | if merge_stderr_with_stdout: |
| 23173 | sp = subprocess.Popen(command + ' ' + expanded_args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, stdin=stdin) |
| 23174 | else: |
| 23175 | sp = subprocess.Popen(command + ' ' + expanded_args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, stdin=stdin) |
| 23176 | else: |
| 23177 | sp = subprocess.Popen(command + ' ' + expanded_args, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=cwd, stdin=stdin) |
| 23178 | else: |
| 23179 | sp = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, stdin=stdin) |
| 23180 | if wait: |
| 23181 | out, err = sp.communicate() |
| 23182 | if out: |
| 23183 | print(out.decode("utf-8")) |
| 23184 | if err: |
| 23185 | print(err.decode("utf-8")) |
| 23186 | except Exception as e: |
| 23187 | warnings.warn('Error in execute_command_subprocess {}'.format(e), UserWarning) |
| 23188 | _error_popup_with_traceback('Error in execute_command_subprocess', e, 'command={}'.format(command), 'args={}'.format(args), 'cwd={}'.format(cwd)) |
| 23189 | sp = None |
| 23190 | return sp |
| 23191 | |
| 23192 |
no test coverage detected