Skip to content

Commit fd6d5a7

Browse files
committed
Fix tests
1 parent 1be9c05 commit fd6d5a7

File tree

2 files changed

+82
-72
lines changed

2 files changed

+82
-72
lines changed

docs/command.rst

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,60 +35,60 @@ This script is a wrapper for ``setuptools_git_versioning`` module, you just call
3535
3636
$ setuptools-git-versioning /path/to/mypackage -v
3737
38-
INFO: No explicit config passed
39-
INFO: Searching for config files in '/path/to/mypackage' folder
40-
INFO: Trying 'setup.py' ...
41-
INFO: '/path/to/mypackage/pyproject.toml' does not exist
42-
INFO: Getting latest tag
43-
INFO: Latest tag: '1.0.0'
44-
INFO: Tag SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
45-
INFO: Parsing tag_formatter 'util:tag_formatter' of type 'str'
46-
INFO: Is dirty: False
47-
INFO: HEAD SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
48-
INFO: Commits count between HEAD and latest tag: 0
49-
INFO: HEAD is tagged: True
50-
INFO: Current branch: 'master'
51-
INFO: Using template from 'template' option
52-
INFO: Version number after resolving substitutions: '1.0.0'
53-
INFO: Result: '1.0.0'
38+
INF0: No explicit config passed
39+
INF0: Searching for config files in '/path/to/mypackage' folder
40+
INF0: Trying 'setup.py' ...
41+
INF0: '/path/to/mypackage/pyproject.toml' does not exist
42+
INF0: Getting latest tag
43+
INF0: Latest tag: '1.0.0'
44+
INF0: Tag SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
45+
INF0: Parsing tag_formatter 'util:tag_formatter' of type 'str'
46+
INF0: Is dirty: False
47+
INF0: HEAD SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
48+
INF0: Commits count between HEAD and latest tag: 0
49+
INF0: HEAD is tagged: Truelog.INF0("'%s' does not exist", file_path)
50+
INF0: Current branch: 'master'
51+
INF0: Using template from 'template' option
52+
INF0: Version number after resolving substitutions: '1.0.0'
53+
INF0: Result: '1.0.0'
5454
5555
1.0.0
5656
5757
58-
``-vv`` shows even more debug messages:
58+
``-vV`` shows even more debug messages:
5959
6060
.. code:: bash
6161
62-
$ setuptools-git-versioning /path/to/mypackage -vvv
62+
$ setuptools-git-versioning /path/to/mypackage -vV
6363
64-
INFO: No explicit config passed
65-
INFO: Searching for config files in '/path/to/mypackage' folder
66-
INFO: Trying 'setup.py' ...
64+
INF0: No explicit config passed
65+
INF0: Searching for config files in '/path/to/mypackage' folder
66+
INF0: Trying 'setup.py' ...
6767
DEBUG: Adding '/path/to/mypackage' folder to sys.path
68-
INFO: '/path/to/mypackage/pyproject.toml' does not exist
69-
INFO: Getting latest tag
68+
INF0: '/path/to/mypackage/pyproject.toml' does not exist
69+
INF0: Getting latest tag
7070
DEBUG: Sorting tags by 'creatordate'
7171
DEBUG: Executing 'git tag --sort=-creatordate --merged' at '/path/to/mypackage'
72-
INFO: Latest tag: '1.0.0'
72+
INF0: Latest tag: '1.0.0'
7373
DEBUG: Executing 'git rev-list -n 1 "1.0.0"' at '/path/to/mypackage'
74-
INFO: Tag SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
75-
INFO: Parsing tag_formatter 'util:tag_formatter' of type 'str'
74+
INF0: Tag SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
75+
INF0: Parsing tag_formatter 'util:tag_formatter' of type 'str'
7676
DEBUG: Executing 'from mypkg.util import tag_formatter'
7777
DEBUG: Tag after formatting: '1.0.0'
7878
DEBUG: Executing 'git status --short' at '/path/to/mypackage'
79-
INFO: Is dirty: False
79+
INF0: Is dirty: False
8080
DEBUG: Executing 'git rev-list -n 1 "HEAD"' at '/path/to/mypackage'
81-
INFO: HEAD SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
81+
INF0: HEAD SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
8282
DEBUG: Executing 'git rev-list --count HEAD "^8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4"' at '/path/to/mypackage'
83-
INFO: Commits count between HEAD and latest tag: 0
84-
INFO: HEAD is tagged: True
83+
INF0: Commits count between HEAD and latest tag: 0
84+
INF0: HEAD is tagged: True
8585
DEBUG: Executing 'git rev-parse --abbrev-ref HEAD' at '/path/to/mypackage'
86-
INFO: Current branch: 'master'
87-
INFO: Using template from 'template' option
86+
INF0: Current branch: 'master'
87+
INF0: Using template from 'template' option
8888
DEBUG: Template: '{tag}'
8989
DEBUG: Args:()
90-
INFO: Version number after resolving substitutions: '1.0.0'
91-
INFO: Result: '1.0.0'
90+
INF0: Version number after resolving substitutions: '1.0.0'
91+
INF0: Result: '1.0.0'
9292
9393
1.0.0
9494

