From 36b6ea2da536aaf8fc7a12b515508de844b1a4bf Mon Sep 17 00:00:00 2001 From: Taka Date: Thu, 27 Dec 2018 14:58:29 +0000 Subject: [PATCH] Show the git branch and last commit ID in the desktop app About box. --- _freeze.py | 29 +++++++++++++++++++++++++++++ vasl_templates/about.py | 11 ++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/_freeze.py b/_freeze.py index c9fb0cb..00f4c45 100755 --- a/_freeze.py +++ b/_freeze.py @@ -4,10 +4,12 @@ import sys import os import shutil +import subprocess import tempfile import time import datetime import json +import re import getopt from PyInstaller.__main__ import run as run_pyinstaller @@ -24,6 +26,32 @@ DEFAULT_TARGET_NAME = "vasl-templates" # --------------------------------------------------------------------- +def get_git_info(): + """Get the git branch/commit we're building from.""" + + # get the latest commit ID + proc = subprocess.run( + [ "git", "log" ], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8" + ) + buf = proc.stdout.split( "\n" )[0] + mo = re.search( r"^commit ([a-z0-9]+)$", buf ) + last_commit_id = mo.group(1) + + # get the current git branch + proc = subprocess.run( + [ "git", "branch" ], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8" + ) + lines = [ s for s in proc.stdout.split("\n") if s.startswith("* ") ] + if len(lines) != 1: + raise RuntimeError( "Can't parse git branch status." ) + branch_name = lines[0][2:] + + return { "last_commit_id": last_commit_id, "branch_name": branch_name } + +# --------------------------------------------------------------------- + # parse the command-line options output_fname = None work_dir = None @@ -131,6 +159,7 @@ for f in fnames: build_info = { "timestamp": int( time.time() ), } +build_info.update( get_git_info() ) dname = os.path.join( dist_dir, "config" ) with open( os.path.join(dname,"build-info.json"), "w" ) as fp: json.dump( build_info, fp ) diff --git a/vasl_templates/about.py b/vasl_templates/about.py index aa96937..a4e8de5 100644 --- a/vasl_templates/about.py +++ b/vasl_templates/about.py @@ -3,6 +3,7 @@ import os import json import time +import io from PyQt5 import uic from PyQt5.QtWidgets import QDialog @@ -38,10 +39,18 @@ class AboutDialog( QDialog ): self.app_name.setText( "{} ({})".format( APP_NAME, APP_VERSION ) ) self.license.setText( "Licensed under the GNU Affero General Public License (v3)." ) if build_info: + buf = io.StringIO() timestamp = build_info[ "timestamp" ] - self.build_info.setText( "Built {}.".format( + buf.write( "Built {}".format( time.strftime( "%d %B %Y %H:%S", time.localtime( timestamp ) ) # nb: "-d" doesn't work on Windows :-/ ) ) + if "branch_name" in build_info or "last_commit_id" in build_info: + buf.write( " ({}".format( build_info["branch_name"] ) ) + if "last_commit_id" in build_info: + buf.write( ":{}".format( build_info["last_commit_id"][:8] ) ) + buf.write( ")" ) + buf.write( "." ) + self.build_info.setText( buf.getvalue() ) else: self.build_info.setText( "" ) self.home_url.setText(