def assert_can_execute(command_and_arguments, prerequisite, caller, env, no_path_search=False, logger=None): with tempfile.NamedTemporaryFile() as f: try: if IS_WIN and not is_string( command_and_arguments) and not no_path_search: which_cmd = which(command_and_arguments[0], path=env.get("PATH") if env else None) if which_cmd: command_and_arguments[0] = which_cmd if logger: logger.debug( "Verifying command: %s", " ".join(repr(cmd) for cmd in command_and_arguments)) process = subprocess.Popen(command_and_arguments, stdout=f, stderr=f, shell=False, env=env) process.wait() except OSError: raise MissingPrerequisiteException(prerequisite, caller)
def execute_command(command_and_arguments, outfile_name=None, env=None, cwd=None, error_file_name=None, shell=False, no_path_search=False, logger=None): if error_file_name is None and outfile_name: error_file_name = outfile_name + ".err" out_file_created = False error_file_created = False if not hasattr(outfile_name, "write"): outfile_name = open(outfile_name, "w") if outfile_name else None out_file_created = True try: if not hasattr(error_file_name, "write"): error_file_name = open(error_file_name, "w") if error_file_name else None error_file_created = True try: if not shell and IS_WIN and not is_string( command_and_arguments) and not no_path_search: which_cmd = which(command_and_arguments[0], path=env.get("PATH") if env else None) if which_cmd: command_and_arguments[0] = which_cmd if logger: logger.debug( "Executing command: %s", " ".join(repr(cmd) for cmd in command_and_arguments)) process = Popen(command_and_arguments, stdout=outfile_name, stderr=error_file_name, env=env, cwd=cwd, shell=shell) return process.wait() finally: if error_file_name and error_file_created: error_file_name.close() finally: if outfile_name and out_file_created: outfile_name.close()
def run_process_and_wait(self, commands, cwd, stdout, stderr=None, no_path_search=True): if is_windows(self.platform) and not no_path_search: which_cmd = which(commands[0], path=self.environ.get("PATH")) if which_cmd: commands[0] = which_cmd with open(os.devnull) as devnull: process = subprocess.Popen(commands, cwd=cwd, stdin=devnull, stdout=stdout, stderr=stderr or stdout, shell=False) return process.wait()