@ -1,7 +1,7 @@
#!/usr/bin/env python3
import os
import subprocess
from typing import List , Optional
from typing import List , Optional , Callable , TypeVar
from functools import lru_cache
from openpilot . common . basedir import BASEDIR
@ -13,8 +13,8 @@ TESTED_BRANCHES = RELEASE_BRANCHES + ['devel', 'devel-staging']
training_version : bytes = b " 0.2.0 "
terms_version : bytes = b " 2 "
def cache ( user_function , / ) :
_RT = TypeVar ( " _RT " )
def cache ( user_function : Callable [ . . . , _RT ] , / ) - > Callable [ . . . , _RT ] :
return lru_cache ( maxsize = None ) ( user_function )
@ -30,41 +30,37 @@ def run_cmd_default(cmd: List[str], default: Optional[str] = None) -> Optional[s
@cache
def get_commit ( branch : str = " HEAD " , default : Optional [ str ] = None ) - > Optional [ str ] :
return run_cmd_default ( [ " git " , " rev-parse " , branch ] , default = default )
def get_commit ( branch : str = " HEAD " ) - > str :
return run_cmd_default ( [ " git " , " rev-parse " , branch ] ) or " "
@cache
def get_short_branch ( default : Optional [ str ] = None ) - > Optional [ str ] :
return run_cmd_default ( [ " git " , " rev-parse " , " --abbrev-ref " , " HEAD " ] , default = default )
def get_short_branch ( ) - > str :
return run_cmd_default ( [ " git " , " rev-parse " , " --abbrev-ref " , " HEAD " ] ) or " "
@cache
def get_branch ( default : Optional [ str ] = None ) - > Optional [ str ] :
return run_cmd_default ( [ " git " , " rev-parse " , " --abbrev-ref " , " --symbolic-full-name " , " @ {u} " ] , default = default )
def get_branch ( ) - > str :
return run_cmd_default ( [ " git " , " rev-parse " , " --abbrev-ref " , " --symbolic-full-name " , " @ {u} " ] ) or " "
@cache
def get_origin ( default : Optional [ str ] = None ) - > Optional [ str ] :
def get_origin ( ) - > str :
try :
local_branch = run_cmd ( [ " git " , " name-rev " , " --name-only " , " HEAD " ] )
tracking_remote = run_cmd ( [ " git " , " config " , " branch. " + local_branch + " .remote " ] )
return run_cmd ( [ " git " , " config " , " remote. " + tracking_remote + " .url " ] )
except subprocess . CalledProcessError : # Not on a branch, fallback
return run_cmd_default ( [ " git " , " config " , " --get " , " remote.origin.url " ] , default = default )
return run_cmd_default ( [ " git " , " config " , " --get " , " remote.origin.url " ] ) or " "
@cache
def get_normalized_origin ( default : Optional [ str ] = None ) - > Optional [ str ] :
origin : Optional [ str ] = get_origin ( )
if origin is None :
return default
return origin . replace ( " git@ " , " " , 1 ) \
. replace ( " .git " , " " , 1 ) \
. replace ( " https:// " , " " , 1 ) \
. replace ( " : " , " / " , 1 )
def get_normalized_origin ( ) - > str :
return get_origin ( ) \
. replace ( " git@ " , " " , 1 ) \
. replace ( " .git " , " " , 1 ) \
. replace ( " https:// " , " " , 1 ) \
. replace ( " : " , " / " , 1 )
@cache
@ -75,7 +71,7 @@ def get_version() -> str:
@cache
def get_short_version ( ) - > str :
return get_version ( ) . split ( ' - ' ) [ 0 ] # type: ignore
return get_version ( ) . split ( ' - ' ) [ 0 ]
@cache
def is_prebuilt ( ) - > bool :
@ -86,12 +82,7 @@ def is_prebuilt() -> bool:
def is_comma_remote ( ) - > bool :
# note to fork maintainers, this is used for release metrics. please do not
# touch this to get rid of the orange startup alert. there's better ways to do that
origin : Optional [ str ] = get_origin ( )
if origin is None :
return False
return origin . startswith ( ( ' git@github.com:commaai ' , ' https://github.com/commaai ' ) )
return get_normalized_origin ( ) == " github.com/commaai/openpilot "
@cache
def is_tested_branch ( ) - > bool :
@ -105,7 +96,7 @@ def is_release_branch() -> bool:
def is_dirty ( ) - > bool :
origin = get_origin ( )
branch = get_branch ( )
if ( origin is None ) or ( branch is None ) :
if not origin or not branch :
return True
dirty = False