move git head functionality to function in util.py

This commit is contained in:
Mike Fährmann
2022-11-04 17:35:47 +01:00
parent 4fd3c893fa
commit 597b63d922
2 changed files with 20 additions and 13 deletions

View File

@@ -118,25 +118,15 @@ def main():
config.set(("output",), "mode", "null") config.set(("output",), "mode", "null")
elif args.loglevel <= logging.DEBUG: elif args.loglevel <= logging.DEBUG:
import platform import platform
import subprocess
import os.path
import requests import requests
extra = "" extra = ""
if getattr(sys, "frozen", False): if getattr(sys, "frozen", False):
extra = " - Executable" extra = " - Executable"
else: else:
try: git_head = util.git_head()
out, err = subprocess.Popen( if git_head:
("git", "rev-parse", "--short", "HEAD"), extra = " - Git HEAD: " + git_head
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(os.path.abspath(__file__)),
).communicate()
if out and not err:
extra = " - Git HEAD: " + out.decode().rstrip()
except (OSError, subprocess.SubprocessError):
pass
log.debug("Version %s%s", __version__, extra) log.debug("Version %s%s", __version__, extra)
log.debug("Python %s - %s", log.debug("Python %s - %s",

View File

@@ -19,6 +19,7 @@ import binascii
import datetime import datetime
import functools import functools
import itertools import itertools
import subprocess
import urllib.parse import urllib.parse
from http.cookiejar import Cookie from http.cookiejar import Cookie
from email.utils import mktime_tz, parsedate_tz from email.utils import mktime_tz, parsedate_tz
@@ -273,6 +274,22 @@ Response Headers
fp.write(response.content) fp.write(response.content)
@functools.lru_cache(maxsize=None)
def git_head():
try:
out, err = subprocess.Popen(
("git", "rev-parse", "--short", "HEAD"),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(os.path.abspath(__file__)),
).communicate()
if out and not err:
return out.decode().rstrip()
except (OSError, subprocess.SubprocessError):
pass
return None
def expand_path(path): def expand_path(path):
"""Expand environment variables and tildes (~)""" """Expand environment variables and tildes (~)"""
if not path: if not path: