Skip to content

Commit

Permalink
Second fix for RTD issue: Generate version file in RTD case
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <maiera@de.ibm.com>
  • Loading branch information
andy-maier committed Jun 23, 2024
1 parent cf48331 commit cd2c9a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
1 change: 1 addition & 0 deletions changes/1559.notshown.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Second fix for RTD issue: Generate version file in RTD case.
88 changes: 48 additions & 40 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,56 @@ def get_version():
version_file = os.path.join('..', 'zhmcclient', '_version_scm.py')
# pylint: disable=unspecified-encoding

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
if not os.path.exists(version_file):

print(f"conf.py: Creating version file {version_file}")

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:
if tag_part.endswith('a0'):
tag_parts2.append(int(tag_part[:-2]))
tag_parts2.append('a0')
else:
tag_parts2.append(int(tag_part))
tags2.append(tag_parts2)
tags2 = sorted(tags2)

# Take the highest version and convert back to string
_version_tuple = tuple(tags2[-1])
_version = '.'.join(map(str, _version_tuple))

with open(version_file, 'w') as fp:
fp.write(
f"""# This file has been generated by docs/conf.py using the highest version tag
# that is reachable from the checked-out branch. This is not the exact version
# generated by setuptools-scm.
__version__ = version = {_version!r}
__version_tuple__ = version_tuple = {_version_tuple!r}
""")

# 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

# Get version from existing version file
print(f"conf.py: Getting version from existing version file {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


Expand Down

0 comments on commit cd2c9a4

Please sign in to comment.