Robot Framework Integrated Development Environment (RIDE)
process.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 import subprocess
18 import tempfile
19 import time
20 
21 
22 class Process():
23 
24  def __init__(self, command):
25  self._command_command = self._parse_command_parse_command(command)
26  self._process_process = None
27  self._error_error = None
28  self._out_file_out_file = None
29  self._out_path_out_path = None
30  self._out_fd_out_fd = None
31 
32  def _parse_command(self, command):
33  if isinstance(command, str):
34  return [val.replace('<SPACE>', ' ') for val in command.split()]
35  return command
36 
37  def start(self):
38  self._out_fd_out_fd, self._out_path_out_path = \
39  tempfile.mkstemp(prefix='rfproc_', suffix='.txt',
40  text=True)
41  self._out_file_out_file = open(self._out_path_out_path)
42  if not self._command_command:
43  self._error_error = 'The command is missing from this run configuration.'
44  return
45  try:
46  self._process_process = subprocess.Popen(self._command_command, stdout=self._out_fd_out_fd,
47  stderr=subprocess.STDOUT)
48  except OSError as err:
49  self._error_error = str(err)
50 
51  def is_finished(self):
52  return self._error_error is not None or self._process_process.poll() is not None
53 
54  def stop(self):
55  self._process_process.kill()
56 
57  def wait(self):
58  if self._process_process is not None:
59  self._process_process.wait()
60 
61 
70  def get_output(self, wait_until_finished=False):
71  if self._error_error:
72  self._close_outputs_close_outputs()
73  return self._error_error
74  if wait_until_finished:
75  self._process_process.wait()
76  output = self._out_file_out_file.read()
77  if self.is_finishedis_finished():
78  self._close_outputs_close_outputs()
79  return output
80 
81  def _close_outputs(self):
82  self._out_file_out_file.close()
83  os.close(self._out_fd_out_fd)
84  self._remove_tempfile_remove_tempfile()
85 
86  def _remove_tempfile(self, attempts=10):
87  try:
88  os.remove(self._out_path_out_path)
89  except OSError:
90  if not attempts:
91  raise
92  time.sleep(1)
93  self._remove_tempfile_remove_tempfile(attempts - 1)
94 
def _parse_command(self, command)
Definition: process.py:32
def __init__(self, command)
Definition: process.py:24
def _remove_tempfile(self, attempts=10)
Definition: process.py:86
def get_output(self, wait_until_finished=False)
Returns the output produced by the process.
Definition: process.py:70