| 1 |
import os |
| 2 |
import sys |
| 3 |
from pathlib import Path |
| 4 |
from typing import Optional |
| 5 |
from typing import Text |
| 6 |
|
| 7 |
import toml |
| 8 |
|
| 9 |
version_file_path = Path("questionary/version.py") |
| 10 |
|
| 11 |
pyproject_file_path = Path("pyproject.toml") |
| 12 |
|
| 13 |
|
| 14 |
def get_pyproject_version(): |
| 15 |
"""Return the project version specified in the poetry build configuration.""" |
| 16 |
data = toml.load(pyproject_file_path) |
| 17 |
return data["tool"]["poetry"]["version"] |
| 18 |
|
| 19 |
|
| 20 |
def get_current_version() -> Text: |
| 21 |
"""Return the current library version as specified in the code.""" |
| 22 |
|
| 23 |
if not version_file_path.is_file(): |
| 24 |
raise FileNotFoundError( |
| 25 |
f"Failed to find version file at {version_file_path().absolute()}" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
_globals = {"__version__": ""} |
| 32 |
with open(version_file_path) as f: |
| 33 |
exec(f.read(), _globals) |
| 34 |
|
| 35 |
return _globals["__version__"] |
| 36 |
|
| 37 |
|
| 38 |
def get_tagged_version() -> Optional[Text]: |
| 39 |
"""Return the version specified in a tagged git commit.""" |
| 40 |
return os.environ.get("TRAVIS_TAG") |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == "__main__": |
| 44 |
if get_pyproject_version() != get_current_version(): |
| 45 |
print( |
| 46 |
f"Version in {pyproject_file_path} does not correspond " |
| 47 |
f"to the version in {version_file_path}! The version needs to be " |
| 48 |
f"set to the same value in both places." |
| 49 |
) |
| 50 |
sys.exit(1) |
| 51 |
elif get_tagged_version() and get_tagged_version() != get_current_version(): |
| 52 |
print( |
| 53 |
f"Tagged version does not correspond to the version " |
| 54 |
f"in {version_file_path}!" |
| 55 |
) |
| 56 |
sys.exit(1) |
| 57 |
elif get_tagged_version() and get_tagged_version() != get_pyproject_version(): |
| 58 |
print( |
| 59 |
f"Tagged version does not correspond to the version " |
| 60 |
f"in {pyproject_file_path}!" |
| 61 |
) |
| 62 |
sys.exit(1) |
| 63 |
else: |
| 64 |
print("Versions look good!") |