-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test build: turn on #28
Conversation
26dd93b
to
f05b7cf
Compare
import os | ||
import subprocess | ||
|
||
_BASE_VERSION = "0.0.1" | ||
|
||
|
||
def _generate_version(base_version): | ||
"""Generate a version with information about the Git repository.""" | ||
pkg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
if not _is_git_repo(pkg_dir) or not _have_git(): | ||
return base_version | ||
|
||
if _is_release(pkg_dir, base_version) and not _is_dirty(pkg_dir): | ||
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 "", | ||
) | ||
|
||
|
||
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(): | ||
"""Can we run the git executable?""" | ||
try: | ||
subprocess.check_output(["git", "--help"]) | ||
return True | ||
except subprocess.CalledProcessError: | ||
return False | ||
except OSError: | ||
return False | ||
|
||
|
||
def _is_release(dir_path, base_version): | ||
try: | ||
output = subprocess.check_output( | ||
["git", "describe", "--tags", "--exact-match"], | ||
cwd=dir_path, | ||
stderr=subprocess.STDOUT, | ||
).decode("utf-8") | ||
tag = output.strip() | ||
return tag == base_version | ||
except subprocess.CalledProcessError: | ||
return False | ||
|
||
|
||
def _git_revision(dir_path): | ||
"""Get SHA of the HEAD of a Git repository.""" | ||
return subprocess.check_output( | ||
["git", "rev-parse", "HEAD"], cwd=dir_path | ||
).strip() | ||
|
||
|
||
def _is_dirty(dir_path): | ||
"""Check whether a git repository has uncommitted changes.""" | ||
try: | ||
subprocess.check_call(["git", "diff", "--quiet"], cwd=dir_path) | ||
return False | ||
except subprocess.CalledProcessError: | ||
return True | ||
|
||
|
||
__version__ = _generate_version(_BASE_VERSION) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shamelessly stolen borrowed from https://github.com/iterative/dvc in order to maintain consistent versioning.
Btw, looks like you are missing https://github.com/iterative/dvc/blob/550cbb5e1154dff930b1cff33ee159bb16b03693/setup.py#L35 . That part generates the version file when you install the package, so you have a frozen version. |
No description provided.