mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-06 15:28:28 +01:00
Merge pull request #1850 from mroutis/version-refactor
version: privatize version.py methods and vars
This commit is contained in:
+17
-13
@@ -1,34 +1,38 @@
|
||||
# Used from setup.py, so don't pull any additional dependencies
|
||||
# Used in setup.py, so don't pull any additional dependencies
|
||||
#
|
||||
# Based on:
|
||||
# - https://github.com/python/mypy/blob/master/mypy/version.py
|
||||
# - https://github.com/python/mypy/blob/master/mypy/git.py
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
BASE_VERSION = "0.35.0"
|
||||
_BASE_VERSION = "0.35.0"
|
||||
|
||||
|
||||
def generate_version(base_version):
|
||||
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():
|
||||
if not _is_git_repo(pkg_dir) or not _have_git():
|
||||
return base_version
|
||||
|
||||
if is_release(pkg_dir, base_version):
|
||||
if _is_release(pkg_dir, base_version):
|
||||
return base_version
|
||||
|
||||
return "{base_version}+{short_sha}{dirty}".format(
|
||||
base_version=base_version,
|
||||
short_sha=git_revision(pkg_dir).decode("utf-8")[0:6],
|
||||
dirty=".mod" if is_dirty(pkg_dir) else "",
|
||||
short_sha=_git_revision(pkg_dir).decode("utf-8")[0:6],
|
||||
dirty=".mod" if _is_dirty(pkg_dir) else "",
|
||||
)
|
||||
|
||||
|
||||
def is_git_repo(dir_path):
|
||||
def _is_git_repo(dir_path):
|
||||
"""Is the given directory version-controlled with git?"""
|
||||
return os.path.exists(os.path.join(dir_path, ".git"))
|
||||
|
||||
|
||||
def have_git():
|
||||
def _have_git():
|
||||
"""Can we run the git executable?"""
|
||||
try:
|
||||
subprocess.check_output(["git", "--help"])
|
||||
@@ -39,7 +43,7 @@ def have_git():
|
||||
return False
|
||||
|
||||
|
||||
def is_release(dir_path, base_version):
|
||||
def _is_release(dir_path, base_version):
|
||||
try:
|
||||
output = subprocess.check_output(
|
||||
["git", "describe", "--tags", "--exact-match"], cwd=dir_path
|
||||
@@ -50,14 +54,14 @@ def is_release(dir_path, base_version):
|
||||
return False
|
||||
|
||||
|
||||
def git_revision(dir_path):
|
||||
def _git_revision(dir_path):
|
||||
"""Get the SHA-1 of the HEAD of a git repository."""
|
||||
return subprocess.check_output(
|
||||
["git", "rev-parse", "HEAD"], cwd=dir_path
|
||||
).strip()
|
||||
|
||||
|
||||
def is_dirty(dir_path):
|
||||
def _is_dirty(dir_path):
|
||||
"""Check whether a git repository has uncommitted changes."""
|
||||
try:
|
||||
subprocess.check_call(["git", "diff", "--quiet"], cwd=dir_path)
|
||||
@@ -66,4 +70,4 @@ def is_dirty(dir_path):
|
||||
return True
|
||||
|
||||
|
||||
__version__ = generate_version(BASE_VERSION)
|
||||
__version__ = _generate_version(_BASE_VERSION)
|
||||
|
||||
@@ -9,14 +9,14 @@ 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)
|
||||
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)
|
||||
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)
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret is False
|
||||
|
||||
Reference in New Issue
Block a user