aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/util.py
diff options
context:
space:
mode:
authorlf <lf-@users.noreply.github.com>2018-10-27 08:44:38 -0600
committerKevinOConnor <kevin@koconnor.net>2018-10-27 10:44:38 -0400
commita33792f07e4d3f1f3c79c16d893b9193ef57e09d (patch)
tree0771dca6a0209b0429b5879c37f916ce2c77eb25 /klippy/util.py
parentf57c29442cac6b110937af8960c9375192bb645e (diff)
downloadkutter-a33792f07e4d3f1f3c79c16d893b9193ef57e09d.tar.gz
kutter-a33792f07e4d3f1f3c79c16d893b9193ef57e09d.tar.xz
kutter-a33792f07e4d3f1f3c79c16d893b9193ef57e09d.zip
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 <software@lfcode.ca>
Diffstat (limited to 'klippy/util.py')
-rw-r--r--klippy/util.py35
1 files changed, 24 insertions, 11 deletions
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 "?"