Skip to content

Commit a33792f

Browse files
lf-KevinOConnor
authored andcommitted
util: Fix versioning when gitdir is absent (Klipper3d#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 <[email protected]>
1 parent f57c294 commit a33792f

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ out
33
*.pyc
44
.config
55
.config.old
6+
klippy/.version

docs/Packaging.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Packaging klipper
2+
3+
Klipper is somewhat of a packaging anomaly among python programs, as it doesn't
4+
use setuptools to build and install. Some notes regarding how best to package it
5+
are as follows:
6+
7+
## C modules
8+
9+
Klipper uses a C module to handle some kinematics calculations more quickly.
10+
This module needs to be compiled at packaging time to avoid introducing a
11+
runtime dependency on a compiler. To compile the C module, run `python2
12+
klippy/chelper/__init__.py`.
13+
14+
## Compiling python code
15+
16+
Many distributions have a policy of compiling all python code before packaging
17+
to improve startup time. You can do this by running `python2 -m compileall
18+
klippy`.
19+
20+
## Versioning
21+
22+
If you are building a package of Klipper from git, it is usual practice not to
23+
ship a .git directory, so the versioning must be handled without git. To do
24+
this, use the script shipped in `scripts/make_version.py` which should be run as
25+
follows: `python2 scripts/make_version.py YOURDISTRONAME > klippy/.version`.
26+
27+
## Sample packaging script
28+
29+
klipper-git is packaged for Arch Linux, and has a PKGBUILD (package build
30+
script) available at
31+
https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=klipper-git.

klippy/util.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,31 @@ def get_cpu_info():
5353
model_name = dict(lines).get("model name", "?")
5454
return "%d core %s" % (core_count, model_name)
5555

56-
def get_git_version():
56+
def get_version_from_file(klippy_src):
57+
try:
58+
with open(os.path.join(klippy_src, '.version')) as h:
59+
return h.read().rstrip()
60+
except IOError:
61+
pass
62+
return "?"
63+
64+
def get_git_version(from_file=True):
65+
klippy_src = os.path.dirname(__file__)
66+
5767
# Obtain version info from "git" program
58-
gitdir = os.path.join(sys.path[0], '..')
59-
if not os.path.exists(gitdir):
60-
logging.debug("No '.git' file/directory found")
61-
return "?"
62-
prog = "git -C %s describe --always --tags --long --dirty" % (gitdir,)
68+
gitdir = os.path.join(klippy_src, '..')
69+
prog = ('git', '-C', gitdir, 'describe', '--always', '--tags', '--long', '--dirty')
6370
try:
64-
process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE)
65-
output = process.communicate()[0]
66-
retcode = process.poll()
71+
process = subprocess.Popen(prog, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
72+
ver, err = process.communicate()
73+
retcode = process.wait()
74+
if retcode == 0:
75+
return ver.strip()
76+
else:
77+
logging.debug("Error getting git version: %s", err)
6778
except OSError:
6879
logging.debug("Exception on run: %s", traceback.format_exc())
69-
return "?"
70-
return output.strip()
80+
81+
if from_file:
82+
return get_version_from_file(klippy_src)
83+
return "?"

scripts/make_version.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python2
2+
# Get the version number for klippy
3+
#
4+
# Copyright (C) 2018 Lucas Fink <[email protected]>
5+
#
6+
# This file may be distributed under the terms of the GNU GPLv3 license.
7+
8+
from __future__ import print_function
9+
10+
import argparse
11+
import os
12+
import sys
13+
14+
sys.path.append(os.path.join(os.path.dirname(__file__), '../klippy'))
15+
16+
import util
17+
18+
19+
def main(argv):
20+
p = argparse.ArgumentParser()
21+
p.add_argument(
22+
'distroname',
23+
help='Name of distro this package is intended for'
24+
)
25+
args = p.parse_args()
26+
print(util.get_git_version(from_file=False),
27+
args.distroname.replace(' ', ''), sep='-')
28+
29+
30+
if __name__ == '__main__':
31+
main(sys.argv[1:])

0 commit comments

Comments
 (0)