get_version.py: allow specifying version in environment variable

While packaging hy for Alpine Linux I noticed that the VERSIONFILE is
ignored if hy is build in a git repository. On Alpine the packages are
build in the package repository resulting in a wrong hy version. This
change allows specifying the version in an environment variable which is
preferred over git-describe(1) and the VERSIONFILE.
This commit is contained in:
Sören Tempel 2019-09-23 20:35:48 +02:00
parent 84d1a116f6
commit 31041f1713
1 changed files with 15 additions and 12 deletions

View File

@ -5,16 +5,19 @@ import os, subprocess, runpy
os.chdir(os.path.split(os.path.abspath(__file__))[0]) os.chdir(os.path.split(os.path.abspath(__file__))[0])
VERSIONFILE = os.path.join("hy", "version.py") VERSIONFILE = os.path.join("hy", "version.py")
try: if "HY_VERSION" in os.environ:
__version__ = (subprocess.check_output __version__ = os.environ["HY_VERSION"]
(["git", "describe", "--tags", "--dirty"]) else:
.decode('ASCII').strip() try:
.replace('-', '+', 1).replace('-', '.')) __version__ = (subprocess.check_output
with open(VERSIONFILE, "wt") as o: (["git", "describe", "--tags", "--dirty"])
o.write("__version__ = {!r}\n".format(__version__)) .decode('ASCII').strip()
.replace('-', '+', 1).replace('-', '.'))
with open(VERSIONFILE, "wt") as o:
o.write("__version__ = {!r}\n".format(__version__))
except (subprocess.CalledProcessError, OSError): except (subprocess.CalledProcessError, OSError):
if os.path.exists(VERSIONFILE): if os.path.exists(VERSIONFILE):
__version__ = runpy.run_path(VERSIONFILE)['__version__'] __version__ = runpy.run_path(VERSIONFILE)['__version__']
else: else:
__version__ = "unknown" __version__ = "unknown"