Skip to content

Commit fb35cbd

Browse files
committed
Move commandline interface to typer
This commit moves the kiwi commandline and plugin interface to typer and drops the use of docopt. Typer is based on python type hints and allows for a more modern way to define Cli interfaces and auto completion, even for plugins. Also docopt is split into an old and a next generation variant and allows us to get rid of spec file hacks as well as documentation rendering hacks.
1 parent 9c0e047 commit fb35cbd

37 files changed

+1587
-1219
lines changed

.github/workflows/ci-code-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
15+
python-version: ["3.10", "3.11", "3.12", "3.13"]
1616

1717
steps:
1818
- uses: actions/checkout@v3

.github/workflows/ci-units-types.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
15+
python-version: ["3.10", "3.11", "3.12", "3.13"]
1616

1717
steps:
1818
- uses: actions/checkout@v3

Makefile

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ install:
3535
done
3636
# completion
3737
install -d -m 755 ${buildroot}usr/share/bash-completion/completions
38-
$(python) helper/completion_generator.py \
39-
> ${buildroot}usr/share/bash-completion/completions/kiwi-ng
38+
install -m 644 completions/bash-completion \
39+
${buildroot}usr/share/bash-completion/completions/kiwi-ng
4040
# kiwi default configuration
4141
install -d -m 755 ${buildroot}etc
4242
install -m 644 kiwi.yml ${buildroot}etc/kiwi.yml
@@ -80,16 +80,6 @@ valid:
8080
fi \
8181
done
8282

83-
git_attributes:
84-
# the following is required to update the $Format:%H$ git attribute
85-
# for details on when this target is called see setup.py
86-
git archive HEAD kiwi/version.py | tar -x
87-
88-
clean_git_attributes:
89-
# cleanup version.py to origin state
90-
# for details on when this target is called see setup.py
91-
git checkout kiwi/version.py
92-
9383
setup:
9484
poetry install --all-extras
9585

@@ -171,7 +161,7 @@ prepare_for_docs: clean setup
171161
travis-sphinx --outdir build_gh_pages build --nowarn --source ./source'
172162
bash -c 'touch ./doc/build_gh_pages/.nojekyll'
173163

