diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..48355fca --- /dev/null +++ b/setup.py @@ -0,0 +1,51 @@ +from setuptools import setup +from pathlib import Path +import subprocess + + +class VrsSubmoduleFetchError(Exception): + """Raise for errors during submodule metadata extraction""" + + +def _get_vrs_submodule_info() -> tuple[str, str, int]: + """Retrieve the commit hash and tag from the vrs submodule. + + :return: tuple containing commit hash, latest version tag, and rev distance between them + """ + try: + vrs_path = "submodules/vrs" + commit = subprocess.check_output( + ["git", "rev-parse", "HEAD"], cwd=vrs_path, text=True + ).strip() + tag = subprocess.check_output( + ["git", "describe", "--tags", "--abbrev=0"], cwd=vrs_path, text=True + ).strip() + distance = subprocess.check_output( + ["git", "rev-list", f"{tag}..HEAD", "--count"], + cwd=vrs_path, + text=True, + ).strip() + return commit, tag, int(distance) + except Exception as e: + raise VrsSubmoduleFetchError from e + + +def write_metadata_file(): + """Generate a Python module with submodule metadata.""" + commit, tag, distance = _get_vrs_submodule_info() + metadata_path = Path("src/ga4gh/vrs/submodule_metadata.py") + metadata_path.parent.mkdir(parents=True, exist_ok=True) + if distance == 0: + version = tag + else: + version = f"{tag}.dev{distance}+g{commit}" + metadata_path.write_text( + f"# Auto-generated metadata from VRS schema submodule\n" + f"VRS_VERSION = '{version}'\n" + ) + + +write_metadata_file() + +# Use setuptools for the actual build (delegates to pyproject.toml) +setup() diff --git a/src/ga4gh/vrs/submodule_metadata.py b/src/ga4gh/vrs/submodule_metadata.py new file mode 100644 index 00000000..8dbfc37f --- /dev/null +++ b/src/ga4gh/vrs/submodule_metadata.py @@ -0,0 +1,2 @@ +# Auto-generated metadata from VRS schema submodule +VRS_VERSION = '2.0.0.connect.2024-04.1.dev83+gcf968d24a37cefca5a2f363f0e2f36741cd12ad5'