Skip to content

Commit

Permalink
Fixed the RTD build issue due to version file not available
Browse files Browse the repository at this point in the history
Details:

* Fixed the RTD build by using the version from the highest tag that is
  reachable from the checked out branch, if the version file _version_scm.py
  is not available.

Signed-off-by: Andreas Maier <maiera@de.ibm.com>
  • Loading branch information
andy-maier committed Jun 23, 2024
1 parent d81c341 commit cf48331
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
3 changes: 3 additions & 0 deletions changes/1559.notshown.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed the RTD build by using the version from the highest tag that is
reachable from the checked out branch, if the version file _version_scm.py
is not available.
64 changes: 55 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,70 @@

import sys
import os
import subprocess


def get_version():
"""
Return the package version as a string.
The version file must have been generated by setuptools-scm.
This script is used both for local docs builds and by ReadTheDocs builds.
For local builds, the Makefile ensures that there is a _version_scm.py
file with the exact version. The version file must have been generated by
setuptools-scm.
For ReadTheDocs builds, the Makefile is not used and because the
_version_scm.py file is not in the repo (since it would need to be updated
in each PR) it is not available. In that case, the version is determined
from the highest tag reachable from the checked out branch (ie. shown by
'git tag -l --merged').
"""
version_file = os.path.join('..', 'zhmcclient', '_version_scm.py')
# pylint: disable=unspecified-encoding
with open(version_file) as fp:
version_source = fp.read()
_globals = {}
exec(version_source, _globals) # pylint: disable=exec-used
try:
_version = _globals['__version__']
except KeyError:
_version = _globals['version']

if os.path.exists(version_file):

# Get version from version file
with open(version_file) as fp:
version_source = fp.read()
_globals = {}
exec(version_source, _globals) # pylint: disable=exec-used
try:
_version = _globals['__version__']
except KeyError:
_version = _globals['version']
return _version

# Get version from highest tag

cmd = 'git tag -l --merged'
cp = subprocess.run(
cmd, shell=True, check=True, capture_output=True, timeout=30)
tags = cp.stdout.decode('utf-8').split('\n')

# Convert the version strings into lists of integers for sorting
tags2 = [] # list of tags represented as list of int
for tag in tags:
if tag != '': # output ends with an empty line -> empty string
tag_parts = tag.split('.')
tag_parts2 = []
for tag_part in tag_parts:
# Handle start tags (e.g. '1.17.0a0')
if tag_part.endswith('a0'):
tag_part = tag_part[:-2]
try:
tag_part2 = int(tag_part)
except ValueError:
# Should not happen, but in case it does we use the string
tag_part2 = tag_part
tag_parts2.append(tag_part2)
tags2.append(tag_parts2)

# Sort the integer-based versions, take the highest and convert to string
highest_tag2 = sorted(tags2)[-1]
_version = '.'.join(map(str, highest_tag2))

return _version


Expand Down

0 comments on commit cf48331

Please sign in to comment.