Skip to content

Commit

Permalink
Refine
Browse files Browse the repository at this point in the history
  • Loading branch information
hattya committed Dec 17, 2023
1 parent 0fbcf6e commit 823d193
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist/
*.py?
*.sw?
.coverage
MANIFEST
scmver/__version__.py
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Installation
$ pip install scmver
Requiements
-----------
Requirements
------------

- Python 3.8+
- setuptools
Expand All @@ -44,7 +44,7 @@ pyproject.toml
[build-system]
requires = [
"setuptools >= 42",
"setuptools >= 42.0",
"scmver[toml] >= 1.5",
]
build-backend = "setuptools.build_meta"
Expand Down Expand Up @@ -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``
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build-system]
requires = [
"setuptools >= 42",
"setuptools >= 42.0",
"scmver",
]
build-backend = "setuptools.build_meta"
5 changes: 4 additions & 1 deletion scmver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 18 additions & 18 deletions scmver/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#

from __future__ import annotations
import collections.abc
import datetime
import importlib
import os
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 14 additions & 6 deletions tests/test_setuptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ 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"
"""))
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()
Expand Down Expand Up @@ -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)
Expand All @@ -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))

0 comments on commit 823d193

Please sign in to comment.