File: aug_process.py

package info (click to toggle)
augustus 3.4.0%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 758,480 kB
  • sloc: cpp: 65,451; perl: 21,436; python: 3,927; ansic: 1,240; makefile: 1,032; sh: 189; javascript: 32
file content (46 lines) | stat: -rw-r--r-- 1,501 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
#!/usr/bin/env python3

import os
import subprocess
import unittest


def execute(testcase, cmd, out=subprocess.PIPE):
    """ Execute the specified cmd and writes the stdout into a file or return
        as string.
        Throws an assertion exception if the return code of the executed cmd was
        not zero or if stderr is not empty.
        Throws an assertion exception if stdout should be written to a file
        but was not.
    :param testcase: the calling unittest.TestCase or None
    :param cmd: cmd string executed in a shell
    :param out: a file name or None
    :return: an empty string if stdout was writte to a file or the stdout as string
    """
    isFile = isinstance(out, str)
    output = open(out, 'w') if isFile else out

    p = subprocess.Popen(cmd,
                         stdout=output,
                         stderr=subprocess.PIPE,
                         shell=True,
                         universal_newlines=True)
    rc = p.wait()
    error = p.stderr.read()
    p.stderr.close()

    testcase = unittest.TestCase() if testcase is None else testcase
    if error:
        print("error " + error)
    # testcase.assertEqual(error, '', error)
    testcase.assertEqual(rc, 0, f'Return code not 0! Error: {error}' )

    if isFile:
        testcase.assertTrue(os.path.isfile(out),
                            'Output file was not created as expected!')
    else:
        stdout = p.stdout.read()
        p.stdout.close()
        return stdout

    return ''