174-
clean: clean_git_attributes
164+
clean:
175165
rm -rf dist
176166
rm -rf doc/build
177167
rm -rf doc/dist
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import typer
2+
3+
from typing import (
4+
Annotated, Optional
5+
)
6+
7+
# Filename must be cli.py and typers variable must be
8+
# provided for kiwi plugins
9+
typers = {
10+
'boxbuild': typer.Typer(
11+
add_completion=False
12+
)
13+
}
14+
15+
system = typers['boxbuild']
16+
17+
@system.command(
18+
context_settings={
19+
'allow_extra_args': True,
20+
'ignore_unknown_options': True
21+
}
22+
)
23+
def kiwi(
24+
ctx: typer.Context,
25+
):
26+
"""
27+
Specify kiwi system build options
28+
"""
29+
Cli=ctx.obj
30+
option = None
31+
for kiwi_arg in ctx.args:
32+
if kiwi_arg.startswith('-'):
33+
Cli.subcommand_args['boxbuild'][kiwi_arg] = True
34+
elif option:
35+
Cli.subcommand_args['boxbuild'][option] = kiwi_arg
36+
option = kiwi_arg
37+
Cli.global_args['command'] = 'boxbuild'
38+
Cli.global_args['system'] = True
39+
Cli.cli_ok = True
40+
41+
@system.callback(
42+
help='build a system image in a self contained virtual machine',
43+
subcommand_metavar='kiwi [OPTIONS]'
44+
)
45+
def boxbuild(
46+
ctx: typer.Context,
47+
some: Annotated[
48+
Optional[str], typer.Option(
49+
help='some'
50+
)
51+
] = 'some'
52+
):
53+
Cli=ctx.obj
54+
Cli.subcommand_args['boxbuild'] = {
55+
'some': some,
56+
'help': False
57+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import typer
2+
3+
from typing import (
4+
Annotated, Optional
5+
)
6+
7+
# Filename must be cli.py and typers variable must be
8+
# provided for kiwi plugins
9+
typers = {
10+
'stackbuild': typer.Typer(
11+
add_completion=False
12+
),
13+
'stash': typer.Typer(
14+
add_completion=False
15+
)
16+
}
17+
18+
system_stackbuild = typers['stackbuild']
19+
system_stash = typers['stash']
20+
21+
@system_stackbuild.callback(
22+
help='Build an image based on a given stash container root. '
23+
'If no KIWI description is provided along with the command, '
24+
'stackbuild rebuilds the image from the stash container. '
25+
'If a KIWI description is provided, this description takes '
26+
'over precedence and a new image from this description based '
27+
'on the given stash container root will be built.',
28+
invoke_without_command=True,
29+
subcommand_metavar=''
30+
)
31+
def stackbuild(
32+
ctx: typer.Context,
33+
some: Annotated[
34+
Optional[str], typer.Option(
35+
help='some'
36+
)
37+
] = 'some'
38+
):
39+
Cli=ctx.obj
40+
Cli.subcommand_args['stackbuild'] = {
41+
'some': some,
42+
'help': False
43+
}
44+
Cli.global_args['command'] = 'stackbuild'
45+
Cli.global_args['system'] = True
46+
Cli.cli_ok = True
47+
48+
@system_stash.callback(
49+
help='Create a container from the given root directory',
50+
invoke_without_command=True,
51+
subcommand_metavar=''
52+
)
53+
def stash(
54+
ctx: typer.Context,
55+
some: Annotated[
56+
Optional[str], typer.Option(
57+
help='some'
58+
)
59+
] = 'some'
60+
):
61+
Cli=ctx.obj
62+
Cli.subcommand_args['stash'] = {
63+
'some': some,
64+
'help': False
65+
}
66+
Cli.global_args['command'] = 'stash'
67+
Cli.global_args['system'] = True
68+
Cli.cli_ok = True

completions/bash-completion

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#========================================
2+
# _kiwi
3+
#----------------------------------------
4+
function setupCompletionLine {
5+
local comp_line=$(echo $COMP_LINE | sed -e 's@kiwi-ng@kiwi@')
6+
local result_comp_line
7+
local prev_was_option=0
8+
for item in $comp_line; do
9+
if [ $prev_was_option = 1 ];then
10+
prev_was_option=0
11+
continue
12+
fi
13+
if [[ $item =~ -.* ]];then
14+
prev_was_option=1
15+
continue
16+
fi
17+
result_comp_line="$result_comp_line $item"
18+
done
19+
echo $result_comp_line
20+
}
21+
22+
function _kiwi {
23+
local cur prev opts
24+
_get_comp_words_by_ref cur prev
25+
local cmd=$(setupCompletionLine | awk -F ' ' '{ print $NF }')
26+
for comp in $prev $cmd;do
27+
case "$comp" in
28+
"image")
29+
__comp_reply "info resize"
30+
return 0
31+
;;
32+
"result")
33+
__comp_reply "bundle list"
34+
return 0
35+
;;
36+
"system")
37+
__comp_reply "build create prepare update"
38+
return 0
39+
;;
40+
"build")
41+
__comp_reply "--add-bootstrap-package --add-container-label --add-package --add-repo --add-repo-credentials --allow-existing-root --clear-cache --delete-package --description --help --ignore-repos --ignore-repos-used-for-build --set-container-derived-from --set-container-tag --set-release-version --set-repo --set-repo-credentials --set-type-attr=<attribute --signing-key --target-dir help"
42+
return 0
43+
;;
44+
"bundle")
45+
__comp_reply "--bundle-dir --bundle-format --help --id --package-as-rpm --target-dir --zsync-source help"
46+
return 0
47+
;;
48+
"create")
49+
__comp_reply "--help --root --signing-key --target-dir help"
50+
return 0
51+
;;
52+
"info")
53+
__comp_reply "--description --help --list-profiles --resolve-package-list help"
54+
return 0
55+
;;
56+
"list")
57+
__comp_reply "--help --target-dir help"
58+
return 0
59+
;;
60+
"prepare")
61+
__comp_reply "--add-bootstrap-package --add-container-label --add-package --add-repo --add-repo-credentials --allow-existing-root --clear-cache --delete-package --description --help --ignore-repos --ignore-repos-used-for-build --root --set-container-derived-from --set-container-tag --set-release-version --set-repo --set-repo-credentials --set-type-attr=<attribute --signing-key help"
62+
return 0
63+
;;
64+
"resize")
65+
__comp_reply "--help --root --size --target-dir help"
66+
return 0
67+
;;
68+
"update")
69+
__comp_reply "--add-package --delete-package --help --root help"
70+
return 0
71+
;;
72+
esac
73+
done
74+
__comp_reply "--help --logfile --profile --version help image result system"
75+
return 0
76+
}
77+
#========================================
78+
# comp_reply
79+
#----------------------------------------
80+
function __comp_reply {
81+
word_list=$@
82+
COMPREPLY=($(compgen -W "$word_list" -- ${cur}))
83+
}
84+
85+
complete -F _kiwi -o default kiwi
86+
complete -F _kiwi -o default kiwi-ng

