add get_build_metadata function (#31923)

* version

* Get build metadata

* two lines

* channel

* cwd

* default to unknown

* dataclass
This commit is contained in:
Justin Newberry 2024-03-20 19:43:58 -04:00 committed by GitHub
parent 1a03da9df3
commit 38d03b9979
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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