Merge pull request #1849 from efiop/version

Version generation improvements
This commit is contained in:
Ruslan Kuprieiev
2019-04-07 23:51:33 +03:00
committed by GitHub
2 changed files with 42 additions and 3 deletions
+20 -3
View File
@@ -3,11 +3,17 @@ import os
import subprocess
BASE_VERSION = "0.35.0"
def generate_version(base_version):
"""Generate a version with information about the git repository"""
pkg_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
if not is_git_repo(pkg_dir) or not have_git(pkg_dir):
if not is_git_repo(pkg_dir) or not have_git():
return base_version
if is_release(pkg_dir, base_version):
return base_version
return "{base_version}+{short_sha}{dirty}".format(
@@ -22,7 +28,7 @@ def is_git_repo(dir_path):
return os.path.exists(os.path.join(dir_path, ".git"))
def have_git(dir_path):
def have_git():
"""Can we run the git executable?"""
try:
subprocess.check_output(["git", "--help"])
@@ -33,6 +39,17 @@ def have_git(dir_path):
return False
def is_release(dir_path, base_version):
try:
output = subprocess.check_output(
["git", "describe", "--tags", "--exact-match"], cwd=dir_path
).decode("utf-8")
tag = output.strip()
return tag == base_version
except subprocess.CalledProcessError:
return False
def git_revision(dir_path):
"""Get the SHA-1 of the HEAD of a git repository."""
return subprocess.check_output(
@@ -49,4 +66,4 @@ def is_dirty(dir_path):
return True
__version__ = generate_version(base_version="0.35.0")
__version__ = generate_version(BASE_VERSION)
+22
View File
@@ -0,0 +1,22 @@
import mock
import subprocess
import dvc.version
from dvc.utils.compat import cast_bytes
def test_is_release():
with mock.patch.object(dvc.version.subprocess, "check_output") as m:
m.side_effect = subprocess.CalledProcessError(1, "cmd")
ret = dvc.version.is_release(None, dvc.version.BASE_VERSION)
assert ret is False
m.side_effect = None
m.return_value = cast_bytes(dvc.version.BASE_VERSION)
ret = dvc.version.is_release(None, dvc.version.BASE_VERSION)
assert ret
m.return_value = cast_bytes("630d1741c2d5dd89a3176bd15b63121b905d35c9")
ret = dvc.version.is_release(None, dvc.version.BASE_VERSION)
assert ret is False