You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					140 lines
				
				3.9 KiB
			
		
		
			
		
	
	
					140 lines
				
				3.9 KiB
			| 
											6 years ago
										 | #!/usr/bin/env python3
 | ||
|  | import os
 | ||
|  | import subprocess
 | ||
| 
											5 years ago
										 | from typing import List, Optional
 | ||
| 
											4 years ago
										 | from functools import lru_cache
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | from common.basedir import BASEDIR
 | ||
| 
											3 years ago
										 | from system.swaglog import cloudlog
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | TESTED_BRANCHES = ['devel', 'release3-staging', 'dashcam3-staging', 'release3', 'dashcam3']
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											4 years ago
										 | training_version: bytes = b"0.2.0"
 | ||
|  | terms_version: bytes = b"2"
 | ||
|  | 
 | ||
|  | 
 | ||
|  | def cache(user_function, /):
 | ||
|  |   return lru_cache(maxsize=None)(user_function)
 | ||
|  | 
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											5 years ago
										 | def run_cmd(cmd: List[str]) -> str:
 | ||
| 
											4 years ago
										 |   return subprocess.check_output(cmd, encoding='utf8').strip()
 | ||
| 
											5 years ago
										 | 
 | ||
|  | 
 | ||
| 
											5 years ago
										 | def run_cmd_default(cmd: List[str], default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											6 years ago
										 |   try:
 | ||
| 
											5 years ago
										 |     return run_cmd(cmd)
 | ||
| 
											6 years ago
										 |   except subprocess.CalledProcessError:
 | ||
| 
											6 years ago
										 |     return default
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_commit(branch: str = "HEAD", default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											5 years ago
										 |   return run_cmd_default(["git", "rev-parse", branch], default=default)
 | ||
|  | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_short_branch(default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											5 years ago
										 |   return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "HEAD"], default=default)
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_branch(default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											5 years ago
										 |   return run_cmd_default(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], default=default)
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_origin(default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											6 years ago
										 |   try:
 | ||
| 
											5 years ago
										 |     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
 | ||
| 
											5 years ago
										 |     return run_cmd_default(["git", "config", "--get", "remote.origin.url"], default=default)
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_normalized_origin(default: Optional[str] = None) -> Optional[str]:
 | ||
| 
											4 years ago
										 |   origin: Optional[str] = get_origin()
 | ||
| 
											4 years ago
										 | 
 | ||
|  |   if origin is None:
 | ||
|  |     return default
 | ||
|  | 
 | ||
|  |   return origin.replace("git@", "", 1) \
 | ||
|  |                .replace(".git", "", 1) \
 | ||
|  |                .replace("https://", "", 1) \
 | ||
|  |                .replace(":", "/", 1)
 | ||
| 
											4 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_version() -> str:
 | ||
| 
											4 years ago
										 |   with open(os.path.join(BASEDIR, "common", "version.h")) as _versionf:
 | ||
| 
											4 years ago
										 |     version = _versionf.read().split('"')[1]
 | ||
|  |   return version
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
|  | def get_short_version() -> str:
 | ||
| 
											4 years ago
										 |   return get_version().split('-')[0]  # type: ignore
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
| 
											4 years ago
										 | def is_prebuilt() -> bool:
 | ||
| 
											4 years ago
										 |   return os.path.exists(os.path.join(BASEDIR, 'prebuilt'))
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
| 
											4 years ago
										 | def is_comma_remote() -> bool:
 | ||
| 
											4 years ago
										 |   # 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
 | ||
| 
											4 years ago
										 |   origin: Optional[str] = get_origin()
 | ||
| 
											4 years ago
										 |   if origin is None:
 | ||
|  |     return False
 | ||
|  | 
 | ||
|  |   return origin.startswith('git@github.com:commaai') or origin.startswith('https://github.com/commaai')
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | @cache
 | ||
| 
											4 years ago
										 | def is_tested_branch() -> bool:
 | ||
| 
											4 years ago
										 |   return get_short_branch() in TESTED_BRANCHES
 | ||
|  | 
 | ||
|  | 
 | ||
|  | @cache
 | ||
| 
											4 years ago
										 | def is_dirty() -> bool:
 | ||
| 
											4 years ago
										 |   origin = get_origin()
 | ||
|  |   branch = get_branch()
 | ||
|  |   if (origin is None) or (branch is None):
 | ||
|  |     return True
 | ||
|  | 
 | ||
|  |   dirty = False
 | ||
|  |   try:
 | ||
| 
											5 years ago
										 |     # Actually check dirty files
 | ||
| 
											4 years ago
										 |     if not is_prebuilt():
 | ||
| 
											5 years ago
										 |       # This is needed otherwise touched files might show up as modified
 | ||
|  |       try:
 | ||
|  |         subprocess.check_call(["git", "update-index", "--refresh"])
 | ||
|  |       except subprocess.CalledProcessError:
 | ||
|  |         pass
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											5 years ago
										 |       dirty = (subprocess.call(["git", "diff-index", "--quiet", branch, "--"]) != 0)
 | ||
|  |   except subprocess.CalledProcessError:
 | ||
|  |     cloudlog.exception("git subprocess failed while checking dirty")
 | ||
| 
											4 years ago
										 |     dirty = True
 | ||
|  | 
 | ||
|  |   return dirty
 | ||
| 
											6 years ago
										 | 
 | ||
|  | 
 | ||
|  | if __name__ == "__main__":
 | ||
| 
											4 years ago
										 |   from common.params import Params
 | ||
|  | 
 | ||
|  |   params = Params()
 | ||
|  |   params.put("TermsVersion", terms_version)
 | ||
|  |   params.put("TrainingVersion", training_version)
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   print(f"Dirty: {is_dirty()}")
 | ||
|  |   print(f"Version: {get_version()}")
 | ||
| 
											4 years ago
										 |   print(f"Short version: {get_short_version()}")
 | ||
| 
											4 years ago
										 |   print(f"Origin: {get_origin()}")
 | ||
| 
											4 years ago
										 |   print(f"Normalized origin: {get_normalized_origin()}")
 | ||
| 
											4 years ago
										 |   print(f"Branch: {get_branch()}")
 | ||
|  |   print(f"Short branch: {get_short_branch()}")
 | ||
|  |   print(f"Prebuilt: {is_prebuilt()}")
 |