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>
This commit is contained in:
lf 2018-10-27 08:44:38 -06:00 committed by KevinOConnor
parent f57c29442c
commit a33792f07e
4 changed files with 88 additions and 12 deletions

View file

@ -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():
# 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,)
def get_version_from_file(klippy_src):
try:
process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE)
output = process.communicate()[0]
retcode = process.poll()
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(klippy_src, '..')
prog = ('git', '-C', gitdir, 'describe', '--always', '--tags', '--long', '--dirty')
try:
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 "?"