Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
# Keep sorted
"click>=8.1.7",
"enlighten>=1.12.4",
"gitpython>=3.1.45",
"hjson>=3.1.0",
"jinja2>=3.1.6",
"logzero>=1.7.0",
Expand Down
22 changes: 11 additions & 11 deletions src/dvsim/flow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import json
import os
import pprint
import re
import sys
from abc import ABC, abstractmethod
from collections.abc import Mapping, Sequence
Expand All @@ -29,6 +28,7 @@
rm_path,
subst_wildcards,
)
from dvsim.utils.git import git_commit_hash

if TYPE_CHECKING:
from dvsim.job.deploy import Deploy
Expand Down Expand Up @@ -455,6 +455,9 @@ def gen_results(self, results: Sequence[CompletedJobStatus]) -> None:

"""
reports_dir = Path(self.scratch_base_path) / "reports"
commit = git_commit_hash(path=Path(self.proj_root))
url = f"https://github.com/lowrisc/opentitan/tree/{commit}"

all_flow_results: Mapping[str, FlowResults] = {}

for item in self.cfgs:
Expand All @@ -464,7 +467,11 @@ def gen_results(self, results: Sequence[CompletedJobStatus]) -> None:
if res.block.name == item.name and res.block.variant == item.variant
]

flow_results: FlowResults = item._gen_json_results(item_results)
flow_results: FlowResults = item._gen_json_results(
run_results=item_results,
commit=commit,
url=url,
)

# Convert to lowercase to match filename
block_result_index = (
Expand All @@ -491,20 +498,13 @@ def gen_results(self, results: Sequence[CompletedJobStatus]) -> None:
.isoformat()
)

# Extract Git properties.
m = re.search(
r"https://github.com/.+?/tree/([0-9a-fA-F]+)",
self.revision,
)
commit = m.group(1) if m else None

results_summary = ResultsSummary(
top=IPMeta(
name=self.name,
variant=self.variant,
commit=str(commit),
commit=commit,
branch=self.branch,
url=self.revision,
url=url,
),
timestamp=timestamp,
flow_results=all_flow_results,
Expand Down
17 changes: 10 additions & 7 deletions src/dvsim/flow/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""Class describing simulation configuration object."""

import fnmatch
import re
import sys
from collections import OrderedDict, defaultdict
from collections.abc import Sequence
Expand Down Expand Up @@ -574,11 +573,18 @@ def cov_unr(self) -> None:
for item in self.cfgs:
item._cov_unr()

def _gen_json_results(self, run_results: Sequence[CompletedJobStatus]) -> FlowResults:
def _gen_json_results(
self,
run_results: Sequence[CompletedJobStatus],
commit: str,
url: str,
) -> FlowResults:
"""Generate structured FlowResults from simulation run data.

Args:
run_results: completed job status.
commit: git commit Hash
url: for the IP source

Returns:
Flow results object.
Expand All @@ -591,15 +597,12 @@ def _gen_json_results(self, run_results: Sequence[CompletedJobStatus]) -> FlowRe
# --- Metadata ---
timestamp = datetime.strptime(self.timestamp, TS_FORMAT).replace(tzinfo=timezone.utc)

commit_match = re.search(r"github.com/.+?/tree/([0-9a-f]{7,40})", self.revision)
commit = commit_match.group(1) if commit_match else None

block = IPMeta(
name=self.name.lower(),
variant=(self.variant or "").lower() or None,
commit=commit or "",
commit=commit,
branch=self.branch or "",
url=f"https://github.com/lowrisc/opentitan/tree/{commit}" if commit else "",
url=url,
)
tool = ToolMeta(name=self.tool.lower(), version="unknown")

Expand Down
36 changes: 31 additions & 5 deletions src/dvsim/utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

"""Git utinity functions."""
"""Git utility functions."""

from dvsim.utils.subprocess import run_cmd
import logging as log
from pathlib import Path

__all__ = ("git_commit_hash",)
from git import Repo

__all__ = ("repo_root",)

def git_commit_hash() -> str:

def repo_root(path: Path) -> Path | None:
"""Given a sub dir in a git repo provide the root path.

Where the provided path is not part of a repo then None is returned.
"""
if (path / ".git").exists():
return path

for p in path.parents:
if (p / ".git").exists():
return p

return None


def git_commit_hash(path: Path | None = None) -> str:
"""Hash of the current git commit."""
return run_cmd("git rev-parse HEAD")
root = repo_root(path=path if path else Path.cwd())

if root is None:
log.error("no git repo found at %s", root)
raise ValueError

r = Repo(root)

return r.head.commit.hexsha
55 changes: 55 additions & 0 deletions tests/utils/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

"""Test git helpers."""

from typing import TYPE_CHECKING

from git import Repo
from hamcrest import assert_that, equal_to

from dvsim.utils.git import git_commit_hash, repo_root

if TYPE_CHECKING:
from pathlib import Path

__all__ = ()


class TestGit:
"""Test git helpers."""

@staticmethod
def test_repo_root(tmp_path: "Path") -> None:
"""Test git repo root can be found."""
repo_root_path = tmp_path / "repo"
repo_root_path.mkdir()

Repo.init(path=repo_root_path)

# from the actual repo root
assert_that(repo_root(path=repo_root_path), equal_to(repo_root_path))

# from the repo sub dir
sub_dir_path = repo_root_path / "a"
sub_dir_path.mkdir()
assert_that(repo_root(path=sub_dir_path), equal_to(repo_root_path))

# from outside the repo
assert_that(repo_root(path=tmp_path), equal_to(None))

@staticmethod
def test_git_commit_hash(tmp_path: "Path") -> None:
"""Test that the expected git commit sha is returned."""
r = Repo.init(path=tmp_path)

file = tmp_path / "a"
file.write_text("file to commit")
r.index.add([file])
r.index.commit("initial commit")

assert_that(
git_commit_hash(tmp_path),
equal_to(r.head.commit.hexsha),
)
12 changes: 7 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading