proxygen
gtest_test_utils.Subprocess Class Reference

Public Member Functions

def __init__ (self, command, working_dir=None, capture_stderr=True, env=None)
 
def __init__ (self, command, working_dir=None, capture_stderr=True, env=None)
 
def __init__ (self, command, working_dir=None, capture_stderr=True, env=None)
 

Public Attributes

 output
 
 terminated_by_signal
 
 exited
 
 signal
 
 exit_code
 

Private Attributes

 _return_code
 

Detailed Description

Definition at line 207 of file gtest_test_utils.py.

Constructor & Destructor Documentation

def gtest_test_utils.Subprocess.__init__ (   self,
  command,
  working_dir = None,
  capture_stderr = True,
  env = None 
)
Changes into a specified directory, if provided, and executes a command.

Restores the old directory afterwards.

Args:
  command:        The command to run, in the form of sys.argv.
  working_dir:    The directory to change into.
  capture_stderr: Determines whether to capture stderr in the output member
              or to discard it.
  env:            Dictionary with environment to pass to the subprocess.

Returns:
  An object that represents outcome of the executed process. It has the
  following attributes:
terminated_by_signal   True iff the child process has been terminated
                       by a signal.
signal                 Sygnal that terminated the child process.
exited                 True iff the child process exited normally.
exit_code              The code with which the child process exited.
output                 Child process's stdout and stderr output
                       combined in a string.

Definition at line 208 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

208  def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
209  """Changes into a specified directory, if provided, and executes a command.
210 
211  Restores the old directory afterwards.
212 
213  Args:
214  command: The command to run, in the form of sys.argv.
215  working_dir: The directory to change into.
216  capture_stderr: Determines whether to capture stderr in the output member
217  or to discard it.
218  env: Dictionary with environment to pass to the subprocess.
219 
220  Returns:
221  An object that represents outcome of the executed process. It has the
222  following attributes:
223  terminated_by_signal True iff the child process has been terminated
224  by a signal.
225  signal Sygnal that terminated the child process.
226  exited True iff the child process exited normally.
227  exit_code The code with which the child process exited.
228  output Child process's stdout and stderr output
229  combined in a string.
230  """
231 
232  # The subprocess module is the preferrable way of running programs
233  # since it is available and behaves consistently on all platforms,
234  # including Windows. But it is only available starting in python 2.4.
235  # In earlier python versions, we revert to the popen2 module, which is
236  # available in python 2.0 and later but doesn't provide required
237  # functionality (Popen4) under Windows. This allows us to support Mac
238  # OS X 10.4 Tiger, which has python 2.3 installed.
239  if _SUBPROCESS_MODULE_AVAILABLE:
240  if capture_stderr:
241  stderr = subprocess.STDOUT
242  else:
243  stderr = subprocess.PIPE
244 
245  p = subprocess.Popen(command,
246  stdout=subprocess.PIPE, stderr=stderr,
247  cwd=working_dir, universal_newlines=True, env=env)
248  # communicate returns a tuple with the file obect for the child's
249  # output.
250  self.output = p.communicate()[0]
251  self._return_code = p.returncode
252  else:
253  old_dir = os.getcwd()
254 
255  def _ReplaceEnvDict(dest, src):
256  # Changes made by os.environ.clear are not inheritable by child
257  # processes until Python 2.6. To produce inheritable changes we have
258  # to delete environment items with the del statement.
259  for key in dest.keys():
260  del dest[key]
261  dest.update(src)
262 
263  # When 'env' is not None, backup the environment variables and replace
264  # them with the passed 'env'. When 'env' is None, we simply use the
265  # current 'os.environ' for compatibility with the subprocess.Popen
266  # semantics used above.
267  if env is not None:
268  old_environ = os.environ.copy()
269  _ReplaceEnvDict(os.environ, env)
270 
271  try:
272  if working_dir is not None:
273  os.chdir(working_dir)
274  if capture_stderr:
275  p = popen2.Popen4(command)
276  else:
277  p = popen2.Popen3(command)
278  p.tochild.close()
279  self.output = p.fromchild.read()
280  ret_code = p.wait()
281  finally:
282  os.chdir(old_dir)
283 
284  # Restore the old environment variables
285  # if they were replaced.
286  if env is not None:
287  _ReplaceEnvDict(os.environ, old_environ)
288 
289  # Converts ret_code to match the semantics of
290  # subprocess.Popen.returncode.
291  if os.WIFSIGNALED(ret_code):
292  self._return_code = -os.WTERMSIG(ret_code)
293  else: # os.WIFEXITED(ret_code) should return True here.
294  self._return_code = os.WEXITSTATUS(ret_code)
295 
296  if self._return_code < 0:
298  self.exited = False
299  self.signal = -self._return_code
300  else:
301  self.terminated_by_signal = False
302  self.exited = True
304 
305 
def __init__(self, command, working_dir=None, capture_stderr=True, env=None)
def gtest_test_utils.Subprocess.__init__ (   self,
  command,
  working_dir = None,
  capture_stderr = True,
  env = None 
)
Changes into a specified directory, if provided, and executes a command.

