|
2 | 2 | """ |
3 | 3 | Sphinx documentation build script |
4 | 4 | """ |
5 | | -import importlib |
6 | | -import importlib.util |
7 | 5 | import json |
8 | 6 | import os |
9 | 7 | import re |
|
28 | 26 | DIR_MAKE_BASE = os.path.dirname(os.path.realpath(__file__)) |
29 | 27 | DIR_REPO_ROOT = os.path.realpath(os.path.join(os.getcwd(), os.pardir)) |
30 | 28 | DIR_REPO_PARENT = os.path.realpath(os.path.join(DIR_REPO_ROOT, os.pardir)) |
31 | | -FACET_PROJECT = os.path.split(os.path.realpath(DIR_REPO_ROOT))[1] |
| 29 | +PROJECT_NAME = os.path.split(os.path.realpath(DIR_REPO_ROOT))[1] |
32 | 30 | DIR_PACKAGE_SRC = os.path.join(DIR_REPO_ROOT, "src") |
33 | 31 | DIR_DOCS = os.path.join(DIR_REPO_ROOT, "docs") |
34 | 32 | DIR_SPHINX_SOURCE = os.path.join(cwd, "source") |
|
51 | 49 | # noinspection SpellCheckingInspection |
52 | 50 | ENV_PYTHON_PATH = "PYTHONPATH" |
53 | 51 |
|
| 52 | +# regex pattern to match the version declaration in a top-level __init__.py |
| 53 | +RE_VERSION_DECLARATION = re.compile(r"\b__version__\s*=\s*(?:\"([^\"]*)\"|'([^']*)')") |
| 54 | + |
54 | 55 | T = TypeVar("T") |
55 | 56 |
|
56 | 57 |
|
@@ -529,25 +530,34 @@ def get_package_version() -> pkg_version.Version: |
529 | 530 | """ |
530 | 531 | Retrieve the package version for the project from __init__ or _version |
531 | 532 | """ |
532 | | - project_src = os.path.abspath(os.path.join(DIR_REPO_ROOT, "src")) |
| 533 | + init_path = os.path.abspath( |
| 534 | + os.path.join(DIR_REPO_ROOT, "src", PROJECT_NAME, "__init__.py") |
| 535 | + ) |
| 536 | + |
| 537 | + print(f"Retrieving package version from {init_path}") |
| 538 | + |
| 539 | + with open(init_path, "rt") as init_file: |
| 540 | + init_lines = init_file.readlines() |
533 | 541 |
|
534 | | - if FACET_PROJECT in ("sklearndf", "flow"): |
535 | | - # for sklearndf and flow __init__ can't be trivially imported due to import |
536 | | - # dependencies. Load the version as defined in FACET_PROJECT._version module |
537 | | - spec = importlib.util.spec_from_file_location( |
538 | | - "_version", os.path.join(project_src, FACET_PROJECT, "_version.py") |
| 542 | + matches = { |
| 543 | + match[1] or match[2] |
| 544 | + for match in (RE_VERSION_DECLARATION.match(line) for line in init_lines) |
| 545 | + if match |
| 546 | + } |
| 547 | + |
| 548 | + if len(matches) == 0: |
| 549 | + raise RuntimeError(f"No valid __version__ declaration found in {init_path}") |
| 550 | + |
| 551 | + elif len(matches) > 1: |
| 552 | + raise RuntimeError( |
| 553 | + f"Multiple conflicting __version__ declarations found in {init_path}: " |
| 554 | + f"{matches}" |
539 | 555 | ) |
| 556 | + |
540 | 557 | else: |
541 | | - # pytools/facet: retrieve version from __init__.py |
542 | | - spec = importlib.util.spec_from_file_location( |
543 | | - "_version", os.path.join(project_src, FACET_PROJECT, "__init__.py") |
544 | | - ) |
| 558 | + package_version = next(iter(matches)) |
545 | 559 |
|
546 | | - version_module = importlib.util.module_from_spec(spec) |
547 | | - # noinspection PyUnresolvedReferences |
548 | | - spec.loader.exec_module(version_module) |
549 | | - # noinspection PyUnresolvedReferences |
550 | | - return pkg_version.parse(version_module.__version__) |
| 560 | + return pkg_version.parse(package_version) |
551 | 561 |
|
552 | 562 |
|
553 | 563 | def is_azure_build() -> bool: |
|
0 commit comments