|
21 | 21 | DEFAULT_SORT_BY = "creatordate" |
22 | 22 | ENV_VARS_REGEXP = re.compile(r"\{env:(?P<name>[^:}]+):?(?P<default>[^}]+\}*)?\}", re.IGNORECASE | re.UNICODE) |
23 | 23 | TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?(?P<fmt>[^:}]+)?\}", re.IGNORECASE | re.UNICODE) |
| 24 | +LOCAL_REGEXP = re.compile(r"[^a-z\d.]+", re.IGNORECASE) |
| 25 | +VERSION_PREFIX_REGEXP = re.compile(r"^[^\d]+", re.IGNORECASE | re.UNICODE) |
24 | 26 |
|
25 | 27 | DEFAULT_CONFIG = { |
26 | 28 | "template": DEFAULT_TEMPLATE, |
@@ -343,16 +345,16 @@ def get_version_from_callback( |
343 | 345 | log.warning(f"Parsing version_callback {version_callback} with type {type(version_callback)}") |
344 | 346 |
|
345 | 347 | if callable(version_callback): |
346 | | - return version_callback() |
347 | | - |
348 | | - result = version_callback |
| 348 | + result = version_callback() |
| 349 | + else: |
| 350 | + result = version_callback |
349 | 351 |
|
350 | | - try: |
351 | | - return load_callable(version_callback, package_name)() |
352 | | - except ValueError: |
353 | | - result = import_reference(version_callback, package_name) |
354 | | - except (ImportError, NameError) as e: |
355 | | - log.warning(f"version_callback is not a valid reference:\n\t{e}") |
| 352 | + try: |
| 353 | + result = load_callable(version_callback, package_name)() |
| 354 | + except ValueError: |
| 355 | + result = import_reference(version_callback, package_name) |
| 356 | + except (ImportError, NameError) as e: |
| 357 | + log.warning(f"version_callback is not a valid reference:\n\t{e}") |
356 | 358 |
|
357 | 359 | from packaging.version import Version |
358 | 360 |
|
@@ -402,8 +404,7 @@ def version_from_git( |
402 | 404 | return starting_version |
403 | 405 |
|
404 | 406 | if not count_commits_from_version_file: |
405 | | - # TODO: drop all leading non-numeric symbols |
406 | | - return tag.lstrip("v") # for tag "v1.0.0" drop leading "v" symbol |
| 407 | + return VERSION_PREFIX_REGEXP.sub("", tag) # for tag "v1.0.0" drop leading "v" symbol |
407 | 408 |
|
408 | 409 | tag_sha = get_latest_file_commit(version_file) |
409 | 410 | else: |
@@ -435,7 +436,6 @@ def version_from_git( |
435 | 436 |
|
436 | 437 | # Ensure local version label only contains permitted characters |
437 | 438 | public, sep, local = version.partition("+") |
438 | | - local_sanitized = re.sub(r"[^a-zA-Z0-9.]", ".", local) |
439 | | - # TODO: drop all leading non-numeric symbols |
440 | | - public_sanitized = public.lstrip("v") # for version "v1.0.0" drop leading "v" symbol |
| 439 | + local_sanitized = LOCAL_REGEXP.sub(".", local) |
| 440 | + public_sanitized = VERSION_PREFIX_REGEXP.sub("", public) # for version "v1.0.0" drop leading "v" symbol |
441 | 441 | return public_sanitized + sep + local_sanitized |
0 commit comments