Skip to content
Snippets Groups Projects
Commit d82a0b5b authored by Raphaël Gomès's avatar Raphaël Gomès
Browse files

bin: rewrite `env-desc` with `click`

This is much simpler and more powerful than rolling it by hand.
parent 8e4c0583
No related branches found
No related tags found
1 merge request!23Use click for argument parsing, etc.
#!/usr/bin/env python3
#
# Small utility to manipulate environment description
import os
# XXX rename to something other than `env-desc` and try to define
# where to define and where to expose those primitives.
import sys
......@@ -5,18 +4,8 @@
import sys
ACTION_SHOW = 'show'
ACTION_SET = 'set'
ACTION_GET = 'get'
ACTION_DEL = 'del'
ALL_ACTIONS = {
ACTION_SHOW,
ACTION_SET,
ACTION_GET,
ACTION_DEL,
}
import click
import poulpe
err = poulpe.err
......@@ -18,6 +7,15 @@
import poulpe
err = poulpe.err
@click.group(no_args_is_help=True)
def env_desc():
"""Low-level utility to manipulate Poulpe description trees.
Poulpe description trees are common to data-envs, bin-envs and benchmarks."""
@env_desc.command(no_args_is_help=True)
@click.argument("path")
def show(path):
......@@ -23,4 +21,9 @@
def show(path):
"""Display the contents of the description tree.
\b
ARGS:
<PATH> is the path to the binary environment directory"""
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
......@@ -24,7 +27,7 @@
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
return 3
sys.exit(3)
poulpe.show(data)
......@@ -28,4 +31,7 @@
poulpe.show(data)
@env_desc.command("get", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
def get_value(path, key):
......@@ -31,4 +37,11 @@
def get_value(path, key):
"""Display the value of a key from the description tree.
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable
"""
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
......@@ -32,6 +45,6 @@
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
return 3
sys.exit(3)
value = poulpe.get_one_value(data, key)
if value is None:
......@@ -36,8 +49,6 @@
value = poulpe.get_one_value(data, key)
if value is None:
return 1
else:
print(value)
return 0
sys.exit(1)
print(value)
......@@ -42,6 +53,32 @@
def set_value(path, key, value):
@env_desc.command("set", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
@click.argument("value")
@click.option(
"--type",
"typ",
type=click.Choice(["int", "bool", "string"]),
default="string",
help="Which type the value should be interpreted as",
)
def set_value(path, key, value, typ):
"""Set a key of the description tree to a new value.
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable
<VALUE> is the value to be set
"""
if typ == "int":
value = int(value)
elif typ == "bool":
value = bool(value)
else:
assert typ == "string", typ
data = poulpe.get_data(path)
if data is None:
err(f'creating new file: "{path}"')
......@@ -50,6 +87,5 @@
value = poulpe.set_one_value(data, key, value)
poulpe.write_data(path, data)
return 0
......@@ -54,3 +90,6 @@
@env_desc.command("del", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
def del_value(path, key):
......@@ -56,4 +95,10 @@
def del_value(path, key):
"""Remove a key from the description tree.
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable to delete
"""
data = poulpe.get_data(path)
if data is None:
err(f'creating new file: "{path}"')
......@@ -62,7 +107,6 @@
poulpe.del_one_value(data, key)
poulpe.write_data(path, data)
return 0
if __name__ == "__main__":
......@@ -66,51 +110,4 @@
if __name__ == "__main__":
if len(sys.argv) < 3:
t = os.path.split(sys.argv[0])[-1]
err(f"{t}: small utility to manipulate environment description")
err("")
err(f" $ {t} show PATH - Display description content")
err(f" $ {t} get PATH key - Display key value")
err(f" $ {t} set PATH key value - Set key value")
err(f" $ {t} del PATH key - Remove key")
sys.exit(2)
action = sys.argv[1]
path = sys.argv[2]
key = value = None
if action not in ALL_ACTIONS:
err(f"unknown action: {action}")
err("(use one of: %s)" % ', '.join(sorted(ALL_ACTIONS)))
sys.exit(2)
if action != ACTION_SHOW:
if len(sys.argv) < 4:
err("missing key")
sys.exit(2)
key = sys.argv[3]
if action == ACTION_SET:
if path == '--bool':
sys.argv.pop(2)
path = sys.argv[2]
key = sys.argv[3]
sys.argv[4] = bool(sys.argv[4])
elif path == '--int':
sys.argv.pop(2)
path = sys.argv[2]
key = sys.argv[3]
sys.argv[4] = int(sys.argv[4])
if len(sys.argv) < 5:
err("missing value")
sys.exit(2)
value = sys.argv[4]
if action == ACTION_SHOW:
ret = show(path)
elif action == ACTION_GET:
ret = get_value(path, key)
elif action == ACTION_SET:
ret = set_value(path, key, value)
elif action == ACTION_DEL:
ret = del_value(path, key)
sys.exit(ret)
env_desc()
......@@ -19,4 +19,4 @@
token="special-token-sdljlajslgjasljdlkasjlgjsjga-match-me"
nb_revs=`hg log --template "${token}\n" -R $repo | grep $token | wc -l`
poulpe env-desc set --int "$desc" data-env-vars.mercurial.repo.graph.visible-revision-count $nb_revs
poulpe env-desc set --type int "$desc" data-env-vars.mercurial.repo.graph.visible-revision-count $nb_revs
......@@ -7,5 +7,9 @@
no args
$ poulpe env-desc
env-desc: small utility to manipulate environment description
Usage: env-desc [OPTIONS] COMMAND [ARGS]...
Low-level utility to manipulate Poulpe description trees.
Poulpe description trees are common to data-envs, bin-envs and benchmarks.
......@@ -11,10 +15,13 @@
$ env-desc show PATH - Display description content
$ env-desc get PATH key - Display key value
$ env-desc set PATH key value - Set key value
$ env-desc del PATH key - Remove key
[2]
Options:
--help Show this message and exit.
Commands:
del Remove a key from the description tree.
get Display the value of a key from the description tree.
set Set a key of the description tree to a new value.
show Display the contents of the description tree.
too few args
$ poulpe env-desc foo
......@@ -17,6 +24,7 @@
too few args
$ poulpe env-desc foo
env-desc: small utility to manipulate environment description
Usage: env-desc [OPTIONS] COMMAND [ARGS]...
Try 'env-desc --help' for help.
......@@ -22,10 +30,7 @@
$ env-desc show PATH - Display description content
$ env-desc get PATH key - Display key value
$ env-desc set PATH key value - Set key value
$ env-desc del PATH key - Remove key
Error: No such command 'foo'.
[2]
bad action
$ poulpe env-desc foo bar
......@@ -27,12 +32,14 @@
[2]
bad action
$ poulpe env-desc foo bar
unknown action: foo
(use one of: del, get, set, show)
Usage: env-desc [OPTIONS] COMMAND [ARGS]...
Try 'env-desc --help' for help.
Error: No such command 'foo'.
[2]
missing GET key
$ poulpe env-desc get no-file
......@@ -34,11 +41,14 @@
[2]
missing GET key
$ poulpe env-desc get no-file
missing key
Usage: env-desc get [OPTIONS] PATH KEY
Try 'env-desc get --help' for help.
Error: Missing argument 'KEY'.
[2]
missing SET key
$ poulpe env-desc set no-file
......@@ -40,11 +50,14 @@
[2]
missing SET key
$ poulpe env-desc set no-file
missing key
Usage: env-desc set [OPTIONS] PATH KEY VALUE
Try 'env-desc set --help' for help.
Error: Missing argument 'KEY'.
[2]
missing SET value
$ poulpe env-desc set no-file key
......@@ -46,11 +59,14 @@
[2]
missing SET value
$ poulpe env-desc set no-file key
missing value
Usage: env-desc set [OPTIONS] PATH KEY VALUE
Try 'env-desc set --help' for help.
Error: Missing argument 'VALUE'.
[2]
missing DEL key
$ poulpe env-desc del no-file
......@@ -52,9 +68,12 @@
[2]
missing DEL key
$ poulpe env-desc del no-file
missing key
Usage: env-desc del [OPTIONS] PATH KEY
Try 'env-desc del --help' for help.
Error: Missing argument 'KEY'.
[2]
Missing file when reading
......
......@@ -75,7 +75,7 @@
$ poulpe env-desc set black-tiny-bad.pbd simple-command.accept-failure "1"
$ poulpe env-desc set black-tiny-bad.pbd simple-command.variables.file DATA-VARS:black.check.tiny.bad
$ poulpe env-desc set black-tiny-bad.pbd simple-command.cwd .
$ poulpe env-desc set --bool black-tiny-bad.pbd simple-command.variants.dimensions.string-normalization.yes.default 1
$ poulpe env-desc set --type bool black-tiny-bad.pbd simple-command.variants.dimensions.string-normalization.yes.default 1
$ poulpe env-desc set black-tiny-bad.pbd simple-command.variants.dimensions.string-normalization.no.extend-command " --skip-string-normalization"
$ poulpe env-desc show black-tiny-bad.pbd
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment