From 597b63d922294912bfa22dbae11d133d839dffb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 4 Nov 2022 17:35:47 +0100 Subject: [PATCH] move git head functionality to function in util.py --- gallery_dl/__init__.py | 16 +++------------- gallery_dl/util.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index ea750d0e..79ecbccc 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -118,25 +118,15 @@ def main(): config.set(("output",), "mode", "null") elif args.loglevel <= logging.DEBUG: import platform - import subprocess - import os.path import requests extra = "" if getattr(sys, "frozen", False): extra = " - Executable" else: - 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: - extra = " - Git HEAD: " + out.decode().rstrip() - except (OSError, subprocess.SubprocessError): - pass + git_head = util.git_head() + if git_head: + extra = " - Git HEAD: " + git_head log.debug("Version %s%s", __version__, extra) log.debug("Python %s - %s", diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 98b6d590..18faec46 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -19,6 +19,7 @@ import binascii import datetime import functools import itertools +import subprocess import urllib.parse from http.cookiejar import Cookie from email.utils import mktime_tz, parsedate_tz @@ -273,6 +274,22 @@ Response Headers 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): """Expand environment variables and tildes (~)""" if not path: