|
from __future__ import annotations |
|
|
|
import importlib.metadata |
|
|
|
|
|
__all__ = ["tag", "version", "commit"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
released = True |
|
|
|
tag = version = commit = "15.0.1" |
|
|
|
|
|
if not released: |
|
import pathlib |
|
import re |
|
import subprocess |
|
|
|
def get_version(tag: str) -> str: |
|
|
|
|
|
file_path = pathlib.Path(__file__) |
|
root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2] |
|
|
|
|
|
try: |
|
version = importlib.metadata.version("websockets") |
|
except ImportError: |
|
pass |
|
else: |
|
|
|
files = importlib.metadata.files("websockets") |
|
if files: |
|
version_files = [f for f in files if f.name == file_path.name] |
|
if version_files: |
|
version_file = version_files[0] |
|
if version_file.locate() == file_path: |
|
return version |
|
|
|
|
|
try: |
|
description = subprocess.run( |
|
["git", "describe", "--dirty", "--tags", "--long"], |
|
capture_output=True, |
|
cwd=root_dir, |
|
timeout=1, |
|
check=True, |
|
text=True, |
|
).stdout.strip() |
|
|
|
except ( |
|
FileNotFoundError, |
|
subprocess.CalledProcessError, |
|
subprocess.TimeoutExpired, |
|
): |
|
pass |
|
else: |
|
description_re = r"[0-9.]+-([0-9]+)-(g[0-9a-f]{7,}(?:-dirty)?)" |
|
match = re.fullmatch(description_re, description) |
|
if match is None: |
|
raise ValueError(f"Unexpected git description: {description}") |
|
distance, remainder = match.groups() |
|
remainder = remainder.replace("-", ".") |
|
return f"{tag}.dev{distance}+{remainder}" |
|
|
|
|
|
return f"{tag}.dev0+gunknown" |
|
|
|
version = get_version(tag) |
|
|
|
def get_commit(tag: str, version: str) -> str: |
|
|
|
version_re = r"[0-9.]+\.dev[0-9]+\+g([0-9a-f]{7,}|unknown)(?:\.dirty)?" |
|
match = re.fullmatch(version_re, version) |
|
if match is None: |
|
raise ValueError(f"Unexpected version: {version}") |
|
(commit,) = match.groups() |
|
return tag if commit == "unknown" else commit |
|
|
|
commit = get_commit(tag, version) |
|
|