Skip to content

Commit

Permalink
RFC: refactor scm_version.py to survive pytest collection if setuptoo…
Browse files Browse the repository at this point in the history
…ls_scm isn't installed
  • Loading branch information
neutrinoceros committed Apr 5, 2024
1 parent 19a2721 commit dd5caa4
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions erfa/_dev/scm_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,55 @@
import os.path as pth
from warnings import warn

try:
from setuptools_scm import git, Configuration, get_version as _get_version
from setuptools_scm.version import guess_next_version
def __getattr__(name):
if name not in ("_guess_next_dev", "get_version"):
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

def _guess_next_dev(version, liberfadir=None):
if liberfadir is None:
liberfadir = pathlib.Path(
__file__).parent.parent.parent / 'liberfa' / 'erfa'
try:
from setuptools_scm import git, Configuration, get_version as _get_version
from setuptools_scm.version import guess_next_version

config = Configuration(root=liberfadir)
erfa_version = git.parse(liberfadir, config=config)
if not erfa_version.exact:
warn(f'liberfa/erfa not at a tagged release, but at {erfa_version}')
def _guess_next_dev(version, liberfadir=None):
if liberfadir is None:
liberfadir = pathlib.Path(
__file__).parent.parent.parent / 'liberfa' / 'erfa'

erfa_tag = erfa_version.format_with("{tag}")
version_string = str(version.tag)
config = Configuration(root=liberfadir)
erfa_version = git.parse(liberfadir, config=config)
if not erfa_version.exact:
warn(f'liberfa/erfa not at a tagged release, but at {erfa_version}')

if version.exact:
if not version_string.startswith(erfa_tag):
warn(f'tag {version_string} does not start with '
f'liberfa/erfa tag {erfa_tag}')
erfa_tag = erfa_version.format_with("{tag}")
version_string = str(version.tag)

return version_string
if version.exact:
if not version_string.startswith(erfa_tag):
warn(f'tag {version_string} does not start with '
f'liberfa/erfa tag {erfa_tag}')

else:
if erfa_tag > version_string:
guessed = erfa_tag
elif 'dev' in version_string or len(version_string.split('.')) > 3:
return version.format_next_version(guess_next_version)
else:
guessed = version_string.partition("+")[0] + '.1'
return version.format_with("{guessed}.dev{distance}",
guessed=guessed)
return version_string

get_version = functools.partial(_get_version,
else:
if erfa_tag > version_string:
guessed = erfa_tag
elif 'dev' in version_string or len(version_string.split('.')) > 3:
return version.format_next_version(guess_next_version)
else:
guessed = version_string.partition("+")[0] + '.1'
return version.format_with("{guessed}.dev{distance}",
guessed=guessed)
except Exception as exc:
raise ImportError('setuptools_scm broken or not installed') from exc

else:
if name == "_guess_next_dev":
return _guess_next_dev
elif name == "get_version":

return functools.partial(_get_version,
root=pth.join('..', '..'),
version_scheme=_guess_next_dev,
relative_to=__file__)
except Exception as exc:
raise ImportError('setuptools_scm broken or not installed') from exc
else:
# supposedly unreachable
raise RuntimeError

0 comments on commit dd5caa4

Please sign in to comment.