Skip to content

Commit 7d45c18

Browse files
committed
Use custom log levels because of setuptools v62.0 unexpected change
1 parent fd6d5a7 commit 7d45c18

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

setuptools_git_versioning.py

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
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")
35+
# setuptools v60.2.0 changed default logging level to DEBUG: https://github.com/pypa/setuptools/pull/2974
36+
# to avoid printing information messages to the same output as version number,
37+
# use a custom levels below built-in DEBUG level (10)
38+
INFO = 9
39+
DEBUG = 8
40+
logging.addLevelName(INFO, "INF0")
41+
logging.addLevelName(DEBUG, "DE8UG")
3942
VERBOSITY_LEVELS = {
4043
0: logging.WARNING,
41-
1: INF0,
42-
2: logging.DEBUG,
44+
1: INFO,
45+
2: DEBUG,
4346
}
4447

4548
DEFAULT_CONFIG = {
@@ -144,13 +147,13 @@ def set_default_options(config: dict):
144147
def read_toml(name_or_path: str | os.PathLike = "pyproject.toml", root: str | os.PathLike | None = None) -> dict:
145148
file_path = Path(root or os.getcwd()).joinpath(name_or_path)
146149
if not file_path.exists():
147-
log.log(INF0, "'%s' does not exist", file_path)
150+
log.log(INFO, "'%s' does not exist", file_path)
148151
return {}
149152

150153
if not file_path.is_file():
151154
raise OSError(f"'{file_path}' is not a file")
152155

153-
log.log(INF0, "Trying 'pyproject.toml' ...")
156+
log.log(INFO, "Trying 'pyproject.toml' ...")
154157
import toml
155158

156159
parsed_file = toml.load(file_path)
@@ -164,7 +167,7 @@ def infer_setup_py(name_or_path: str = "setup.py", root: str | os.PathLike | Non
164167
root_path = Path(root or os.getcwd())
165168
file_path = root_path.joinpath(name_or_path)
166169
if not file_path.exists():
167-
log.log(INF0, "'%s' does not exist", file_path)
170+
log.log(INFO, "'%s' does not exist", file_path)
168171
return None
169172

170173
from distutils.core import run_setup
@@ -210,7 +213,7 @@ def parse_config(dist: Distribution, attr: Any, value: Any) -> None:
210213

211214
# real version is generated here
212215
def infer_version(dist: Distribution, root: str | os.PathLike | None = None) -> str | None:
213-
log.log(INF0, "Trying 'setup.py' ...")
216+
log.log(INFO, "Trying 'setup.py' ...")
214217

215218
from distutils.errors import DistutilsOptionError, DistutilsSetupError
216219

@@ -356,7 +359,7 @@ def load_tag_formatter(
356359
package_name: str | None = None,
357360
root: str | os.PathLike | None = None,
358361
) -> Callable:
359-
log.log(INF0, "Parsing tag_formatter '%s' of type '%s'", tag_formatter, type(tag_formatter).__name__)
362+
log.log(INFO, "Parsing tag_formatter '%s' of type '%s'", tag_formatter, type(tag_formatter).__name__)
360363

361364
if callable(tag_formatter):
362365
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(tag_formatter))
@@ -389,7 +392,7 @@ def load_branch_formatter(
389392
package_name: str | None = None,
390393
root: str | os.PathLike | None = None,
391394
) -> Callable:
392-
log.log(INF0, "Parsing branch_formatter '%s' of type '%s'", branch_formatter, type(branch_formatter).__name__)
395+
log.log(INFO, "Parsing branch_formatter '%s' of type '%s'", branch_formatter, type(branch_formatter).__name__)
393396

394397
if callable(branch_formatter):
395398
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(branch_formatter))
@@ -423,30 +426,30 @@ def get_version_from_callback(
423426
package_name: str | None = None,
424427
root: str | os.PathLike | None = None,
425428
) -> str:
426-
log.log(INF0, "Parsing version_callback %s of type %s", version_callback, type(version_callback))
429+
log.log(INFO, "Parsing version_callback %s of type %s", version_callback, type(version_callback))
427430

