Files
dragonpilot/selfdrive/version.py

74 lines
3.0 KiB
Python
Raw Normal View History

2019-10-09 18:43:53 +00:00
#!/usr/bin/env python3
2017-05-11 12:41:17 -07:00
import os
2018-03-17 00:01:50 -07:00
import subprocess
2019-04-16 12:55:20 -07:00
from selfdrive.swaglog import cloudlog
2019-06-06 04:38:45 +00:00
def get_git_commit():
2019-10-09 18:43:53 +00:00
return subprocess.check_output(["git", "rev-parse", "HEAD"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
2019-06-06 04:38:45 +00:00
def get_git_branch():
2019-10-09 18:43:53 +00:00
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
2019-06-06 04:38:45 +00:00
def get_git_full_branchname():
2019-10-09 18:43:53 +00:00
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
2019-06-06 04:38:45 +00:00
def get_git_remote():
try:
2019-10-09 18:43:53 +00:00
local_branch = subprocess.check_output(["git", "name-rev", "--name-only", "HEAD"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
tracking_remote = subprocess.check_output(["git", "config", "branch." + local_branch + ".remote"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
return subprocess.check_output(["git", "config", "remote." + tracking_remote + ".url"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
2019-06-06 04:38:45 +00:00
except subprocess.CalledProcessError:
# Not on a branch, fallback
2019-10-09 18:43:53 +00:00
return subprocess.check_output(["git", "config", "--get", "remote.origin.url"], encoding='utf8').strip() # pylint: disable=unexpected-keyword-arg
2019-06-06 04:38:45 +00:00
2017-05-11 12:41:17 -07:00
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "common", "version.h")) as _versionf:
version = _versionf.read().split('"')[1]
2018-03-17 00:01:50 -07:00
try:
2019-06-06 04:38:45 +00:00
origin = get_git_remote()
2018-06-16 20:59:34 -07:00
if origin.startswith('git@github.com:commaai') or origin.startswith('https://github.com/commaai'):
2018-04-14 06:10:58 +00:00
if origin.endswith('/one.git'):
dirty = True
2018-03-17 00:01:50 -07:00
else:
2019-06-06 04:38:45 +00:00
branch = get_git_full_branchname()
# This is needed otherwise touched files might show up as modified
try:
subprocess.check_call(["git", "update-index", "--refresh"])
except subprocess.CalledProcessError:
pass
2018-04-14 06:10:58 +00:00
dirty = subprocess.call(["git", "diff-index", "--quiet", branch, "--"]) != 0
2019-04-16 12:55:20 -07:00
if dirty:
2019-10-09 18:43:53 +00:00
dirty_files = subprocess.check_output(["git", "diff-index", branch, "--"], encoding='utf8') # pylint: disable=unexpected-keyword-arg
commit = subprocess.check_output(["git", "rev-parse", "--verify", "HEAD"], encoding='utf8').rstrip() # pylint: disable=unexpected-keyword-arg
origin_commit = subprocess.check_output(["git", "rev-parse", "--verify", branch], encoding='utf8').rstrip() # pylint: disable=unexpected-keyword-arg
2019-04-16 12:55:20 -07:00
cloudlog.event("dirty comma branch", vesion=version, dirty=dirty, origin=origin, branch=branch, dirty_files=dirty_files, commit=commit, origin_commit=origin_commit)
2019-06-06 04:38:45 +00:00
2018-04-14 06:10:58 +00:00
else:
dirty = True
2018-03-17 00:01:50 -07:00
except subprocess.CalledProcessError:
2019-04-16 12:55:20 -07:00
try:
cloudlog.exception("git subprocess failed while finding version")
except:
pass
2018-03-17 00:01:50 -07:00
dirty = True
2018-04-28 09:44:39 +00:00
2019-10-09 18:43:53 +00:00
training_version = b"0.1.0"
terms_version = b"2"
2019-06-06 04:38:45 +00:00
if __name__ == "__main__":
print("Dirty: %s" % dirty)
print("Version: %s" % version)
print("Remote: %s" % origin)
try:
print("Branch %s" % branch)
except NameError:
pass