Skip to content
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

use setuptools_scm #278

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ venv.bak/

# Editor
.vscode/
.idea/
.idea/

/dvclive/_version.py
75 changes: 6 additions & 69 deletions dvclive/version.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,6 @@
import os
import subprocess

_BASE_VERSION = "0.10.0"


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)
# pylint: disable=unused-import
try:
from ._version import version as __version__
from ._version import version_tuple # noqa: F401
except ImportError:
__version__ = "UNKNOWN"
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
[build-system]
requires = ["setuptools>=48", "setuptools_scm[toml]>=7"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "dvclive/_version.py"

[tool.black]
line-length = 79
include = '\.pyi?$'
Expand Down
38 changes: 0 additions & 38 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,4 @@
import importlib.util
import os

from setuptools import find_packages, setup
from setuptools.command.build_py import build_py as _build_py

# Read package meta-data from version.py
# see https://packaging.python.org/guides/single-sourcing-package-version/
pkg_dir = os.path.dirname(os.path.abspath(__file__))
version_path = os.path.join(pkg_dir, "dvclive", "version.py")
spec = importlib.util.spec_from_file_location("dvclive.version", version_path)
dvclive_version = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dvclive_version)
version = dvclive_version.__version__


# To achieve consistency between the build version and the one provided
# by your package during runtime, you need to **pin** the build version.
#
# This custom class will replace the version.py module with a **static**
# `__version__` that your package can read at runtime, assuring consistency.
#
# References:
# - https://docs.python.org/3/distutils/extending.html
# - https://github.com/python/mypy
class build_py(_build_py):
def pin_version(self):
path = os.path.join(self.build_lib, "dvclive")
self.mkpath(path)
with open(os.path.join(path, "version.py"), "w") as fobj:
fobj.write("# AUTOGENERATED at build time by setup.py\n")
fobj.write('__version__ = "{}"\n'.format(version))

def run(self):
self.execute(self.pin_version, ())
_build_py.run(self)


render = ["dvc_render[table]>=0.0.8"]
image = ["pillow"]
Expand Down Expand Up @@ -76,7 +40,6 @@ def run(self):

setup(
name="dvclive",
version=version,
author="Paweł Redzyński",
author_email="pawel@iterative.ai",
packages=find_packages(exclude="tests"),
Expand All @@ -102,7 +65,6 @@ def run(self):
},
keywords="data-science metrics machine-learning developer-tools ai",
python_requires=">=3.8",
cmdclass={"build_py": build_py},
url="https://dvc.org/doc/dvclive",
download_url="https://github.com/iterative/dvclive",
)