To use simply run
VERSION_PATH='<your package dir>/version.py' python <(curl -Ls https://gist.githubusercontent.com/samuelcolvin/4e1ad439c5489e8d6478cdee3eb952ef/raw/check_version.py)
#!/usr/bin/env python3 | |
""" | |
see https://gist.github.com/samuelcolvin/4e1ad439c5489e8d6478cdee3eb952ef for details | |
""" | |
import json | |
import os | |
import re | |
import sys | |
from importlib.machinery import SourceFileLoader | |
try: | |
from packaging.version import parse as parse_version | |
except ImportError: | |
parse_version = None | |
def main(version_path_env_var='VERSION_PATH', env_var='GITHUB_REF') -> int: | |
version_path = os.getenv(version_path_env_var) | |
if not version_path: | |
print(f'✖ "{version_path_env_var}" env variable not found') | |
return 1 | |
git_ref = os.getenv(env_var, 'none') | |
tag = re.sub('^refs/tags/v*', '', git_ref.lower()) | |
version = SourceFileLoader('version', version_path).load_module().VERSION.lower() | |
if tag == version: | |
if parse_version: | |
packaging_version = parse_version(version) | |
is_prerelease = packaging_version.is_prerelease | |
print( | |
f'✓ {env_var} env var {git_ref!r} matches package version: {tag!r} == {version!r}, ' | |
f'is pre-release: {is_prerelease}' | |
) | |
print(f'::set-output name=IS_PRERELEASE::{json.dumps(is_prerelease)}') | |
print(f'::set-output name=VERSION::{packaging_version}') | |
else: | |
print( | |
f'✓ {env_var} env var {git_ref!r} matches package version: {tag!r} == {version!r} ' | |
f'(packaging not installed, not checking pre-release)' | |
) | |
return 0 | |
else: | |
print(f'✖ {env_var} env var {git_ref!r} does not match package version: {tag!r} != {version!r}') | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |