From a33792f07e4d3f1f3c79c16d893b9193ef57e09d Mon Sep 17 00:00:00 2001 From: lf Date: Sat, 27 Oct 2018 08:44:38 -0600 Subject: util: Fix versioning when gitdir is absent (#809) The gitdir previously could be absent and produce a version of "" in spite of checks for it. Fixed. Parent directories with shlex-interpreted characters in their names could be misinterpreted. Removed shlex parsing. Packagers may want to remove the git history to slim down the package size, so add an option for using a file 'version' in the klippy directory to set version without using git. Signed-Off-By: Lucas Fink --- klippy/util.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'klippy/util.py') diff --git a/klippy/util.py b/klippy/util.py index 86f42246..dfb505a8 100644 --- a/klippy/util.py +++ b/klippy/util.py @@ -53,18 +53,31 @@ def get_cpu_info(): model_name = dict(lines).get("model name", "?") return "%d core %s" % (core_count, model_name) -def get_git_version(): +def get_version_from_file(klippy_src): + try: + with open(os.path.join(klippy_src, '.version')) as h: + return h.read().rstrip() + except IOError: + pass + return "?" + +def get_git_version(from_file=True): + klippy_src = os.path.dirname(__file__) + # Obtain version info from "git" program - gitdir = os.path.join(sys.path[0], '..') - if not os.path.exists(gitdir): - logging.debug("No '.git' file/directory found") - return "?" - prog = "git -C %s describe --always --tags --long --dirty" % (gitdir,) + gitdir = os.path.join(klippy_src, '..') + prog = ('git', '-C', gitdir, 'describe', '--always', '--tags', '--long', '--dirty') try: - process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE) - output = process.communicate()[0] - retcode = process.poll() + process = subprocess.Popen(prog, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + ver, err = process.communicate() + retcode = process.wait() + if retcode == 0: + return ver.strip() + else: + logging.debug("Error getting git version: %s", err) except OSError: logging.debug("Exception on run: %s", traceback.format_exc()) - return "?" - return output.strip() + + if from_file: + return get_version_from_file(klippy_src) + return "?" -- cgit v1.2.3-70-g09d2