Restores the old directory afterwards.

Args:
  command:        The command to run, in the form of sys.argv.
  working_dir:    The directory to change into.
  capture_stderr: Determines whether to capture stderr in the output member
              or to discard it.
  env:            Dictionary with environment to pass to the subprocess.

Returns:
  An object that represents outcome of the executed process. It has the
  following attributes:
terminated_by_signal   True iff the child process has been terminated
                       by a signal.
signal                 Sygnal that terminated the child process.
exited                 True iff the child process exited normally.
exit_code              The code with which the child process exited.
output                 Child process's stdout and stderr output
                       combined in a string.

Definition at line 208 of file gtest_test_utils.py.

References gtest_test_utils.Subprocess.__init__(), gtest_test_utils._ParseAndStripGTestFlags(), gtest_test_utils.Subprocess._return_code, gtest_test_utils.Subprocess.exit_code, gtest_test_utils.Subprocess.exited, gtest_test_utils.Main(), gtest_test_utils.Subprocess.output, gtest_test_utils.Subprocess.signal, and gtest_test_utils.Subprocess.terminated_by_signal.

208  def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
209  """Changes into a specified directory, if provided, and executes a command.
210 
211  Restores the old directory afterwards.
212 
213  Args:
214  command: The command to run, in the form of sys.argv.
215  working_dir: The directory to change into.
216  capture_stderr: Determines whether to capture stderr in the output member
217  or to discard it.
218  env: Dictionary with environment to pass to the subprocess.
219 
220  Returns:
221  An object that represents outcome of the executed process. It has the
222  following attributes:
223  terminated_by_signal True iff the child process has been terminated
224  by a signal.
225  signal Sygnal that terminated the child process.
226  exited True iff the child process exited normally.
227  exit_code The code with which the child process exited.
228  output Child process's stdout and stderr output
229  combined in a string.
230  """
231 
232  # The subprocess module is the preferrable way of running programs
233  # since it is available and behaves consistently on all platforms,
234  # including Windows. But it is only available starting in python 2.4.
235  # In earlier python versions, we revert to the popen2 module, which is
236  # available in python 2.0 and later but doesn't provide required
237  # functionality (Popen4) under Windows. This allows us to support Mac
238  # OS X 10.4 Tiger, which has python 2.3 installed.
239  if _SUBPROCESS_MODULE_AVAILABLE:
240  if capture_stderr:
241  stderr = subprocess.STDOUT
242  else:
243  stderr = subprocess.PIPE
244 
245  p = subprocess.Popen(command,
246  stdout=subprocess.PIPE, stderr=stderr,
247  cwd=working_dir, universal_newlines=True, env=env)
248  # communicate returns a tuple with the file obect for the child's
249  # output.
250  self.output = p.communicate()[0]
251  self._return_code = p.returncode
252  else:
253  old_dir = os.getcwd()
254 
255  def _ReplaceEnvDict(dest, src):
256  # Changes made by os.environ.clear are not inheritable by child
257  # processes until Python 2.6. To produce inheritable changes we have
258  # to delete environment items with the del statement.
259  for key in dest.keys():
260  del dest[key]
261  dest.update(src)
262 
263  # When 'env' is not None, backup the environment variables and replace
264  # them with the passed 'env'. When 'env' is None, we simply use the
265  # current 'os.environ' for compatibility with the subprocess.Popen
266  # semantics used above.
267  if env is not None:
268  old_environ = os.environ.copy()
269  _ReplaceEnvDict(os.environ, env)
270 
271  try:
272  if working_dir is not None:
273  os.chdir(working_dir)
274  if capture_stderr:
275  p = popen2.Popen4(command)
276  else:
277  p = popen2.Popen3(command)
278  p.tochild.close()
279  self.output = p.fromchild.read()
280  ret_code = p.wait()
281  finally:
282  os.chdir(old_dir)
283 
284  # Restore the old environment variables
285  # if they were replaced.
286  if env is not None:
287  _ReplaceEnvDict(os.environ, old_environ)
288 
289  # Converts ret_code to match the semantics of
290  # subprocess.Popen.returncode.
291  if os.WIFSIGNALED(ret_code):
292  self._return_code = -os.WTERMSIG(ret_code)
293  else: # os.WIFEXITED(ret_code) should return True here.
294  self._return_code = os.WEXITSTATUS(ret_code)
295 
296  if self._return_code < 0:
297  self.terminated_by_signal = True
298  self.exited = False
299  self.signal = -self._return_code
300  else:
301  self.terminated_by_signal = False
302  self.exited = True
303  self.exit_code = self._return_code
304 
305 
def __init__(self, command, working_dir=None, capture_stderr=True, env=None)
def gtest_test_utils.Subprocess.__init__ (   self,
  command,
  working_dir = None,
  capture_stderr = True,
  env = None 
)
Changes into a specified directory, if provided, and executes a command.

Restores the old directory afterwards.

Args:
  command:        The command to run, in the form of sys.argv.
  working_dir:    The directory to change into.
  capture_stderr: Determines whether to capture stderr in the output member
              or to discard it.
  env:            Dictionary with environment to pass to the subprocess.

