File: subprocessExtras.py

package info (click to toggle)
subuser 0.6.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 4,208 kB
  • sloc: python: 5,201; sh: 380; makefile: 73
file content (60 lines) | stat: -rwxr-xr-x 1,580 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-

"""
Helper functions for running foreign executables.
"""

#external imports
import subprocess
import os
import tempfile
#internal imports
import subuserlib.test

def call(args,cwd=None):
  """
  Same as subprocess.call except here you can specify the cwd.

  Returns subprocess's exit code.
  """
  process = subprocess.Popen(args,cwd=cwd)
  (stdout,stderr) = process.communicate()
  return process.returncode

def callBackground(args,cwd=None,suppressOutput=True,collectStdout=False,collectStderr=False):
  """
  Same as subprocess.call except here you can specify the cwd.
  Returns imediately with the subprocess
  """
  stdout = None
  stderr = None

  if suppressOutput:
    devnull = open(os.devnull,"a")
    stdout = devnull
    stderr = devnull

  if collectStdout:
    temp_stdout = tempfile.TemporaryFile(mode="r")
    stdout = temp_stdout.fileno()
  if collectStderr:
    temp_stderr = tempfile.TemporaryFile(mode="r")
    stderr = temp_stderr.fileno()

  process = subprocess.Popen(args,cwd=cwd,stdout=stdout,stderr=stderr,close_fds=True)

  if collectStdout:
    process.stdout_file = temp_stdout
  if collectStderr:
    process.stderr_file = temp_stderr

  return process

def callCollectOutput(args,cwd=None):
  """
  Run the command and return a tuple with: (returncode,the output to stdout as a string,stderr as a string).
  """
  process = subprocess.Popen(args,stdout=subprocess.PIPE,stderr=subprocess.PIPE,cwd=cwd)
  (stdout,stderr) = process.communicate()
  return (process.returncode,stdout.decode("utf-8"),stderr.decode("utf-8"))