xref: /llvm-project/openmp/runtime/tools/libomputils.py (revision 88dae3d5d0230747f3cbabdde9ac5ae9e5dc3f8d)
1#
2# //===----------------------------------------------------------------------===//
3# //
4# // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# // See https://llvm.org/LICENSE.txt for license information.
6# // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7# //
8# //===----------------------------------------------------------------------===//
9#
10
11import os
12import subprocess
13import sys
14
15
16class ScriptError(Exception):
17    """Convenience class for user errors generated"""
18
19    def __init__(self, msg):
20        super(Exception, self).__init__(msg)
21
22
23def error(msg):
24    raise ScriptError(msg)
25
26
27def print_line(msg, form="i"):
28    print("{}: ({}) {}".format(os.path.basename(sys.argv[0]), form, msg))
29
30
31def print_info_line(msg):
32    print_line(msg)
33
34
35def print_error_line(msg):
36    print_line(msg, form="x")
37
38
39class RunResult:
40    """
41    Auxiliary class for execute_command() containing the
42    results of running a command
43    """
44
45    def __init__(self, args, stdout, stderr, returncode):
46        self.executable = args[0]
47        self.stdout = stdout.decode("utf-8")
48        self.stderr = stderr.decode("utf-8")
49        self.returncode = returncode
50        self.command = " ".join(args)
51
52
53def execute_command(args):
54    """
55    Run a command with arguments: args
56
57    Return RunResult containing stdout, stderr, returncode
58    """
59    handle = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60    stdout, stderr = handle.communicate()
61    returncode = handle.wait()
62    return RunResult(args, stdout, stderr, returncode)
63
64
65# end of file
66