From d94f6d74eee17e4dc25346742fc059f8a99061c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Trifir=C3=B2?= Date: Wed, 31 Aug 2022 18:32:35 +0200 Subject: [PATCH] use setuptools_scm --- .gitignore | 4 ++- dvclive/version.py | 75 ++++------------------------------------------ pyproject.toml | 7 +++++ setup.py | 38 ----------------------- 4 files changed, 16 insertions(+), 108 deletions(-) diff --git a/.gitignore b/.gitignore index 622b336c..0e4619b2 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,6 @@ venv.bak/ # Editor .vscode/ -.idea/ \ No newline at end of file +.idea/ + +/dvclive/_version.py diff --git a/dvclive/version.py b/dvclive/version.py index 9753c541..3611fbee 100644 --- a/dvclive/version.py +++ b/dvclive/version.py @@ -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" diff --git a/pyproject.toml b/pyproject.toml index a4fc8cbe..64ceb5c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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?$' diff --git a/setup.py b/setup.py index 91e9e586..fe5e5e07 100644 --- a/setup.py +++ b/setup.py @@ -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"] @@ -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"), @@ -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", )