File: cmd_helper.py

package info (click to toggle)
memkind 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,508 kB
  • sloc: ansic: 72,572; cpp: 39,493; sh: 4,594; perl: 4,250; xml: 2,044; python: 1,753; makefile: 1,393; csh: 7
file content (35 lines) | stat: -rw-r--r-- 1,093 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
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (C) 2016 - 2021 Intel Corporation.

import os
import tempfile
import subprocess


class CMD_helper(object):

    def execute_cmd(self, command, sudo=False):
        if sudo:
            command = "sudo {0}".format(command)
        # Initialize temp file for stdout. Will be removed when closed.
        outfile = tempfile.SpooledTemporaryFile()
        try:
            # Invoke process
            p = subprocess.Popen(
                command, stdout=outfile, stderr=subprocess.STDOUT, shell=True)
            p.communicate()
            # Read stdout from file
            outfile.seek(0)
            stdout = outfile.read()
        except subprocess.CalledProcessError:
            raise
        finally:
            # Make sure the file is closed
            outfile.close()
        retcode = p.returncode
        return stdout.decode("utf-8"), retcode

    def get_command_path(self, binary):
        """Get the path to the binary."""
        path = os.path.dirname(os.path.abspath(__file__))
        return os.path.join(path, binary)