diff --git a/.gitignore b/.gitignore index a1e314a8..d5414bd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.gitversion .DS_Store build dist.* diff --git a/constants.py b/constants.py index 814471e4..9e757be7 100644 --- a/constants.py +++ b/constants.py @@ -9,6 +9,7 @@ found necessary to move out of other files. # config.py appname = 'EDMarketConnector' applongname = 'E:D Market Connector' +GITVERSION_FILE = '.gitversion' # protocol.py protocolhandler_redirect = 'edmc://auth' diff --git a/setup.py b/setup.py index 703f2d96..eb1e16b2 100755 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ 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,10 +22,34 @@ from typing import Any, Generator, Set import semantic_version from config import appcmdname, applongname, appname, appversion, copyright, 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}") + 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) + +print(f'Git short hash: {git_shorthash}') + if sys.platform == 'win32': assert platform.architecture()[0] == '32bit', 'Assumes a Python built for 32bit' import py2exe # noqa: F401 # Yes, this *is* used