Skip to content

Commit

Permalink
Seperate stdout and stderr logs, use asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
themperek committed Feb 4, 2022
1 parent 5fa2c48 commit aa2c39a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
55 changes: 34 additions & 21 deletions cocotb_test/simulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import subprocess
import os
import sys
import tempfile
Expand All @@ -12,6 +11,7 @@
import warnings
import cocotb._vendor.find_libpython as find_libpython
import cocotb.config
import asyncio

from distutils.spawn import find_executable
from distutils.sysconfig import get_config_var
Expand Down Expand Up @@ -247,12 +247,14 @@ def run(self):
for ts in tree.iter("testsuite"):
for tc in ts.iter("testcase"):
for _ in tc.iter("failure"):
self.logger.error(f'Failed: {tc.get("classname")}::{tc.get("name")}')
failed += 1

if failed:
raise SystemExit(f"ERROR: Failed {failed} tests.")
raise SystemExit(f"FAILED {failed} tests.")

self.logger.info(f"Results file: {results_xml_file}")

print("Results file: %s" % results_xml_file)
return results_xml_file

def get_include_commands(self, includes):
Expand Down Expand Up @@ -282,34 +284,45 @@ def get_abs_paths(self, paths):
libs[lib] = self.normalize_paths(src)
return libs

async def _log_pipe(self, level, stream):
while not stream.at_eof():
line = await stream.readline()
if line:
self.logger.log(level, line.decode('utf-8').rstrip())

async def _exec(self, cmds):

p = await asyncio.create_subprocess_exec(
*cmds,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=self.work_dir,
env=self.env
)

self.process = p

await asyncio.wait([
asyncio.create_task(self._log_pipe(logging.INFO, p.stdout)),
asyncio.create_task(self._log_pipe(logging.ERROR, p.stderr))
])

def execute(self, cmds):

__tracebackhide__ = True # Hide the traceback when using PyTest.

loop = asyncio.get_event_loop()

self.set_env()
for cmd in cmds:
self.logger.info("Running command: " + " ".join(cmd))

with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=self.work_dir,
env=self.env,
) as p:
self.process = p
for line in p.stdout:
self.logger.info(line.decode("utf-8").rstrip())
loop.run_until_complete(self._exec(cmd))

if self.process.returncode:
raise SystemExit(f"Process {cmd[0]} termindated with error {self.process.returncode}") #% (self.process.args[0], self.process.returncode))

self.process = None
if p.returncode:
raise SystemExit("Process '%s' termindated with error %d" % (p.args[0], p.returncode))

# def execute(self, cmds):
# self.set_env()
# for cmd in cmds:
# print(" ".join(cmd))
# process = subprocess.check_call(cmd, cwd=self.work_dir, env=self.env)

def outdated_list(self, output, dependencies):

Expand Down
2 changes: 1 addition & 1 deletion tests/dff_cocotb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ async def test_dff_simple(dut):
await FallingEdge(dut.clk) # Synchronize with the clock
for i in range(10):
val = random.randint(0, 1)
dut.d <= val # Assign the random value val to the input port d
dut.d.value = val # Assign the random value val to the input port d
await FallingEdge(dut.clk)
assert dut.q.value == val, "output q was incorrect on the {}th cycle".format(i)

0 comments on commit aa2c39a

Please sign in to comment.