From 58f07d754c4536d40678fc4ca83b2afb9e22a6c3 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sat, 19 Sep 2020 14:55:27 -0700 Subject: [PATCH] add type hints to selfdrive/version.py old-commit-hash: 0040ac987aa4e836744abc5f541c1aeeda17edf5 --- .../test/process_replay/camera_replay.py | 2 ++ selfdrive/test/process_replay/update_model.py | 2 ++ selfdrive/test/process_replay/update_refs.py | 2 ++ selfdrive/version.py | 24 ++++++++++--------- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/selfdrive/test/process_replay/camera_replay.py b/selfdrive/test/process_replay/camera_replay.py index fb50b0c4af..08911c6d99 100755 --- a/selfdrive/test/process_replay/camera_replay.py +++ b/selfdrive/test/process_replay/camera_replay.py @@ -84,6 +84,8 @@ if __name__ == "__main__": if update: ref_commit = get_git_commit() + if ref_commit is None: + raise Exception("couldn't get ref commit") log_fn = "%s_%s_%s.bz2" % (TEST_ROUTE, "model", ref_commit) save_log(log_fn, log_msgs) with open("model_replay_ref_commit", "w") as f: diff --git a/selfdrive/test/process_replay/update_model.py b/selfdrive/test/process_replay/update_model.py index 31629563e2..a53e26a19c 100755 --- a/selfdrive/test/process_replay/update_model.py +++ b/selfdrive/test/process_replay/update_model.py @@ -17,6 +17,8 @@ if __name__ == "__main__": ref_commit_fn = os.path.join(process_replay_dir, "model_ref_commit") ref_commit = get_git_commit() + if ref_commit is None: + raise Exception("couldn't get ref commit") with open(ref_commit_fn, "w") as f: f.write(ref_commit) diff --git a/selfdrive/test/process_replay/update_refs.py b/selfdrive/test/process_replay/update_refs.py index b4243c56d3..054257bfbf 100755 --- a/selfdrive/test/process_replay/update_refs.py +++ b/selfdrive/test/process_replay/update_refs.py @@ -17,6 +17,8 @@ if __name__ == "__main__": ref_commit_fn = os.path.join(process_replay_dir, "ref_commit") ref_commit = get_git_commit() + if ref_commit is None: + raise Exception("couldn't get ref commit") with open(ref_commit_fn, "w") as f: f.write(ref_commit) diff --git a/selfdrive/version.py b/selfdrive/version.py index 4fec0b4cc7..ef975f8940 100644 --- a/selfdrive/version.py +++ b/selfdrive/version.py @@ -1,34 +1,36 @@ #!/usr/bin/env python3 import os import subprocess +from typing import List, Optional + from common.basedir import BASEDIR from selfdrive.swaglog import cloudlog -def run_cmd(cmd): +def run_cmd(cmd: List[str]) -> str: return subprocess.check_output(cmd, encoding='utf8').strip() -def run_cmd_default(cmd, default=None): +def run_cmd_default(cmd: List[str], default: Optional[str] = None) -> Optional[str]: try: return run_cmd(cmd) except subprocess.CalledProcessError: return default -def get_git_commit(branch="HEAD", default=None): +def get_git_commit(branch: str = "HEAD", default: Optional[str] = None) -> Optional[str]: return run_cmd_default(["git", "rev-parse", branch], default=default) -def get_git_branch(default=None): +def get_git_branch(default: Optional[str] = None) -> Optional[str]: return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "HEAD"], default=default) -def get_git_full_branchname(default=None): +def get_git_full_branchname(default: Optional[str] = None) -> Optional[str]: return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], default=default) -def get_git_remote(default=None): +def get_git_remote(default: Optional[str] = None) -> Optional[str]: try: local_branch = run_cmd(["git", "name-rev", "--name-only", "HEAD"]) tracking_remote = run_cmd(["git", "config", "branch." + local_branch + ".remote"]) @@ -42,12 +44,12 @@ with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "common", "ve prebuilt = os.path.exists(os.path.join(BASEDIR, 'prebuilt')) -training_version = b"0.2.0" -terms_version = b"2" +training_version: bytes = b"0.2.0" +terms_version: bytes = b"2" -dirty = True -comma_remote = False -tested_branch = False +dirty: bool = True +comma_remote: bool = False +tested_branch: bool = False origin = get_git_remote() branch = get_git_full_branchname()