Returns:
  An object that represents outcome of the executed process. It has the
  following attributes:
terminated_by_signal   True iff the child process has been terminated
                       by a signal.
signal                 Sygnal that terminated the child process.
exited                 True iff the child process exited normally.
exit_code              The code with which the child process exited.
output                 Child process's stdout and stderr output
                       combined in a string.

Definition at line 208 of file gtest_test_utils.py.

References gtest_test_utils.Subprocess.__init__(), gtest_test_utils._ParseAndStripGTestFlags(), gtest_test_utils.Subprocess._return_code, gtest_test_utils.Subprocess.exit_code, gtest_test_utils.Subprocess.exited, gtest_test_utils.Main(), gtest_test_utils.Subprocess.output, gtest_test_utils.Subprocess.signal, and gtest_test_utils.Subprocess.terminated_by_signal.

208  def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
209  """Changes into a specified directory, if provided, and executes a command.
210 
211  Restores the old directory afterwards.
212 
213  Args:
214  command: The command to run, in the form of sys.argv.
215  working_dir: The directory to change into.
216  capture_stderr: Determines whether to capture stderr in the output member
217  or to discard it.
218  env: Dictionary with environment to pass to the subprocess.
219 
220  Returns:
221  An object that represents outcome of the executed process. It has the
222  following attributes:
223  terminated_by_signal True iff the child process has been terminated
224  by a signal.
225  signal Sygnal that terminated the child process.
226  exited True iff the child process exited normally.
227  exit_code The code with which the child process exited.
228  output Child process's stdout and stderr output
229  combined in a string.
230  """
231 
232  # The subprocess module is the preferrable way of running programs
233  # since it is available and behaves consistently on all platforms,
234  # including Windows. But it is only available starting in python 2.4.
235  # In earlier python versions, we revert to the popen2 module, which is
236  # available in python 2.0 and later but doesn't provide required
237  # functionality (Popen4) under Windows. This allows us to support Mac
238  # OS X 10.4 Tiger, which has python 2.3 installed.
239  if _SUBPROCESS_MODULE_AVAILABLE:
240  if capture_stderr:
241  stderr = subprocess.STDOUT
242  else:
243  stderr = subprocess.PIPE
244 
245  p = subprocess.Popen(command,
246  stdout=subprocess.PIPE, stderr=stderr,
247  cwd=working_dir, universal_newlines=True, env=env)
248  # communicate returns a tuple with the file obect for the child's
249  # output.
250  self.output = p.communicate()[0]
251  self._return_code = p.returncode
252  else:
253  old_dir = os.getcwd()
254 
255  def _ReplaceEnvDict(dest, src):
256  # Changes made by os.environ.clear are not inheritable by child
257  # processes until Python 2.6. To produce inheritable changes we have
258  # to delete environment items with the del statement.
259  for key in dest.keys():
260  del dest[key]
261  dest.update(src)
262 
263  # When 'env' is not None, backup the environment variables and replace
264  # them with the passed 'env'. When 'env' is None, we simply use the
265  # current 'os.environ' for compatibility with the subprocess.Popen
266  # semantics used above.
267  if env is not None:
268  old_environ = os.environ.copy()
269  _ReplaceEnvDict(os.environ, env)
270 
271  try:
272  if working_dir is not None:
273  os.chdir(working_dir)
274  if capture_stderr:
275  p = popen2.Popen4(command)
276  else:
277  p = popen2.Popen3(command)
278  p.tochild.close()
279  self.output = p.fromchild.read()
280  ret_code = p.wait()
281  finally:
282  os.chdir(old_dir)
283 
284  # Restore the old environment variables
285  # if they were replaced.
286  if env is not None:
287  _ReplaceEnvDict(os.environ, old_environ)
288 
289  # Converts ret_code to match the semantics of
290  # subprocess.Popen.returncode.
291  if os.WIFSIGNALED(ret_code):
292  self._return_code = -os.WTERMSIG(ret_code)
293  else: # os.WIFEXITED(ret_code) should return True here.
294  self._return_code = os.WEXITSTATUS(ret_code)
295 
296  if self._return_code < 0:
297  self.terminated_by_signal = True
298  self.exited = False
299  self.signal = -self._return_code
300  else:
301  self.terminated_by_signal = False
302  self.exited = True
303  self.exit_code = self._return_code
304 
305 
def __init__(self, command, working_dir=None, capture_stderr=True, env=None)

Member Data Documentation

gtest_test_utils.Subprocess._return_code
private

Definition at line 251 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

gtest_test_utils.Subprocess.exit_code

Definition at line 303 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

gtest_test_utils.Subprocess.exited

Definition at line 298 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

gtest_test_utils.Subprocess.output

Definition at line 250 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

gtest_test_utils.Subprocess.signal

Definition at line 299 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().

gtest_test_utils.Subprocess.terminated_by_signal

Definition at line 297 of file gtest_test_utils.py.

Referenced by gtest_test_utils.Subprocess.__init__().


The documentation for this class was generated from the following file: