|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""Extract version number from __init__.py""" |
3 | 2 |
|
4 | | -import os |
| 3 | +"""Determine and print version number. |
5 | 4 |
|
| 5 | +Used in top level ``meson.build``. |
| 6 | +""" |
6 | 7 |
|
7 | | -def git_version(version): |
8 | | - """Append last commit date and hash to version, if available""" |
9 | | - import subprocess |
10 | | - import os.path |
| 8 | +import subprocess |
| 9 | +from pathlib import Path |
11 | 10 |
|
12 | | - git_hash = '' |
| 11 | + |
| 12 | +def version_from_init(): |
| 13 | + """Extract version string from ``skimage/__init__.py``.""" |
| 14 | + skimage_init = Path(__file__).parent / '../__init__.py' |
| 15 | + assert skimage_init.is_file() |
| 16 | + |
| 17 | + with skimage_init.open("r") as file: |
| 18 | + data = file.readlines() |
| 19 | + |
| 20 | + version_line = next(line for line in data if line.startswith('__version__ =')) |
| 21 | + version = version_line.strip().split(' = ')[1].replace('"', '').replace("'", '') |
| 22 | + return version |
| 23 | + |
| 24 | + |
| 25 | +def append_git_revision_and_date(version): |
| 26 | + """Try to append last commit date and hash to version. |
| 27 | +
|
| 28 | + Appends nothing if the current working directory is outside a git |
| 29 | + repository. |
| 30 | + """ |
13 | 31 | try: |
14 | | - p = subprocess.Popen( |
| 32 | + result = subprocess.run( |
15 | 33 | ['git', 'log', '-1', '--format="%H %aI"'], |
16 | | - stdout=subprocess.PIPE, |
17 | | - stderr=subprocess.PIPE, |
18 | | - cwd=os.path.dirname(__file__), |
| 34 | + capture_output=True, |
| 35 | + text=True, |
| 36 | + check=True, |
19 | 37 | ) |
20 | | - except FileNotFoundError: |
| 38 | + except subprocess.CalledProcessError: |
21 | 39 | pass |
22 | 40 | else: |
23 | | - out, err = p.communicate() |
24 | | - if p.returncode == 0: |
25 | | - git_hash, git_date = ( |
26 | | - out.decode('utf-8') |
27 | | - .strip() |
28 | | - .replace('"', '') |
29 | | - .split('T')[0] |
30 | | - .replace('-', '') |
31 | | - .split() |
32 | | - ) |
33 | | - |
34 | | - version += f'+git{git_date}.{git_hash[:7]}' |
35 | | - |
| 41 | + git_hash, git_date = ( |
| 42 | + result.stdout.strip() |
| 43 | + .replace('"', '') |
| 44 | + .split('T')[0] |
| 45 | + .replace('-', '') |
| 46 | + .split() |
| 47 | + ) |
| 48 | + version += f'+git{git_date}.{git_hash[:7]}' |
36 | 49 | return version |
37 | 50 |
|
38 | 51 |
|
39 | | -ski_init = os.path.join(os.path.dirname(__file__), '../__init__.py') |
40 | | - |
41 | | -data = open(ski_init).readlines() |
42 | | -version_line = next(line for line in data if line.startswith('__version__ =')) |
43 | | - |
44 | | -version = version_line.strip().split(' = ')[1].replace('"', '').replace("'", '') |
45 | | - |
46 | | -if 'dev' in version: |
47 | | - version = git_version(version) |
48 | | - |
49 | | -print(version) |
| 52 | +if __name__ == "__main__": |
| 53 | + version = version_from_init() |
| 54 | + if 'dev' in version: |
| 55 | + version = append_git_revision_and_date(version) |
| 56 | + print(version) |
0 commit comments