setuptools_git_versioning.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@
3232
VERSION_PREFIX_REGEXP = re.compile(r"^[^\d]+", re.IGNORECASE | re.UNICODE)
3333

3434
LOG_FORMAT = "[%(asctime)s] %(levelname)+8s: %(message)s"
35+
# setup.py changes log level to INFO, so using a custom level between INFO and DEBUG
36+
# it is called INF0 (zero at the end)
37+
INF0 = 15
38+
logging.addLevelName(INF0, "INF0")
3539
VERBOSITY_LEVELS = {
3640
0: logging.WARNING,
37-
1: logging.INFO,
41+
1: INF0,
3842
2: logging.DEBUG,
3943
}
4044

@@ -140,12 +144,13 @@ def set_default_options(config: dict):
140144
def read_toml(name_or_path: str | os.PathLike = "pyproject.toml", root: str | os.PathLike | None = None) -> dict:
141145
file_path = Path(root or os.getcwd()).joinpath(name_or_path)
142146
if not file_path.exists():
143-
log.info("'%s' does not exist", file_path)
147+
log.log(INF0, "'%s' does not exist", file_path)
144148
return {}
145149

146150
if not file_path.is_file():
147151
raise OSError(f"'{file_path}' is not a file")
148152

153+
log.log(INF0, "Trying 'pyproject.toml' ...")
149154
import toml
150155

151156
parsed_file = toml.load(file_path)
@@ -159,7 +164,7 @@ def infer_setup_py(name_or_path: str = "setup.py", root: str | os.PathLike | Non
159164
root_path = Path(root or os.getcwd())
160165
file_path = root_path.joinpath(name_or_path)
161166
if not file_path.exists():
162-
log.info("'%s' does not exist", file_path)
167+
log.log(INF0, "'%s' does not exist", file_path)
163168
return None
164169

165170
from distutils.core import run_setup
@@ -205,6 +210,8 @@ def parse_config(dist: Distribution, attr: Any, value: Any) -> None:
205210

206211
# real version is generated here
207212
def infer_version(dist: Distribution, root: str | os.PathLike | None = None) -> str | None:
213+
log.log(INF0, "Trying 'setup.py' ...")
214+
208215
from distutils.errors import DistutilsOptionError, DistutilsSetupError
209216

210217
config = getattr(dist, "setuptools_git_versioning", None) or getattr(dist, "version_config", None)
@@ -349,7 +356,7 @@ def load_tag_formatter(
349356
package_name: str | None = None,
350357
root: str | os.PathLike | None = None,
351358
) -> Callable:
352-
log.info("Parsing tag_formatter '%s' of type '%s'", tag_formatter, type(tag_formatter).__name__)
359+
log.log(INF0, "Parsing tag_formatter '%s' of type '%s'", tag_formatter, type(tag_formatter).__name__)
353360

354361
if callable(tag_formatter):
355362
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(tag_formatter))
@@ -382,7 +389,7 @@ def load_branch_formatter(
382389
package_name: str | None = None,
383390
root: str | os.PathLike | None = None,
384391
) -> Callable:
385-
log.info("Parsing branch_formatter '%s' of type '%s'", branch_formatter, type(branch_formatter).__name__)
392+
log.log(INF0, "Parsing branch_formatter '%s' of type '%s'", branch_formatter, type(branch_formatter).__name__)
386393

