From 823d19381f895dbcd0f5e746823263d7f256aee7 Mon Sep 17 00:00:00 2001 From: Akinori Hattori Date: Sun, 17 Dec 2023 18:42:49 +0900 Subject: [PATCH] Refine --- .gitignore | 1 + README.rst | 12 ++++++------ pyproject.toml | 2 +- scmver/cli.py | 5 ++++- scmver/core.py | 36 ++++++++++++++++++------------------ tests/test_core.py | 2 +- tests/test_setuptools.py | 20 ++++++++++++++------ 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 5fc69f2..02897dd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ dist/ *.py? *.sw? .coverage +MANIFEST scmver/__version__.py diff --git a/README.rst b/README.rst index d28376f..67f83e5 100644 --- a/README.rst +++ b/README.rst @@ -27,8 +27,8 @@ Installation $ pip install scmver -Requiements ------------ +Requirements +------------ - Python 3.8+ - setuptools @@ -44,7 +44,7 @@ pyproject.toml [build-system] requires = [ - "setuptools >= 42", + "setuptools >= 42.0", "scmver[toml] >= 1.5", ] build-backend = "setuptools.build_meta" @@ -99,7 +99,7 @@ root Default: ``'.'`` spec - A version specifier to construct the public version indentifiers. It will be + A version specifier to construct the public version identifiers. It will be incremented by the number of commits from the latest tag. ``major`` @@ -169,7 +169,7 @@ fallback ``string`` It is in the ``'package.module:some.attribute'`` format - (ex: ``'scmver:version'``). + (ex: ``'scmver:__version__'``). ``list`` It consists of a ``string`` which is described above, and a path to import @@ -215,4 +215,4 @@ subversion.tags License ------- -scmver is distrutbuted under the terms of the MIT License. +scmver is distributed under the terms of the MIT License. diff --git a/pyproject.toml b/pyproject.toml index b02dfd4..48de2da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] requires = [ - "setuptools >= 42", + "setuptools >= 42.0", "scmver", ] build-backend = "setuptools.build_meta" diff --git a/scmver/cli.py b/scmver/cli.py index cb1dd5c..ed8559b 100644 --- a/scmver/cli.py +++ b/scmver/cli.py @@ -10,7 +10,10 @@ import re from typing import Any, Callable, Dict, Optional, Sequence -import click +try: + import click +except ImportError: + raise SystemExit("Missing dependencies, try 'pip install scmver[cli]'") from . import __version__, core diff --git a/scmver/core.py b/scmver/core.py index 9c586b1..38d1d76 100644 --- a/scmver/core.py +++ b/scmver/core.py @@ -7,6 +7,7 @@ # from __future__ import annotations +import collections.abc import datetime import importlib import os @@ -105,14 +106,10 @@ def take(d: Mapping[str, str], *keys: str) -> Dict[str, Any]: fallback = kwargs['fallback'] if callable(fallback): return cast(str, fallback()) - else: - if isinstance(fallback, str): - spec = fallback - path = None - else: - spec = fallback[0] - path = os.path.join(root, fallback[1]) - return load_version(spec, path) + elif isinstance(fallback, str): + return load_version(fallback) + elif isinstance(fallback, collections.abc.Sequence): + return load_version(fallback[0], os.path.join(root, fallback[1])) return None @@ -166,16 +163,19 @@ def load_project(path: Path = 'pyproject.toml') -> Optional[Dict[str, Any]]: except ImportError: return None - if os.path.isfile(path): - with open(path, 'rb') as fp: - proj = toml.load(fp) - if 'tool' in proj: - if 'scmver' in proj['tool']: - root = os.path.dirname(os.path.abspath(path)) - scmver: Dict[str, Any] = proj['tool']['scmver'] - scmver['root'] = os.path.join(root, scmver['root']) if 'root' in scmver else root - return scmver - return None + if not os.path.isfile(path): + return None + with open(path, 'rb') as fp: + proj = toml.load(fp) + if not ('tool' in proj + and 'scmver' in proj['tool']): + return None + + scmver: Dict[str, Any] = proj['tool']['scmver'] + # root + root = os.path.dirname(os.path.abspath(path)) + scmver['root'] = os.path.join(root, scmver['root']) if 'root' in scmver else root + return scmver def stat(path: Path, **kwargs: Any) -> Optional[SCMInfo]: diff --git a/tests/test_core.py b/tests/test_core.py index 1613e0f..d15c5db 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -130,7 +130,7 @@ def test_load_project(self): fp.write(textwrap.dedent("""\ [build-system] requires = [ - "setuptools >= 42", + "setuptools >= 42.0", "scmver[toml] >= 1.5", ] build-backend = "setuptools.build_meta" diff --git a/tests/test_setuptools.py b/tests/test_setuptools.py index 5103daf..e0d0232 100644 --- a/tests/test_setuptools.py +++ b/tests/test_setuptools.py @@ -50,7 +50,7 @@ def finalize_version(self, scmver): fp.write(textwrap.dedent("""\ [build-system] requires = [ - "setuptools >= 42", + "setuptools >= 42.0", "scmver[toml] >= 1.5", ] build-backend = "setuptools.build_meta" @@ -58,7 +58,12 @@ def finalize_version(self, scmver): if scmver is not None: fp.write('[tool.scmver]\n') for k, v in scmver.items(): - fp.write(f'{k} = {v!r}\n') + if isinstance(v, str): + fp.write(f'{k} = "{v}"\n') + elif isinstance(v, list): + fp.write(f'{k} = [') + fp.write(', '.join(f'"{v}"' for v in v)) + fp.write(']\n') fp.flush() dist = distutils.dist.Distribution() @@ -129,8 +134,8 @@ def test_scmver_fallback(self): value = {'fallback': lambda: '1.0'} self.assertEqual(self.scmver(value), '1.0') - value = {'fallback': 'toast:version'} - with open('toast.py', 'w') as fp: + value = {'fallback': 'bacon:version'} + with open('bacon.py', 'w') as fp: fp.write(core._TEMPLATE.format(version='1.1')) fp.flush() sys.path.append(self.root) @@ -139,8 +144,11 @@ def test_scmver_fallback(self): finally: sys.path.pop() - value = {'fallback': ['beans:version', '.']} - with open('beans.py', 'w') as fp: + value = {'fallback': ['sausage:version', '.']} + with open('sausage.py', 'w') as fp: fp.write(core._TEMPLATE.format(version='1.2')) fp.flush() self.assertEqual(self.scmver(value), '1.2') + + value = {'fallback': None} + self.assertIsNone(self.scmver(value))