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

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ out
*.pyc
.config
.config.old
klippy/.version

31
docs/Packaging.md Normal file
View File

@ -0,0 +1,31 @@
# Packaging klipper
Klipper is somewhat of a packaging anomaly among python programs, as it doesn't
use setuptools to build and install. Some notes regarding how best to package it
are as follows:
## C modules
Klipper uses a C module to handle some kinematics calculations more quickly.
This module needs to be compiled at packaging time to avoid introducing a
runtime dependency on a compiler. To compile the C module, run `python2
klippy/chelper/__init__.py`.
## Compiling python code
Many distributions have a policy of compiling all python code before packaging
to improve startup time. You can do this by running `python2 -m compileall
klippy`.
## Versioning
If you are building a package of Klipper from git, it is usual practice not to
ship a .git directory, so the versioning must be handled without git. To do
this, use the script shipped in `scripts/make_version.py` which should be run as
follows: `python2 scripts/make_version.py YOURDISTRONAME > klippy/.version`.
## Sample packaging script
klipper-git is packaged for Arch Linux, and has a PKGBUILD (package build
script) available at
https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=klipper-git.

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 "?"

31
scripts/make_version.py Normal file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python2
# Get the version number for klippy
#
# Copyright (C) 2018 Lucas Fink <software@lfcode.ca>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
from __future__ import print_function
import argparse
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../klippy'))
import util
def main(argv):
p = argparse.ArgumentParser()
p.add_argument(
'distroname',
help='Name of distro this package is intended for'
)
args = p.parse_args()
print(util.get_git_version(from_file=False),
args.distroname.replace(' ', ''), sep='-')
if __name__ == '__main__':
main(sys.argv[1:])