cleanup version.py

pull/1020/head
Willem Melching 5 years ago
parent 277b187a29
commit b552a627e1
  1. 45
      selfdrive/version.py

@ -5,15 +5,24 @@ from selfdrive.swaglog import cloudlog
def get_git_commit(): def get_git_commit():
try:
return subprocess.check_output(["git", "rev-parse", "HEAD"], encoding='utf8').strip() return subprocess.check_output(["git", "rev-parse", "HEAD"], encoding='utf8').strip()
except subprocess.CalledProcessError:
return None
def get_git_branch(): def get_git_branch():
try:
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], encoding='utf8').strip() return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], encoding='utf8').strip()
except subprocess.CalledProcessError:
return None
def get_git_full_branchname(): def get_git_full_branchname():
try:
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], encoding='utf8').strip() return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], encoding='utf8').strip()
except subprocess.CalledProcessError:
return None
def get_git_remote(): def get_git_remote():
@ -21,53 +30,49 @@ def get_git_remote():
local_branch = subprocess.check_output(["git", "name-rev", "--name-only", "HEAD"], encoding='utf8').strip() local_branch = subprocess.check_output(["git", "name-rev", "--name-only", "HEAD"], encoding='utf8').strip()
tracking_remote = subprocess.check_output(["git", "config", "branch." + local_branch + ".remote"], encoding='utf8').strip() tracking_remote = subprocess.check_output(["git", "config", "branch." + local_branch + ".remote"], encoding='utf8').strip()
return subprocess.check_output(["git", "config", "remote." + tracking_remote + ".url"], encoding='utf8').strip() return subprocess.check_output(["git", "config", "remote." + tracking_remote + ".url"], encoding='utf8').strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
try:
# Not on a branch, fallback # Not on a branch, fallback
return subprocess.check_output(["git", "config", "--get", "remote.origin.url"], encoding='utf8').strip() return subprocess.check_output(["git", "config", "--get", "remote.origin.url"], encoding='utf8').strip()
except subprocess.CalledProcessError:
return None
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "common", "version.h")) as _versionf: with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "common", "version.h")) as _versionf:
version = _versionf.read().split('"')[1] version = _versionf.read().split('"')[1]
try: training_version = b"0.2.0"
origin = get_git_remote() terms_version = b"2"
if origin.startswith('git@github.com:commaai') or origin.startswith('https://github.com/commaai'):
if origin.endswith('/one.git'): dirty = True
dirty = True origin = get_git_remote()
else: branch = get_git_full_branchname()
branch = get_git_full_branchname()
try:
is_comma_remote = origin.startswith('git@github.com:commaai') or origin.startswith('https://github.com/commaai')
if is_comma_remote and branch is not None:
# This is needed otherwise touched files might show up as modified # This is needed otherwise touched files might show up as modified
try: try:
subprocess.check_call(["git", "update-index", "--refresh"]) subprocess.check_call(["git", "update-index", "--refresh"])
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
pass pass
dirty = subprocess.call(["git", "diff-index", "--quiet", branch, "--"]) != 0 dirty = 'master' in branch
dirty = dirty or (subprocess.call(["git", "diff-index", "--quiet", branch, "--"]) != 0)
if dirty: if dirty:
dirty_files = subprocess.check_output(["git", "diff-index", branch, "--"], encoding='utf8') dirty_files = subprocess.check_output(["git", "diff-index", branch, "--"], encoding='utf8')
commit = subprocess.check_output(["git", "rev-parse", "--verify", "HEAD"], encoding='utf8').rstrip() commit = subprocess.check_output(["git", "rev-parse", "--verify", "HEAD"], encoding='utf8').rstrip()
origin_commit = subprocess.check_output(["git", "rev-parse", "--verify", branch], encoding='utf8').rstrip() origin_commit = subprocess.check_output(["git", "rev-parse", "--verify", branch], encoding='utf8').rstrip()
cloudlog.event("dirty comma branch", version=version, dirty=dirty, origin=origin, branch=branch, dirty_files=dirty_files, commit=commit, origin_commit=origin_commit) cloudlog.event("dirty comma branch", version=version, dirty=dirty, origin=origin, branch=branch, dirty_files=dirty_files, commit=commit, origin_commit=origin_commit)
else:
dirty = True
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
try:
cloudlog.exception("git subprocess failed while finding version")
except:
pass
dirty = True dirty = True
cloudlog.exception("git subprocess failed while checking dirty")
training_version = b"0.2.0"
terms_version = b"2"
if __name__ == "__main__": if __name__ == "__main__":
print("Dirty: %s" % dirty) print("Dirty: %s" % dirty)
print("Version: %s" % version) print("Version: %s" % version)
print("Remote: %s" % origin) print("Remote: %s" % origin)
try:
print("Branch %s" % branch) print("Branch %s" % branch)
except NameError:
pass

Loading…
Cancel
Save