387394
if callable(branch_formatter):
388395
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(branch_formatter))
@@ -416,30 +423,30 @@ def get_version_from_callback(
416423
package_name: str | None = None,
417424
root: str | os.PathLike | None = None,
418425
) -> str:
419-
log.info("Parsing version_callback %s of type %s", version_callback, type(version_callback))
426+
log.log(INF0, "Parsing version_callback %s of type %s", version_callback, type(version_callback))
420427

421428
if callable(version_callback):
422429
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(version_callback))
423430
result = version_callback()
424431
else:
425432

426-
log.info("Is not callable, trying to import ...")
433+
log.log(INF0, "Is not callable, trying to import ...")
427434
result = version_callback
428435

429436
try:
430437
callback = load_callable(version_callback, package_name, root=root)
431438
result = callback()
432439
except ValueError as e:
433-
log.info("Is not a callable")
440+
log.log(INF0, "Is not a callable")
434441
log.debug(str(e))
435-
log.info("Assuming it is a string attribute")
442+
log.log(INF0, "Assuming it is a string attribute")
436443
result = import_reference(version_callback, package_name, root=root)
437444
except (ImportError, NameError) as e:
438445
log.warning("version_callback is not a valid reference: %s", e)
439446

440447
from packaging.version import Version
441448

442-
log.info("Result %s", result)
449+
log.log(INF0, "Result %s", result)
443450
return Version(result).public
444451

445452