doc/source/commands/image_info.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ SYNOPSIS
1010
1111
kiwi-ng [global options] service <command> [<args>]
1212
13-
kiwi-ng image info -h | --help
13+
kiwi-ng image info --help
1414
kiwi-ng image info --description=<directory>
1515
[--resolve-package-list]
1616
[--list-profiles]
1717
[--print-kiwi-env]
1818
[--ignore-repos]
1919
[--add-repo=<source,type,alias,priority>...]
2020
[--print-xml|--print-yaml]
21-
kiwi-ng image info help
2221
2322
.. _db_image_info_desc:
2423

doc/source/commands/image_resize.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ SYNOPSIS
1212
1313
kiwi-ng [global options] service <command> [<args>]
1414
15-
kiwi-ng image resize -h | --help
15+
kiwi-ng image resize --help
1616
kiwi-ng image resize --target-dir=<directory> --size=<size>
1717
[--root=<directory>]
18-
kiwi-ng image resize help
1918
2019
.. _db_kiwi_image_resize_desc:
2120

doc/source/commands/kiwi.rst

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,13 @@ SYNOPSIS
88

99
.. code:: bash
1010
11-
kiwi-ng [global options] service <command> [<args>]
12-
13-
kiwi-ng -h | --help
14-
kiwi-ng [--profile=<name>...]
15-
[--temp-dir=<directory>]
16-
[--type=<build_type>]
17-
[--logfile=<filename>]
18-
[--logsocket=<socketfile>]
19-
[--loglevel=<number>]
20-
[--debug]
21-
[--debug-run-scripts-in-screen]
22-
[--color-output]
23-
[--config=<configfile>]
24-
[--kiwi-file=<kiwifile>]
25-
image <command> [<args>...]
26-
kiwi-ng [--logfile=<filename>]
27-
[--logsocket=<socketfile>]
28-
[--loglevel=<number>]
29-
[--debug]
30-
[--debug-run-scripts-in-screen]
31-
[--color-output]
32-
[--config=<configfile>]
33-
result <command> [<args>...]
34-
kiwi-ng [--profile=<name>...]
35-
[--shared-cache-dir=<directory>]
36-
[--temp-dir=<directory>]
37-
[--target-arch=<name>]
38-
[--type=<build_type>]
39-
[--logfile=<filename>]
40-
[--logsocket=<socketfile>]
41-
[--loglevel=<number>]
42-
[--debug]
43-
[--debug-run-scripts-in-screen]
44-
[--color-output]
45-
[--config=<configfile>]
46-
[--kiwi-file=<kiwifile>]
47-
system <command> [<args>...]
48-
kiwi-ng -v | --version
49-
kiwi-ng help
11+
kiwi-ng --help | --version
12+
13+
kiwi-ng [global options] image <command> [<args>...]
14+
kiwi-ng [global options] result <command> [<args>...]
15+
kiwi-ng [global options] system <command> [<args>...]
16+
17+
kiwi-ng help [kiwi::COMMAND::SUBCOMMAND]
5018
5119
.. _db_commands_kiwi_desc:
5220

@@ -95,8 +63,8 @@ GLOBAL OPTIONS
9563
--config=<configfile>
9664

9765
Use specified runtime configuration file. If not specified, the
98-
runtime configuration is expected to be in the :file:`~/.config/kiwi/config.yml`
99-
or :file:`/etc/kiwi.yml` files.
66+
runtime configuration is expected to be in the
67+
:file:`~/.config/kiwi/config.yml` or :file:`/etc/kiwi.yml` files.
10068

10169
--debug
10270

doc/source/commands/result_bundle.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ SYNOPSIS
1010
1111
kiwi-ng [global options] service <command> [<args>]
1212
13-
kiwi-ng result bundle -h | --help
13+
kiwi-ng result bundle --help
1414
kiwi-ng result bundle --target-dir=<directory> --id=<bundle_id> --bundle-dir=<directory>
1515
[--bundle-format=<format>]
1616
[--zsync_source=<download_location>]
1717
[--package-as-rpm]
18-
kiwi-ng result bundle help
1918
2019
.. _db_kiwi_result_bundle_desc:
2120

0 commit comments

Comments
 (0)