Skip to content

Commit

Permalink
util: Fix versioning when gitdir is absent (Klipper3d#809)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lf- authored and KevinOConnor committed Oct 27, 2018
1 parent f57c294 commit a33792f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ out
*.pyc
.config
.config.old
klippy/.version
31 changes: 31 additions & 0 deletions docs/Packaging.md
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 24 additions & 11 deletions klippy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "?"
31 changes: 31 additions & 0 deletions scripts/make_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python2
# Get the version number for klippy
#
# Copyright (C) 2018 Lucas Fink <[email protected]>
#
# 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:])

0 comments on commit a33792f

Please sign in to comment.