diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2022-09-19 21:32:06 +0300 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2022-09-19 21:34:06 +0300 |
commit | a81e55e721372856bed5b40f0a22ec68ad45a644 (patch) | |
tree | 8809507116b5d5088d3b6477acc02ce664164f42 /src | |
parent | dist.distutils_extensions: fix _verinfo.py file (diff) | |
download | snakeoil-a81e55e721372856bed5b40f0a22ec68ad45a644.tar.gz snakeoil-a81e55e721372856bed5b40f0a22ec68ad45a644.tar.bz2 snakeoil-a81e55e721372856bed5b40f0a22ec68ad45a644.zip |
Revert "dist.distutils_extensions: fix _verinfo.py file"
Revert: b4658d2ebfe9d929ba9cc93e84a93c0ac2d399c7
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/snakeoil/dist/distutils_extensions.py | 25 | ||||
-rw-r--r-- | src/snakeoil/version.py | 72 |
2 files changed, 45 insertions, 52 deletions
diff --git a/src/snakeoil/dist/distutils_extensions.py b/src/snakeoil/dist/distutils_extensions.py index 03888e3..f919826 100644 --- a/src/snakeoil/dist/distutils_extensions.py +++ b/src/snakeoil/dist/distutils_extensions.py @@ -18,6 +18,7 @@ import subprocess import sys import textwrap from contextlib import ExitStack, contextmanager, redirect_stderr, redirect_stdout +from datetime import datetime from multiprocessing import cpu_count from setuptools import find_packages @@ -125,8 +126,8 @@ def module_version(moduledir=MODULEDIR): version = re.search( r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE).group(1) - except IOError as exc: - if exc.errno == errno.ENOENT: + except IOError as e: + if e.errno == errno.ENOENT: pass else: raise @@ -136,12 +137,16 @@ def module_version(moduledir=MODULEDIR): # use versioning scheme similar to setuptools_scm for untagged versions git_version = get_git_version(REPODIR) - if git_version is not None: - tag = git_version.tag + if git_version: + tag = git_version['tag'] if tag is None: - if (commits := git_version.commits) is not None: + commits = git_version['commits'] + rev = git_version['rev'][:7] + date = datetime.strptime(git_version['date'], '%a, %d %b %Y %H:%M:%S %z') + date = datetime.strftime(date, '%Y%m%d') + if commits is not None: version += f'.dev{commits}' - version += f'+g{git_version.short_revision}.d{git_version.date:%Y%m%d}' + version += f'+g{rev}.d{date}' elif tag != version: raise DistutilsError( f'unmatched git tag {tag!r} and {MODULE_NAME} version {version!r}') @@ -159,9 +164,7 @@ def generate_verinfo(target_dir): path = os.path.join(target_dir, '_verinfo.py') log.info(f'generating version info: {path}') with open(path, 'w') as f: - f.write('from snakeoil.version import GitVersion\n') - f.write('import datetime\n') - f.write(f'version_info={data!r}') + f.write('version_info=%r' % (data,)) return path @@ -171,8 +174,8 @@ def readme(topdir=REPODIR): try: with open(os.path.join(topdir, doc), encoding='utf-8') as f: return f.read() - except IOError as exc: - if exc.errno == errno.ENOENT: + except IOError as e: + if e.errno == errno.ENOENT: pass else: raise diff --git a/src/snakeoil/version.py b/src/snakeoil/version.py index 0e88dbf..fa3fc96 100644 --- a/src/snakeoil/version.py +++ b/src/snakeoil/version.py @@ -3,14 +3,12 @@ import errno import os import subprocess -from datetime import datetime from importlib import import_module -from typing import NamedTuple, Optional _ver = None -def get_version(project, repo_file, api_version=None) -> str: +def get_version(project, repo_file, api_version=None): """Determine a project's version information. Standardized version retrieval for git-based projects. In summary, if the @@ -42,73 +40,65 @@ def get_version(project, repo_file, api_version=None) -> str: version_info = get_git_version(path) if version_info is None: - suffix = '' - elif version_info.tag == api_version: - suffix = f' -- released {version_info.date_rfc2822}' + s = '' + elif version_info['tag'] == api_version: + s = f" -- released {version_info['date']}" else: - rev = version_info.short_revision - date = version_info.date_rfc2822 - commits = f'-{version_info.commits}' if version_info.commits is not None else '' - suffix = f'{commits}-g{rev} -- {date}' + rev = version_info['rev'][:7] + date = version_info['date'] + commits = version_info.get('commits', None) + commits = f'-{commits}' if commits is not None else '' + s = f'{commits}-g{rev} -- {date}' - _ver = f'{project} {api_version}{suffix}' + _ver = f'{project} {api_version}{s}' return _ver -def _run_git(path: str, *cmd: str): +def _run_git(path, cmd): env = dict(os.environ) for key in env.copy(): # pragma: no cover if key.startswith("LC_"): del env[key] env["LC_CTYPE"] = "C" env["LC_ALL"] = "C" - r = subprocess.Popen(('git', ) + cmd, env=env, cwd=path, - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) - stdout, _ = r.communicate() - return stdout, r.returncode - - -class GitVersion(NamedTuple): - revision: str - date: datetime - tag: Optional[str] = None - commits: Optional[int] = None + r = subprocess.Popen( + ['git'] + list(cmd), stdout=subprocess.PIPE, env=env, + stderr=subprocess.DEVNULL, cwd=path) - @property - def date_rfc2822(self): - return self.date.strftime('%a, %d %b %Y %H:%M:%S %z') - - @property - def short_revision(self): - return self.revision[:7] + stdout = r.communicate()[0] + return stdout, r.returncode -def get_git_version(path: str) -> Optional[GitVersion]: +def get_git_version(path): """Return git related revision data.""" path = os.path.abspath(path) try: - stdout, ret = _run_git(path, "log", "--format=%H\n%aI", "HEAD^..HEAD") + stdout, ret = _run_git(path, ["log", "--format=%H\n%aD", "HEAD^..HEAD"]) + if ret != 0: return None - revision, date = stdout.decode().splitlines() - tag = _get_git_tag(path, revision) + data = stdout.decode().splitlines() + tag = _get_git_tag(path, data[0]) # get number of commits since most recent tag - stdout, ret = _run_git(path, 'describe', '--tags', '--abbrev=0') + stdout, ret = _run_git(path, ['describe', '--tags', '--abbrev=0']) + prev_tag = None commits = None if ret == 0: prev_tag = stdout.decode().strip() stdout, ret = _run_git( - path, 'log', '--oneline', f'{prev_tag}..HEAD') + path, ['log', '--oneline', f'{prev_tag}..HEAD']) if ret == 0: commits = len(stdout.decode().splitlines()) - return GitVersion( - revision=revision, date=datetime.fromisoformat(date), - tag=tag, commits=commits, - ) + return { + 'rev': data[0], + 'date': data[1], + 'tag': tag, + 'commits': commits, + } except EnvironmentError as exc: # ENOENT is thrown when the git binary can't be found. if exc.errno != errno.ENOENT: @@ -117,7 +107,7 @@ def get_git_version(path: str) -> Optional[GitVersion]: def _get_git_tag(path, rev): - stdout, _ = _run_git(path, 'name-rev', '--tag', rev) + stdout, _ = _run_git(path, ['name-rev', '--tag', rev]) tag = stdout.decode().split() if len(tag) != 2: return None |