428431
if callable(version_callback):
429432
log.debug("Value is callable with signature %s", inspect.Signature.from_callable(version_callback))
430433
result = version_callback()
431434
else:
432435

433-
log.log(INF0, "Is not callable, trying to import ...")
436+
log.log(INFO, "Is not callable, trying to import ...")
434437
result = version_callback
435438

436439
try:
437440
callback = load_callable(version_callback, package_name, root=root)
438441
result = callback()
439442
except ValueError as e:
440-
log.log(INF0, "Is not a callable")
443+
log.log(INFO, "Is not a callable")
441444
log.debug(str(e))
442-
log.log(INF0, "Assuming it is a string attribute")
445+
log.log(INFO, "Assuming it is a string attribute")
443446
result = import_reference(version_callback, package_name, root=root)
444447
except (ImportError, NameError) as e:
445448
log.warning("version_callback is not a valid reference: %s", e)
446449

447450
from packaging.version import Version
448451

449-
log.log(INF0, "Result %s", result)
452+
log.log(INFO, "Result %s", result)
450453
return Version(result).public
451454

452455

@@ -468,12 +471,12 @@ def version_from_git(
468471
# Check if PKG-INFO file exists and Version is present in it
469472
pkg_info = Path(root or os.getcwd()).joinpath("PKG-INFO")
470473
if pkg_info.exists():
471-
log.log(INF0, "File '%s' is found, reading its content", pkg_info)
474+
log.log(INFO, "File '%s' is found, reading its content", pkg_info)
472475
lines = pkg_info.read_text().splitlines()
473476
for line in lines:
474477
if line.startswith("Version:"):
475478
result = line[8:].strip()
476-
log.log(INF0, "Return '%s'", result)
479+
log.log(INFO, "Return '%s'", result)
477480
return result
478481

479482
if version_callback is not None:
@@ -484,106 +487,106 @@ def version_from_git(
484487
return get_version_from_callback(version_callback, package_name, root=root)
485488

486489
from_file = False
487-
log.log(INF0, "Getting latest tag")
490+
log.log(INFO, "Getting latest tag")
488491
log.debug("Sorting tags by '%s'", sort_by)
489492
tag = get_tag(sort_by=sort_by, root=root)
490493

491494
if tag is None:
492-
log.log(INF0, "No tag, checking for 'version_file'")
495+
log.log(INFO, "No tag, checking for 'version_file'")
493496
if version_file is None:
494-
log.log(INF0, "No 'version_file' set, return starting_version '%s'", starting_version)
497+
log.log(INFO, "No 'version_file' set, return starting_version '%s'", starting_version)
495498
return starting_version
496499

497500
if not Path(version_file).exists():
498501
log.log(
499-
INF0,
502+
INFO,
500503
"version_file '%s' does not exist, return starting_version '%s'",
501504
version_file,
502505
starting_version,
503506
)
504507
return starting_version
505508

506-
log.log(INF0, "version_file '%s' does exist, reading its content", version_file)
509+
log.log(INFO, "version_file '%s' does exist, reading its content", version_file)
507510
from_file = True
508511
tag = read_version_from_file(version_file, root=root)
509512

510513
if not tag:
511-
log.log(INF0, "File is empty, return starting_version '%s'", version_file, starting_version)
514+
log.log(INFO, "File is empty, return starting_version '%s'", version_file, starting_version)
512515
return starting_version
513516

514517
log.debug("File content: '%s'", tag)
515518
if not count_commits_from_version_file:
516519
result = VERSION_PREFIX_REGEXP.sub("", tag) # for tag "v1.0.0" drop leading "v" symbol
517-
log.log(INF0, "Return '%s'", result)
520+
log.log(INFO, "Return '%s'", result)
518521
return result
519522

520523
tag_sha = get_latest_file_commit(version_file, root=root)
521524
log.debug("File content: '%s'", tag)
522525
else:
523-
log.log(INF0, "Latest tag: '%s'", tag)
526+
log.log(INFO, "Latest tag: '%s'", tag)
524527
tag_sha = get_sha(tag, root=root)
525-
log.log(INF0, "Tag SHA-256: '%s'", tag_sha)
528+
log.log(INFO, "Tag SHA-256: '%s'", tag_sha)
526529

527530
if tag_formatter is not None:
528531
tag_fmt = load_tag_formatter(tag_formatter, package_name, root=root)
529532
tag = tag_fmt(tag)
530533
log.debug("Tag after formatting: '%s'", tag)
531534

532535
dirty = is_dirty(root=root)
533-
log.log(INF0, "Is dirty: %s", dirty)
536+
log.log(INFO, "Is dirty: %s", dirty)
534537

535538
head_sha = get_sha(root=root)
536-
log.log(INF0, "HEAD SHA-256: '%s'", head_sha)
539+
log.log(INFO, "HEAD SHA-256: '%s'", head_sha)
537540

538541
full_sha = head_sha if head_sha is not None else ""
539542
ccount = count_since(tag_sha, root=root) if tag_sha is not None else None
540-
log.log(INF0, "Commits count between HEAD and latest tag: %s", ccount)
543+
log.log(INFO, "Commits count between HEAD and latest tag: %s", ccount)
541544

542545
on_tag = head_sha is not None and head_sha == tag_sha and not from_file
543-
log.log(INF0, "HEAD is tagged: %s", on_tag)
546+
log.log(INFO, "HEAD is tagged: %s", on_tag)
544547

545548
branch = get_branch(root=root)
546-
log.log(INF0, "Current branch: '%s'", branch)
549+
log.log(INFO, "Current branch: '%s'", branch)
547550

548551
if branch_formatter is not None and branch is not None:
549552
branch_fmt = load_branch_formatter(branch_formatter, package_name, root=root)
550553
branch = branch_fmt(branch)
551-
log.log(INF0, "Branch after formatting: '%s'", branch)
554+
log.log(INFO, "Branch after formatting: '%s'", branch)
552555

553556
if dirty:
554-
log.log(INF0, "Using template from 'dirty_template' option")
557+
log.log(INFO, "Using template from 'dirty_template' option")
555558
t = dirty_template
556559
elif not on_tag and ccount is not None:
557-
log.log(INF0, "Using template from 'dev_template' option")
560+
log.log(INFO, "Using template from 'dev_template' option")
558561
t = dev_template
559562
else:
560-
log.log(INF0, "Using template from 'template' option")
563+
log.log(INFO, "Using template from 'template' option")
561564
t = template
562565

563566
version = resolve_substitutions(t, sha=full_sha[:8], tag=tag, ccount=ccount, branch=branch, full_sha=full_sha)
564-
log.log(INF0, "Version number after resolving substitutions: '%s'", version)
567+
log.log(INFO, "Version number after resolving substitutions: '%s'", version)
565568

566569
# Ensure local version label only contains permitted characters
567570
public, sep, local = version.partition("+")
568571
local_sanitized = LOCAL_REGEXP.sub(".", local)
569572
if local_sanitized != local:
570-
log.log(INF0, "Local version part after sanitization: '%s'", local_sanitized)
573+
log.log(INFO, "Local version part after sanitization: '%s'", local_sanitized)
571574

572575
public_sanitized = VERSION_PREFIX_REGEXP.sub("", public) # for version "v1.0.0" drop leading "v" symbol
573576
if public_sanitized != public:
574-
log.log(INF0, "Public version part after sanitization: '%s'", public_sanitized)
577+
log.log(INFO, "Public version part after sanitization: '%s'", public_sanitized)
575578

576579
result = (public_sanitized + sep + local_sanitized) or "0.0.0"
577-
log.log(INF0, "Result: '%s'", result)
580+
log.log(INFO, "Result: '%s'", result)
578581
return result
579582

580583

581584
def main(config: dict | None = None, root: str | os.PathLike | None = None) -> Version:
582585
from packaging.version import Version
583586

584587
if not config:
585-
log.log(INF0, "No explicit config passed")
586-
log.log(INF0, "Searching for config files in '%s' folder", root or os.getcwd())
588+
log.log(INFO, "No explicit config passed")
589+
log.log(INFO, "Searching for config files in '%s' folder", root or os.getcwd())
587590
result = infer_setup_py(root=root)
588591
if result is not None:
589592
return Version(result)

0 commit comments

Comments
 (0)