version.py: sync with other projects

Change-Id: I0e8b1502a9d1e8efa3ee1e0798ec5a85bf9d0f35
This commit is contained in:
Georg Brandl 2023-02-28 08:14:25 +01:00 committed by Markus Zolliker
parent 0ce5a495f0
commit 09f6149822

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# ***************************************************************************** # *****************************************************************************
# Copyright (c) 2015-2019 by the authors, see LICENSE # Copyright (c) 2015-2023 by the authors, see LICENSE
# #
# This program is free software; you can redistribute it and/or modify it under # This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software # the terms of the GNU General Public License as published by the Free Software
@ -19,12 +18,9 @@
# Module authors: # Module authors:
# Douglas Creager <dcreager@dcreager.net> # Douglas Creager <dcreager@dcreager.net>
# This file is placed into the public domain. # This file is placed into the public domain.
# fixes for PEP440 by:
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
# #
# ***************************************************************************** # *****************************************************************************
import os.path import os.path
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
@ -35,20 +31,18 @@ RELEASE_VERSION_FILE = os.path.join(os.path.dirname(__file__),
GIT_REPO = os.path.join(os.path.dirname(__file__), '..', '.git') GIT_REPO = os.path.join(os.path.dirname(__file__), '..', '.git')
def get_git_version(abbrev=4, cwd=None): def translate_version(ver):
ver = ver.lstrip('v').rsplit('-', 2)
return f'{ver[0]}.post{ver[1]}+{ver[2]}' if len(ver) == 3 else ver[0]
def get_git_version(abbrev=4):
try: try:
print("REPO:", GIT_REPO) with Popen(['git', f'--git-dir={GIT_REPO}',
with Popen(['git', '--git-dir=%s' % GIT_REPO, 'describe', f'--abbrev={abbrev}'],
'describe', '--abbrev=%d' % abbrev],
stdout=PIPE, stderr=PIPE) as p: stdout=PIPE, stderr=PIPE) as p:
stdout, _stderr = p.communicate() stdout, _stderr = p.communicate()
version = stdout.strip().decode('utf-8', 'ignore') return translate_version(stdout.strip().decode('utf-8', 'ignore'))
print("git:", version)
# mangle version to comply with pep440
if version.count('-'):
version, patchcount, githash = version.split('-')
version += '.post%s+%s' % (patchcount, githash)
return version
except Exception: except Exception:
return None return None
@ -63,7 +57,7 @@ def read_release_version():
def write_release_version(version): def write_release_version(version):
with open(RELEASE_VERSION_FILE, 'w', encoding='utf-8') as f: with open(RELEASE_VERSION_FILE, 'w', encoding='utf-8') as f:
f.write("%s\n" % version) f.write(f'{version}\n')
def get_version(abbrev=4): def get_version(abbrev=4):
@ -76,8 +70,9 @@ def get_version(abbrev=4):
if git_version != release_version: if git_version != release_version:
write_release_version(git_version) write_release_version(git_version)
return git_version return git_version
if release_version: elif release_version:
return release_version return release_version
else:
raise ValueError('Cannot find a version number - make sure that ' raise ValueError('Cannot find a version number - make sure that '
'git is installed or a RELEASE-VERSION file is ' 'git is installed or a RELEASE-VERSION file is '
'present!') 'present!')