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

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

This is much simpler and more powerful than rolling it by hand.
parent 07297ef8
No related branches found
No related tags found
1 merge request!23Use click for argument parsing, etc.
#!/usr/bin/env python3
#
# Small tool to manipulate bin-envs
import argparse
import os
......@@ -5,2 +2,3 @@
import os
import sys
import subprocess
......@@ -6,5 +4,6 @@
import subprocess
import sys
import click
import poulpe
......@@ -18,7 +17,7 @@
err = poulpe.err
def create_empty(env_path, method="manual"):
def _create_empty(env_path, method="manual"):
"""create a new environ (in a non-ready state)"""
os.makedirs(env_path, exist_ok=True)
base_data = {
......@@ -32,5 +31,5 @@
file_path = poulpe.bin_env_file(env_path)
if os.path.exists(file_path):
err('This is already a bin-env, aborting')
return ERR_CODE_EXISTS
sys.exit(ERR_CODE_EXISTS)
poulpe.write_data(file_path, base_data)
......@@ -36,4 +35,23 @@
poulpe.write_data(file_path, base_data)
return 0
def _mark_ready(path):
shell_path = os.path.join(path, poulpe.SHELL_FILE)
if not os.access(shell_path, os.X_OK):
err(f'cannot find an executable file at: "{shell_path}"')
sys.exit(ERR_CODE_NO_BIN_ENV_SHELL)
path = poulpe.bin_env_file(path)
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
sys.exit(ERR_CODE_NO_BIN_ENV_DESC)
data['ready'] = True
poulpe.write_data(path, data)
@click.group(no_args_is_help=True)
def bin_env_util():
"""Tool to manipulate binary environments"""
......@@ -38,5 +56,27 @@
def setup_one(env_path, script):
@bin_env_util.command(no_args_is_help=True)
@click.argument("path")
def create_empty(path):
"""Create a new empty bin-env at a given path
\b
ARGS:
<PATH> is the path of the environment to create
"""
_create_empty(path)
@bin_env_util.command(no_args_is_help=True)
@click.argument("path")
@click.argument("script")
def setup_one(path, script):
"""Create a new bin-env with a setup script.
\b
ARGS:
<PATH> is the path of the environment to create
<SCRIPT> is the path to the script to setup the environment
"""
if not os.access(script, os.X_OK):
err(f'no executable setup script at: "{script}"')
......@@ -41,8 +81,6 @@
if not os.access(script, os.X_OK):
err(f'no executable setup script at: "{script}"')
return ERR_CODE_NO_SETUP_SCRIPT
ret = create_empty(env_path, method="script")
if ret != 0:
return ret
sys.exit(ERR_CODE_NO_SETUP_SCRIPT)
_create_empty(path, method="script")
script = os.path.abspath(script)
try:
......@@ -47,5 +85,5 @@
script = os.path.abspath(script)
try:
subprocess.check_call(script, cwd=env_path)
subprocess.check_call(script, cwd=path)
except subprocess.CalledProcessError as exc:
err(f'script returned with status {exc.returncode}: {script}')
......@@ -50,4 +88,4 @@
except subprocess.CalledProcessError as exc:
err(f'script returned with status {exc.returncode}: {script}')
return ERR_CODE_SETUP_SCRIPT_FAILED
sys.exit(ERR_CODE_SETUP_SCRIPT_FAILED)
......@@ -53,3 +91,3 @@
env_file = poulpe.bin_env_file(env_path)
env_file = poulpe.bin_env_file(path)
data = poulpe.get_data(env_file)
......@@ -55,5 +93,5 @@
data = poulpe.get_data(env_file)
vars_file = os.path.join(env_path, "POULPE-VARS")
vars_file = os.path.join(path, "POULPE-VARS")
if os.path.exists(vars_file):
with open(vars_file) as f:
for line in f:
......@@ -63,6 +101,6 @@
k = f'bin-env-vars.{k}'
poulpe.set_one_value(data, k, v)
poulpe.write_data(env_file, data)
return mark_ready(env_path)
_mark_ready(path)
......@@ -67,8 +105,7 @@
def mark_ready(env_path):
shell_path = os.path.join(env_path, poulpe.SHELL_FILE)
if not os.access(shell_path, os.X_OK):
err(f'cannot find an executable file at: "{shell_path}"')
return ERR_CODE_NO_BIN_ENV_SHELL
@bin_env_util.command(no_args_is_help=True)
@click.argument("path")
def mark_ready(path):
"""Mark a bin-env as ready.
......@@ -74,11 +111,8 @@
path = poulpe.bin_env_file(env_path)
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
return ERR_CODE_NO_BIN_ENV_DESC
data['ready'] = True
poulpe.write_data(path, data)
return 0
\b
ARGS:
<PATH> is the path of the environment to mark as ready
"""
_mark_ready(path)
......@@ -83,7 +117,15 @@
def show(env_path):
path = poulpe.bin_env_file(env_path)
@bin_env_util.command(no_args_is_help=True)
@click.argument("path")
def show(path):
"""Show all data we have about this environment.
\b
ARGS:
<PATH> is the path of the environment to show
"""
path = poulpe.bin_env_file(path)
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
......@@ -87,5 +129,5 @@
data = poulpe.get_data(path)
if data is None:
err(f'missing file: "{path}"')
return ERR_CODE_NO_BIN_ENV_DESC
sys.exit(ERR_CODE_NO_BIN_ENV_DESC)
poulpe.show(data)
......@@ -91,4 +133,26 @@
poulpe.show(data)
return 0
@bin_env_util.command("get", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
def get_value(path, key):
"""Show a specific bin-env variable.
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable
"""
f = poulpe.bin_env_file(path)
data = poulpe.get_data(f)
if data is None:
err(f'missing file: "{f}"')
sys.exit(ERR_CODE_NO_BIN_ENV_DESC)
k = f'bin-env-vars.{key}'
value = poulpe.get_one_value(data, k)
if value is None:
sys.exit(ERR_CODE_NO_KEY)
print(value)
......@@ -93,41 +157,9 @@
def _parsers():
top_parser = argparse.ArgumentParser(prog='poulpe-bin-env-util')
subparsers = top_parser.add_subparsers(
help='available sub-command', dest='command', required=True
)
# XXX having PATH everywhere is not great
### bin-env-util create-empty PATH
cmd_parser = subparsers.add_parser(
'create-empty', help='create a new empty bin-env'
)
cmd_parser.add_argument(
'PATH',
help="the path to the (future) environment",
)
### bin-env-util setup-one-sh PATH SCRIPT
cmd_parser = subparsers.add_parser(
'setup-one', help='create a new bin-env with a setup script'
)
cmd_parser.add_argument(
'PATH',
help="the path of the environment",
)
cmd_parser.add_argument(
'SCRIPT',
help="the path to the script to setup the env",
)
### bin-env-util mark-ready PATH
cmd_parser = subparsers.add_parser(
'mark-ready', help='mark a bin-env as ready'
)
cmd_parser.add_argument(
'PATH',
help="the path to the environment",
)
@bin_env_util.command("set", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
@click.argument("value")
def set_value(path, key, value):
"""Set the value of a specific bin-env variable.
......@@ -133,56 +165,17 @@
### bin-env-util show PATH
cmd_parser = subparsers.add_parser(
'show', help='Show all data we have about this environment'
)
cmd_parser.add_argument(
'PATH',
help="the path to the environment",
)
### bin-env-util get PATH KEY
cmd_parser = subparsers.add_parser(
'get', help='Show a specific bin-env variable'
)
cmd_parser.add_argument(
'PATH',
help="the path to the environment",
)
cmd_parser.add_argument(
'KEY',
help="the path to the variable",
)
### bin-env-util set PATH KEY VALUE
cmd_parser = subparsers.add_parser(
'set', help='Set the value of a specific bin-env variable'
)
cmd_parser.add_argument(
'PATH',
help="the path to the environment",
)
cmd_parser.add_argument(
'KEY',
help="the path to the variable",
)
cmd_parser.add_argument(
'VALUE',
help="the value to set",
)
### bin-env-util del PATH KEY
cmd_parser = subparsers.add_parser(
'del', help='Delete a specific bin-env variable'
)
cmd_parser.add_argument(
'PATH',
help="the path to the environment",
)
cmd_parser.add_argument(
'KEY',
help="the path to the variable",
)
return top_parser
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable
<VALUE> is the value to be set
"""
f = poulpe.bin_env_file(path)
k = f'bin-env-vars.{key}'
data = poulpe.get_data(f)
if data is None:
err(f'creating new file: "{f}"')
data = {}
poulpe.set_one_value(data, k, value)
poulpe.write_data(f, data)
......@@ -187,47 +180,19 @@
def main(args):
parser = _parsers()
param = parser.parse_args(args)
if param.command == 'create-empty':
ret = create_empty(param.PATH)
elif param.command == 'setup-one':
ret = setup_one(param.PATH, param.SCRIPT)
elif param.command == 'mark-ready':
ret = mark_ready(param.PATH)
elif param.command == 'show':
ret = show(param.PATH)
elif param.command == 'get':
f = poulpe.bin_env_file(param.PATH)
data = poulpe.get_data(f)
if data is None:
err(f'missing file: "{f}"')
ret = ERR_CODE_NO_BIN_ENV_DESC
else:
k = f'bin-env-vars.{param.KEY}'
value = poulpe.get_one_value(data, k)
if value is None:
ret = ERR_CODE_NO_KEY
else:
print(value)
ret = 0
elif param.command == 'set':
f = poulpe.bin_env_file(param.PATH)
k = f'bin-env-vars.{param.KEY}'
data = poulpe.get_data(f)
if data is None:
err(f'creating new file: "{f}"')
data = {}
poulpe.set_one_value(data, k, param.VALUE)
poulpe.write_data(f, data)
return 0
elif param.command == 'del':
f = poulpe.bin_env_file(param.PATH)
k = f'bin-env-vars.{param.KEY}'
ret = poulpe.del_value(f, k)
else:
assert False
return ret
@bin_env_util.command("del", no_args_is_help=True)
@click.argument("path")
@click.argument("key")
def del_value(path, key):
"""Delete a specific bin-env variable.
\b
ARGS:
<PATH> is the path of the environment
<KEY> is the path to the variable to delete
"""
f = poulpe.bin_env_file(path)
k = f'bin-env-vars.{key}'
poulpe.del_value(f, k)
if __name__ == "__main__":
......@@ -231,6 +196,4 @@
if __name__ == "__main__":
ret = main(sys.argv[1:])
assert ret is not None
sys.exit(ret)
bin_env_util()
......@@ -4,7 +4,10 @@
Check basic invocation
$ poulpe bin-env-util --help
usage: poulpe-bin-env-util [-h]
{create-empty,setup-one,mark-ready,show,get,set,del}
...
Usage: bin-env-util [OPTIONS] COMMAND [ARGS]...
Tool to manipulate binary environments
Options:
--help Show this message and exit.
......@@ -10,17 +13,12 @@
positional arguments:
{create-empty,setup-one,mark-ready,show,get,set,del}
available sub-command
create-empty create a new empty bin-env
setup-one create a new bin-env with a setup script
mark-ready mark a bin-env as ready
show Show all data we have about this environment
get Show a specific bin-env variable
set Set the value of a specific bin-env variable
del Delete a specific bin-env variable
option*s: (glob)
-h, --help show this help message and exit
Commands:
create-empty Create a new empty bin-env at a given path
del Delete a specific bin-env variable.
get Show a specific bin-env variable.
mark-ready Mark a bin-env as ready.
set Set the value of a specific bin-env variable.
setup-one Create a new bin-env with a setup script.
show Show all data we have about this environment.
Check repository creation
......
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