From cc571e44b12a15bd1e446d36468338e8e6fb168a Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 25 Mar 2021 15:03:24 +0000 Subject: [PATCH] Re-factor git short hash into a config.py function. This should clear the way for using it in running code when not frozen. --- config.py | 30 ++++++++++++++++++++++++++++++ setup.py | 28 +++++++++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/config.py b/config.py index 3c1e9dbb..fa2e3815 100644 --- a/config.py +++ b/config.py @@ -13,6 +13,8 @@ import functools import logging import os import pathlib +import re +import subprocess import sys import traceback import warnings @@ -89,6 +91,34 @@ elif platform == 'linux': _T = TypeVar('_T') +########################################################################### +def git_shorthash_from_head() -> str: + """ + Determine short hash for current git HEAD. + + :return: str - None if we couldn't determine the short hash. + """ + shorthash: str = None # type: ignore + try: + git_cmd = subprocess.Popen('git rev-parse --short HEAD'.split(), + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + out, err = git_cmd.communicate() + + except Exception as e: + logger.error(f"Couldn't run git command for short hash: {e!r}") + + else: + shorthash = out.decode().rstrip('\n') + if re.match(r'^[0-9a-f]{7,}$', shorthash) is None: + logger.error(f"'{shorthash}' doesn't look like a valid git short hash, forcing to None") + shorthash = None # type: ignore + + return shorthash +########################################################################### + + class AbstractConfig(abc.ABC): """Abstract root class of all platform specific Config implementations.""" diff --git a/setup.py b/setup.py index eb1e16b2..c5066624 100755 --- a/setup.py +++ b/setup.py @@ -12,7 +12,6 @@ import os import platform import re import shutil -import subprocess import sys from distutils.core import setup from os.path import exists, isdir, join @@ -21,34 +20,25 @@ from typing import Any, Generator, Set import semantic_version -from config import appcmdname, applongname, appname, appversion, copyright, update_feed, update_interval +from config import ( + appcmdname, applongname, appname, appversion, copyright, git_shorthash_from_head, update_feed, update_interval +) from constants import GITVERSION_FILE if sys.version_info[0:2] != (3, 9): raise AssertionError(f'Unexpected python version {sys.version}') +########################################################################### # Retrieve current git short hash and store in file GITVERSION_FILE -try: - git_cmd = subprocess.Popen('git rev-parse --short HEAD'.split(), - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT - ) - out, err = git_cmd.communicate() - -except Exception as e: - print(f"Couldn't run git command for short hash: {e!r}") +git_shorthash = git_shorthash_from_head() +if git_shorthash is None: exit(-1) -else: - git_shorthash: str = out.decode().rstrip('\n') - if re.match(r'^[0-9a-f]{7,}$', git_shorthash) is None: - print(f"'{git_shorthash}' doesn't look like a valid git short hash!") - exit(-2) - - with open(GITVERSION_FILE, 'w+', encoding='utf-8') as gvf: - gvf.write(git_shorthash) +with open(GITVERSION_FILE, 'w+', encoding='utf-8') as gvf: + gvf.write(git_shorthash) print(f'Git short hash: {git_shorthash}') +########################################################################### if sys.platform == 'win32': assert platform.architecture()[0] == '32bit', 'Assumes a Python built for 32bit'