From 38d03b997958effea805a6c3860d2c5edaf2ae65 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 20 Mar 2024 19:43:58 -0400 Subject: [PATCH] add get_build_metadata function (#31923) * version * Get build metadata * two lines * channel * cwd * default to unknown * dataclass --- common/git.py | 16 ++++++++-------- common/run.py | 8 ++++---- system/version.py | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/common/git.py b/common/git.py index e15a5051d2..1ab8b87099 100644 --- a/common/git.py +++ b/common/git.py @@ -4,23 +4,23 @@ from openpilot.common.run import run_cmd, run_cmd_default @cache -def get_commit(branch: str = "HEAD") -> str: - return run_cmd_default(["git", "rev-parse", branch]) +def get_commit(cwd: str = None, branch: str = "HEAD") -> str: + return run_cmd_default(["git", "rev-parse", branch], cwd=cwd) @cache -def get_commit_date(commit: str = "HEAD") -> str: - return run_cmd_default(["git", "show", "--no-patch", "--format='%ct %ci'", commit]) +def get_commit_date(cwd: str = None, commit: str = "HEAD") -> str: + return run_cmd_default(["git", "show", "--no-patch", "--format='%ct %ci'", commit], cwd=cwd) @cache -def get_short_branch() -> str: - return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "HEAD"]) +def get_short_branch(cwd: str = None) -> str: + return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd) @cache -def get_branch() -> str: - return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]) +def get_branch(cwd: str = None) -> str: + return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], cwd=cwd) @cache diff --git a/common/run.py b/common/run.py index 25abe98c41..c1a36bf041 100644 --- a/common/run.py +++ b/common/run.py @@ -1,13 +1,13 @@ import subprocess -def run_cmd(cmd: list[str]) -> str: - return subprocess.check_output(cmd, encoding='utf8').strip() +def run_cmd(cmd: list[str], cwd=None) -> str: + return subprocess.check_output(cmd, encoding='utf8', cwd=cwd).strip() -def run_cmd_default(cmd: list[str], default: str = "") -> str: +def run_cmd_default(cmd: list[str], default: str = "", cwd=None) -> str: try: - return run_cmd(cmd) + return run_cmd(cmd, cwd=cwd) except subprocess.CalledProcessError: return default diff --git a/system/version.py b/system/version.py index 7ae8313089..8866026152 100755 --- a/system/version.py +++ b/system/version.py @@ -1,17 +1,22 @@ #!/usr/bin/env python3 +from dataclasses import dataclass +import json import os +import pathlib import subprocess from openpilot.common.basedir import BASEDIR from openpilot.common.swaglog import cloudlog from openpilot.common.utils import cache -from openpilot.common.git import get_origin, get_branch, get_short_branch, get_normalized_origin, get_commit_date +from openpilot.common.git import get_commit, get_origin, get_branch, get_short_branch, get_normalized_origin, get_commit_date RELEASE_BRANCHES = ['release3-staging', 'release3', 'nightly'] TESTED_BRANCHES = RELEASE_BRANCHES + ['devel', 'devel-staging'] +BUILD_METADATA_FILENAME = "build.json" + training_version: bytes = b"0.2.0" terms_version: bytes = b"2" @@ -75,6 +80,41 @@ def is_dirty() -> bool: return dirty +@dataclass(frozen=True) +class OpenpilotMetadata: + version: str + release_notes: str + git_commit: str + + +@dataclass(frozen=True) +class BuildMetadata: + channel: str + openpilot: OpenpilotMetadata + + + +def get_build_metadata(path: str = BASEDIR) -> BuildMetadata | None: + build_metadata_path = pathlib.Path(path) / BUILD_METADATA_FILENAME + + if build_metadata_path.exists(): + build_metadata = json.loads(build_metadata_path.read_text()) + openpilot_metadata = build_metadata.get("openpilot", {}) + + channel = build_metadata.get("channel", "unknown") + version = openpilot_metadata.get("version", "unknown") + release_notes = openpilot_metadata.get("release_notes", "unknown") + git_commit = openpilot_metadata.get("git_commit", "unknown") + return BuildMetadata(channel, OpenpilotMetadata(version, release_notes, git_commit)) + + git_folder = pathlib.Path(path) / ".git" + + if git_folder.exists(): + return BuildMetadata(get_short_branch(path), OpenpilotMetadata(get_version(path), get_release_notes(path), get_commit(path))) + + return None + + if __name__ == "__main__": from openpilot.common.params import Params