Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | import os |
| 2 | |
| 3 | # Copyright (c) 2020, ARM Limited. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import json |
| 18 | import shlex |
| 19 | import subprocess |
| 20 | from enum import IntEnum, unique |
| 21 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 22 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 23 | def run_sh_command(args, full_cmd, capture_output=False): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 24 | """Utility function to run an external command. Optionally return captured stdout/stderr""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 25 | |
| 26 | # Quote the command line for printing |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 27 | full_cmd_esc = [shlex.quote(x) for x in full_cmd] |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 28 | |
| 29 | if args.verbose: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 30 | print("### Running {}".format(" ".join(full_cmd_esc))) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 31 | |
| 32 | if capture_output: |
| 33 | rc = subprocess.run(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 34 | if rc.returncode != 0: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 35 | print(rc.stdout.decode("utf-8")) |
| 36 | print(rc.stderr.decode("utf-8")) |
| 37 | raise Exception( |
| 38 | "Error running command: {}.\n{}".format( |
| 39 | " ".join(full_cmd_esc), rc.stderr.decode("utf-8") |
| 40 | ) |
| 41 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 42 | return (rc.stdout, rc.stderr) |
| 43 | else: |
| 44 | rc = subprocess.run(full_cmd) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 45 | |
| 46 | return rc.returncode |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 47 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 48 | |
| 49 | class TosaTestRunner: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 50 | def __init__(self, args, runnerArgs, testDir): |
| 51 | |
| 52 | self.args = args |
| 53 | self.runnerArgs = runnerArgs |
| 54 | self.testDir = testDir |
| 55 | |
| 56 | # Load the json test file |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 57 | with open(os.path.join(testDir, "desc.json"), "r") as fd: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 58 | self.testDesc = json.load(fd) |
| 59 | |
| 60 | def runModel(self): |
| 61 | pass |
| 62 | |
| 63 | class Result(IntEnum): |
| 64 | EXPECTED_PASS = 0 |
| 65 | EXPECTED_FAILURE = 1 |
| 66 | UNEXPECTED_PASS = 2 |
| 67 | UNEXPECTED_FAILURE = 3 |
| 68 | INTERNAL_ERROR = 4 |