diff --git a/.gitignore b/.gitignore index 09a95debe2..d138ad54d0 100644 --- a/.gitignore +++ b/.gitignore @@ -44,4 +44,6 @@ docs/vyper.*.rst # vyper vyper/version.py +vyper/vyper_git_version.txt +vyper/vyper_git_commithash.txt *.spec diff --git a/Makefile b/Makefile index d62bc02993..daa1c2bfc9 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ clean-build: @rm -fr _build/ # docs build dir @rm -fr dist/ @rm -fr *.egg-info - @rm -f vyper/version.py + @rm -f vyper/version.py vyper/vyper_git_version.txt vyper/vyper_git_commithash.txt @rm -f *.spec clean-docs: diff --git a/setup.py b/setup.py index 0830a9caf7..fbbd0b1cc8 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +import os import re +import subprocess from setuptools import find_packages, setup @@ -40,14 +42,9 @@ long_description = f.read() -# force commit hash to be appended to version even when tag is exact -# (breaks PEP 440, but this is the debug info, not the version tag for pypi) +# strip local version def _local_version(version): - commithash = version.node[1:] # git describe adds 'g' prefix - ret = f"+commit.{commithash}" - if version.dirty: - ret += "-dirty" - return ret + return "" def _global_version(version): @@ -59,6 +56,22 @@ def _global_version(version): return re.sub(r"\.dev\d+", "", version_str) +hash_file_rel_path = os.path.join("vyper", "vyper_git_commithash.txt") +hashfile = os.path.relpath(hash_file_rel_path) + +# there is no way in setuptools-scm to get metadata besides the package +# version into version.py. (and we need that version to be PEP440 compliant +# in order to get it into pypi). so, add the commit hash to the package +# separately, in order so that we can add it to `vyper --version`. +try: + commithash = subprocess.check_output("git rev-parse --short HEAD".split()) + commithash_str = commithash.decode("utf-8").strip() + with open(hashfile, "w") as fh: + fh.write(commithash_str) +except subprocess.CalledProcessError: + pass + + setup( name="vyper", use_scm_version={ @@ -105,4 +118,5 @@ def _global_version(version): "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", ], + data_files=[("", [hash_file_rel_path])], ) diff --git a/vyper/__init__.py b/vyper/__init__.py index f564a391e3..35237bd044 100644 --- a/vyper/__init__.py +++ b/vyper/__init__.py @@ -1,3 +1,5 @@ +from pathlib import Path as _Path + from vyper.compiler import compile_code, compile_codes # noqa: F401 try: @@ -7,6 +9,14 @@ from importlib_metadata import PackageNotFoundError # type: ignore from importlib_metadata import version as _version # type: ignore +_commit_hash_file = _Path(__file__).parent.joinpath("vyper_git_commithash.txt") + +if _commit_hash_file.exists(): + with _commit_hash_file.open() as fp: + __commit__ = fp.read() +else: + __commit__ = "unknown" + try: __version__ = _version(__name__) except PackageNotFoundError: diff --git a/vyper/cli/vyper_compile.py b/vyper/cli/vyper_compile.py index 98e234f603..956cc0169f 100755 --- a/vyper/cli/vyper_compile.py +++ b/vyper/cli/vyper_compile.py @@ -88,7 +88,7 @@ def _parse_args(argv): parser.add_argument( "--version", action="version", - version=vyper.__version__, + version=f"{vyper.__version__}+commit.{vyper.__commit__}", ) parser.add_argument( "--show-gas-estimates", diff --git a/vyper/cli/vyper_ir.py b/vyper/cli/vyper_ir.py index b524de2efa..76a8d25c97 100755 --- a/vyper/cli/vyper_ir.py +++ b/vyper/cli/vyper_ir.py @@ -21,7 +21,7 @@ def _parse_args(argv): parser.add_argument( "--version", action="version", - version=f"{vyper.__version__}", + version=f"{vyper.__version__}+commit{vyper.__commit__}", ) parser.add_argument( "-f", diff --git a/vyper/cli/vyper_json.py b/vyper/cli/vyper_json.py index 96258ab56d..556816fb4c 100755 --- a/vyper/cli/vyper_json.py +++ b/vyper/cli/vyper_json.py @@ -50,7 +50,7 @@ def _parse_args(argv): parser.add_argument( "--version", action="version", - version=vyper.__version__, + version=f"{vyper.__version__}+commit.{vyper.__commit__}", ) parser.add_argument( "-o", diff --git a/vyper/cli/vyper_serve.py b/vyper/cli/vyper_serve.py index 21ef6edf76..564e0d0110 100755 --- a/vyper/cli/vyper_serve.py +++ b/vyper/cli/vyper_serve.py @@ -18,7 +18,9 @@ def _parse_cli_args(): def _parse_args(argv): parser = argparse.ArgumentParser(description="Serve Vyper compiler as an HTTP Service") - parser.add_argument("--version", action="version", version=f"{vyper.__version__}") + parser.add_argument( + "--version", action="version", version=f"{vyper.__version__}+commit{vyper.__commit__}" + ) parser.add_argument( "-b", help="Address to bind JSON server on, default: localhost:8000",