@@ -461,12 +468,12 @@ def version_from_git(
461468
# Check if PKG-INFO file exists and Version is present in it
462469
pkg_info = Path(root or os.getcwd()).joinpath("PKG-INFO")
463470
if pkg_info.exists():
464-
log.info("File '%s' is found, reading its content", pkg_info)
471+
log.log(INF0, "File '%s' is found, reading its content", pkg_info)
465472
lines = pkg_info.read_text().splitlines()
466473
for line in lines:
467474
if line.startswith("Version:"):
468475
result = line[8:].strip()
469-
log.info("Return '%s'", result)
476+
log.log(INF0, "Return '%s'", result)
470477
return result
471478

472479
if version_callback is not None:
@@ -477,107 +484,110 @@ def version_from_git(
477484
return get_version_from_callback(version_callback, package_name, root=root)
478485

479486
from_file = False
480-
log.info("Getting latest tag")
487+
log.log(INF0, "Getting latest tag")
481488
log.debug("Sorting tags by '%s'", sort_by)
482489
tag = get_tag(sort_by=sort_by, root=root)
483490

484491
if tag is None:
485-
log.info("No tag, checking for 'version_file'")
492+
log.log(INF0, "No tag, checking for 'version_file'")
486493
if version_file is None:
487-
log.info("No 'version_file' set, return starting_version '%s'", starting_version)
494+
log.log(INF0, "No 'version_file' set, return starting_version '%s'", starting_version)
488495
return starting_version
489496

490497
if not Path(version_file).exists():
491-
log.info("version_file '%s' does not exist, return starting_version '%s'", version_file, starting_version)
498+
log.log(
499+
INF0,
500+
"version_file '%s' does not exist, return starting_version '%s'",
501+
version_file,
502+
starting_version,
503+
)
492504
return starting_version
493505

494-
log.info("version_file '%s' does exist, reading its content", version_file)
506+
log.log(INF0, "version_file '%s' does exist, reading its content", version_file)
495507
from_file = True
496508
tag = read_version_from_file(version_file, root=root)
497509

498510
if not tag:
499-
log.info("File is empty, return starting_version '%s'", version_file, starting_version)
511+
log.log(INF0, "File is empty, return starting_version '%s'", version_file, starting_version)
500512
return starting_version
501513

502514
log.debug("File content: '%s'", tag)
503515
if not count_commits_from_version_file:
504516
result = VERSION_PREFIX_REGEXP.sub("", tag) # for tag "v1.0.0" drop leading "v" symbol
505-
log.info("Return '%s'", result)
517+
log.log(INF0, "Return '%s'", result)
506518
return result
507519

508520
tag_sha = get_latest_file_commit(version_file, root=root)
509521
log.debug("File content: '%s'", tag)
510522
else:
511-
log.info("Latest tag: '%s'", tag)
523+
log.log(INF0, "Latest tag: '%s'", tag)
512524
tag_sha = get_sha(tag, root=root)
513-
log.info("Tag SHA-256: '%s'", tag_sha)
525+
log.log(INF0, "Tag SHA-256: '%s'", tag_sha)
514526

515527
if tag_formatter is not None:
516528
tag_fmt = load_tag_formatter(tag_formatter, package_name, root=root)
517529
tag = tag_fmt(tag)
518530
log.debug("Tag after formatting: '%s'", tag)
519531

520532
dirty = is_dirty(root=root)
521-
log.info("Is dirty: %s", dirty)
533+
log.log(INF0, "Is dirty: %s", dirty)
522534

523535
head_sha = get_sha(root=root)
524-
log.info("HEAD SHA-256: '%s'", head_sha)
536+
log.log(INF0, "HEAD SHA-256: '%s'", head_sha)
525537

526538
full_sha = head_sha if head_sha is not None else ""
527539
ccount = count_since(tag_sha, root=root) if tag_sha is not None else None
528-
log.info("Commits count between HEAD and latest tag: %s", ccount)
540+
log.log(INF0, "Commits count between HEAD and latest tag: %s", ccount)
529541

530542
on_tag = head_sha is not None and head_sha == tag_sha and not from_file
531-
log.info("HEAD is tagged: %s", on_tag)
543+
log.log(INF0, "HEAD is tagged: %s", on_tag)
532544

533545
branch = get_branch(root=root)
534-
log.info("Current branch: '%s'", branch)
546+
log.log(INF0, "Current branch: '%s'", branch)
535547

536548
if branch_formatter is not None and branch is not None:
537549
branch_fmt = load_branch_formatter(branch_formatter, package_name, root=root)
538550
branch = branch_fmt(branch)
539-
log.info("Branch after formatting: '%s'", branch)
551+
log.log(INF0, "Branch after formatting: '%s'", branch)
540552

541553
if dirty:
542-
log.info("Using template from 'dirty_template' option")
554+
log.log(INF0, "Using template from 'dirty_template' option")
543555
t = dirty_template
544556
elif not on_tag and ccount is not None:
545-
log.info("Using template from 'dev_template' option")
557+
log.log(INF0, "Using template from 'dev_template' option")
546558
t = dev_template
547559
else:
548-
log.info("Using template from 'template' option")
560+
log.log(INF0, "Using template from 'template' option")
549561
t = template
550562

551563
version = resolve_substitutions(t, sha=full_sha[:8], tag=tag, ccount=ccount, branch=branch, full_sha=full_sha)
552-
log.info("Version number after resolving substitutions: '%s'", version)
564+
log.log(INF0, "Version number after resolving substitutions: '%s'", version)
553565

554566
# Ensure local version label only contains permitted characters
555567
public, sep, local = version.partition("+")
556568
local_sanitized = LOCAL_REGEXP.sub(".", local)
557569
if local_sanitized != local:
558-
log.info("Local version part after sanitization: '%s'", local_sanitized)
570+
log.log(INF0, "Local version part after sanitization: '%s'", local_sanitized)
559571

560572
public_sanitized = VERSION_PREFIX_REGEXP.sub("", public) # for version "v1.0.0" drop leading "v" symbol
561573
if public_sanitized != public:
562-
log.info("Public version part after sanitization: '%s'", public_sanitized)
574+
log.log(INF0, "Public version part after sanitization: '%s'", public_sanitized)
563575

564576
result = (public_sanitized + sep + local_sanitized) or "0.0.0"
565-
log.info("Result: '%s'", result)
577+
log.log(INF0, "Result: '%s'", result)
566578
return result
567579

568580

569581
def main(config: dict | None = None, root: str | os.PathLike | None = None) -> Version:
570582
from packaging.version import Version
571583

572584
if not config:
573-
log.info("No explicit config passed")
574-
log.info("Searching for config files in '%s' folder", root or os.getcwd())
575-
log.info("Trying 'setup.py' ...")
585+
log.log(INF0, "No explicit config passed")
586+
log.log(INF0, "Searching for config files in '%s' folder", root or os.getcwd())
576587
result = infer_setup_py(root=root)
577588
if result is not None:
578589
return Version(result)
579590

580-
log.info("Trying 'pyproject.toml' ...")
581591
config = read_toml(root=root)
582592

583593
if not config or not config.pop("enabled", True):

0 commit comments

Comments
 (0)