Skip to content

Commit

Permalink
fix: package version (#2816)
Browse files Browse the repository at this point in the history
this is a hybrid approach. previously we used bumpversion to bump the
version and then appended the commithash to the `--version` output. as
of 0.3.1, we used setuptools-scm to append the commit hash. however,
setuptools-scm did not have a way to create multiple versions -- one for
our `--version` output, and one for PKG-INFO.

this commit uses setuptools-scm to automate the global (ex. v0.3.2)
version string calculation, while using an auxiliary file to keep track
of the commit hash for `--version`.

also, add old version files to .gitignore and `make clean`.

even though we don't generate them anymore, they sometimes get generated
when building old releases locally
  • Loading branch information
charles-cooper authored Apr 19, 2022
1 parent 3b6a411 commit b1942c0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ docs/vyper.*.rst

# vyper
vyper/version.py
vyper/vyper_git_version.txt
vyper/vyper_git_commithash.txt
*.spec
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 21 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

import os
import re
import subprocess

from setuptools import find_packages, setup

Expand Down Expand Up @@ -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):
Expand All @@ -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={
Expand Down Expand Up @@ -105,4 +118,5 @@ def _global_version(version):
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
data_files=[("", [hash_file_rel_path])],
)
10 changes: 10 additions & 0 deletions vyper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path as _Path

from vyper.compiler import compile_code, compile_codes # noqa: F401

try:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion vyper/cli/vyper_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion vyper/cli/vyper_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion vyper/cli/vyper_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion vyper/cli/vyper_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b1942c0

Please sign in to comment.