2024-10-31 10:01:39 +01:00
|
|
|
"""
|
|
|
|
|
Silique (https://www.silique.fr)
|
2025-02-10 10:00:09 +01:00
|
|
|
Copyright (C) 2024-2025
|
2024-10-31 10:01:39 +01:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
Mtools is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-05-14 08:27:11 +02:00
|
|
|
import os
|
2025-10-27 14:32:15 +01:00
|
|
|
from warnings import filterwarnings
|
2025-05-12 19:23:27 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
from sys import exit
|
2025-10-31 06:19:45 +01:00
|
|
|
from warnings import warn
|
2025-05-12 19:23:27 +02:00
|
|
|
|
2024-08-02 10:41:11 +02:00
|
|
|
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
2024-11-27 09:20:30 +01:00
|
|
|
from tiramisu.error import PropertiesOptionError
|
2025-11-02 18:44:17 +01:00
|
|
|
from tiramisu import MetaConfig
|
2024-08-02 10:41:11 +02:00
|
|
|
|
2024-11-27 09:20:30 +01:00
|
|
|
from rougail import Rougail
|
2025-12-19 19:02:47 +01:00
|
|
|
from rougail.user_data import UserData
|
2024-08-02 10:41:11 +02:00
|
|
|
from rougail.config import get_rougail_config
|
|
|
|
|
from rougail.utils import load_modules
|
2025-10-02 22:42:53 +02:00
|
|
|
from rougail.error import RougailWarning
|
2025-05-11 19:15:11 +02:00
|
|
|
|
2025-03-31 09:30:29 +02:00
|
|
|
try:
|
|
|
|
|
from rougail.user_data_yaml import RougailUserDataYaml
|
|
|
|
|
except ImportError:
|
|
|
|
|
RougailUserDataYaml = None
|
2024-11-27 09:20:30 +01:00
|
|
|
try:
|
|
|
|
|
from rougail.user_data_environment import RougailUserDataEnvironment
|
|
|
|
|
except ImportError:
|
|
|
|
|
RougailUserDataEnvironment = None
|
2025-10-27 14:32:15 +01:00
|
|
|
from .rougailconfig import load as rougailconfig_load
|
2024-08-02 10:41:11 +02:00
|
|
|
|
2024-10-31 10:01:39 +01:00
|
|
|
from .i18n import _
|
2024-08-02 10:41:11 +02:00
|
|
|
|
2024-10-31 10:01:39 +01:00
|
|
|
|
2025-05-14 08:27:11 +02:00
|
|
|
ENV_PREFIX = "ROUGAILCLI"
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 22:54:52 +02:00
|
|
|
def _main(arguments, do_not_print):
|
2025-10-06 21:40:38 +02:00
|
|
|
global print_traceback
|
2024-11-01 10:34:46 +01:00
|
|
|
rougailconfig = get_rougail_config(
|
|
|
|
|
backward_compatibility=False, add_extra_options=False
|
|
|
|
|
)
|
2025-12-19 19:02:47 +01:00
|
|
|
cmd_config = load_cmd_user_data(rougailconfig, arguments)
|
2025-10-06 21:40:38 +02:00
|
|
|
print_traceback = rougailconfig["cli.debug"]
|
|
|
|
|
if rougailconfig["cli.versions"]:
|
|
|
|
|
versions = display_version(cmd_config)
|
|
|
|
|
if do_not_print:
|
|
|
|
|
return list(versions)
|
|
|
|
|
for version in versions:
|
|
|
|
|
print(version)
|
2025-10-31 06:19:45 +01:00
|
|
|
exit()
|
2025-12-19 19:02:47 +01:00
|
|
|
layer_datas, metaconfig, config, err_warn = load_user_data(rougailconfig)
|
2025-11-07 08:40:30 +01:00
|
|
|
output = get_output(rougailconfig, metaconfig, config, err_warn, layer_datas)
|
2025-10-31 06:19:45 +01:00
|
|
|
if do_not_print:
|
|
|
|
|
return output.run()
|
|
|
|
|
ret = output.print()
|
|
|
|
|
if ret is False:
|
|
|
|
|
exit(1)
|
2025-10-06 21:40:38 +02:00
|
|
|
|
|
|
|
|
|
2025-12-19 19:02:47 +01:00
|
|
|
def load_cmd_user_data(rougailconfig, arguments):
|
2025-10-27 14:32:15 +01:00
|
|
|
rougailconfig.generate_config()
|
|
|
|
|
cmd_config = rougailconfig.config
|
2025-05-11 19:15:11 +02:00
|
|
|
origin_prop = cmd_config.property.default("read_write", "append")
|
|
|
|
|
cmd_config.property.setdefault(
|
|
|
|
|
frozenset(origin_prop | {"not_for_commandline"}), "read_write", "append"
|
|
|
|
|
)
|
2024-08-02 10:41:11 +02:00
|
|
|
cmd_config.property.read_write()
|
2025-12-19 19:02:47 +01:00
|
|
|
config_files = None
|
2025-03-31 10:24:01 +02:00
|
|
|
if RougailUserDataYaml:
|
2025-12-19 19:02:47 +01:00
|
|
|
_config_files = os.environ.pop(f"{ENV_PREFIX}_CLI.CONFIG_FILE", None)
|
|
|
|
|
if _config_files:
|
|
|
|
|
_config_files = _config_files.split(',')
|
|
|
|
|
else:
|
|
|
|
|
_config_files = cmd_config.forcepermissive.option("cli.config_file").value.get()
|
|
|
|
|
_config_files = [_config_file for _config_file in _config_files if Path(_config_file).is_file()]
|
|
|
|
|
if _config_files:
|
|
|
|
|
config_files = _config_files
|
2024-11-27 09:20:30 +01:00
|
|
|
if RougailUserDataEnvironment:
|
2025-10-27 14:32:15 +01:00
|
|
|
env_prefix = ENV_PREFIX
|
|
|
|
|
else:
|
|
|
|
|
env_prefix = None
|
2025-12-19 19:02:47 +01:00
|
|
|
user_data = rougailconfig_load(rougailconfig, config_files, env_prefix, True, _arguments=arguments, _generate=False, _add_help=False)
|
2025-10-31 06:19:45 +01:00
|
|
|
display_warnings = rougailconfig["cli.warnings"]
|
|
|
|
|
manage_warnings(display_warnings)
|
2025-10-06 21:40:38 +02:00
|
|
|
if not cmd_config.option("cli.versions").value.get():
|
2025-10-31 06:19:45 +01:00
|
|
|
if display_warnings and user_data["warnings"]:
|
|
|
|
|
for warning in user_data["warnings"]:
|
2025-12-19 19:02:47 +01:00
|
|
|
if isinstance(warning, dict):
|
|
|
|
|
warning = next(iter(warning))
|
2025-10-31 06:19:45 +01:00
|
|
|
warn(warning)
|
|
|
|
|
if manage_warnings:
|
|
|
|
|
# replays to display errors if needed
|
|
|
|
|
parser = TiramisuCmdlineParser(
|
|
|
|
|
cmd_config,
|
|
|
|
|
short_name_max_len=2,
|
|
|
|
|
)
|
|
|
|
|
parser.parse_args(arguments)
|
2025-05-12 19:23:27 +02:00
|
|
|
if user_data["errors"]:
|
|
|
|
|
raise Exception(user_data["errors"][0])
|
2025-10-06 21:40:38 +02:00
|
|
|
|
2025-05-11 19:15:11 +02:00
|
|
|
cmd_config.property.setdefault(origin_prop, "read_write", "append")
|
2024-08-02 10:41:11 +02:00
|
|
|
cmd_config.property.read_only()
|
2025-10-31 06:19:45 +01:00
|
|
|
cmd_config.property.remove("not_for_commandline")
|
|
|
|
|
|
2025-10-06 21:40:38 +02:00
|
|
|
return cmd_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_version(cmd_config):
|
|
|
|
|
versions = []
|
|
|
|
|
from tiramisu import __version__
|
|
|
|
|
yield(f"tiramisu: {__version__}")
|
|
|
|
|
|
|
|
|
|
from tiramisu_cmdline_parser import __version__
|
|
|
|
|
yield(f"tiramisu-cmdline-parser: {__version__}")
|
|
|
|
|
|
|
|
|
|
from rougail import __version__
|
|
|
|
|
yield(f"rougail: {__version__}")
|
|
|
|
|
|
|
|
|
|
from . import __version__
|
|
|
|
|
yield(f"rougail-cli: {__version__}")
|
|
|
|
|
|
|
|
|
|
for step in ["structural", "user_data", "output"]:
|
|
|
|
|
display_step = step.replace("_", "-")
|
|
|
|
|
for step_name in sorted(cmd_config.unrestraint.option(f"step.{step}").value.list()):
|
|
|
|
|
path = (
|
|
|
|
|
Path(__file__).parent.parent
|
|
|
|
|
/ (step + "_" + step_name)
|
|
|
|
|
/ "__init__.py"
|
|
|
|
|
)
|
|
|
|
|
if path.is_file():
|
|
|
|
|
try:
|
|
|
|
|
module = load_modules(
|
|
|
|
|
"rougail." + step + "_" + step_name, str(path)
|
|
|
|
|
)
|
|
|
|
|
yield(
|
|
|
|
|
f"rougail-{display_step}-{step_name}: {module.__version__}"
|
|
|
|
|
)
|
|
|
|
|
except Exception as err:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def manage_warnings(warnings):
|
|
|
|
|
if not warnings:
|
2025-10-02 22:42:53 +02:00
|
|
|
filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
|
filterwarnings("ignore", category=RougailWarning)
|
|
|
|
|
else:
|
|
|
|
|
filterwarnings("default", category=DeprecationWarning)
|
|
|
|
|
filterwarnings("default", category=RougailWarning)
|
2025-10-06 21:40:38 +02:00
|
|
|
|
2025-11-02 18:44:17 +01:00
|
|
|
|
2025-12-19 19:02:47 +01:00
|
|
|
def load_user_data(rougailconfig):
|
2025-11-07 08:40:30 +01:00
|
|
|
layer_datas = {}
|
2025-11-02 18:44:17 +01:00
|
|
|
if not rougailconfig["cli.load_config"]:
|
2025-11-07 13:05:28 +01:00
|
|
|
return None, None, None, {"errors": [], "warnings": []}
|
2024-11-05 08:35:25 +01:00
|
|
|
try:
|
|
|
|
|
user_data_names = rougailconfig["step.user_data"]
|
|
|
|
|
except PropertiesOptionError:
|
|
|
|
|
user_data_names = []
|
2025-11-06 06:32:13 +01:00
|
|
|
has_layers = rougailconfig["cli.layers"]
|
|
|
|
|
if has_layers:
|
2025-11-02 18:44:17 +01:00
|
|
|
layers = [[ud] for ud in user_data_names]
|
|
|
|
|
last_layers = len(layers) - 1
|
2025-05-02 08:09:14 +02:00
|
|
|
else:
|
2025-11-02 18:44:17 +01:00
|
|
|
layers = [user_data_names]
|
|
|
|
|
last_layers = 0
|
|
|
|
|
rougail = Rougail(rougailconfig)
|
|
|
|
|
layer_name = "_".join(layers[-1])
|
2025-11-06 06:32:13 +01:00
|
|
|
subconfig = rougail.run(name=layer_name)
|
2025-11-21 08:18:15 +01:00
|
|
|
try:
|
|
|
|
|
read_write = set()
|
|
|
|
|
read_only = set()
|
|
|
|
|
if rougailconfig["cli.inaccessible_read_write_modes"]:
|
|
|
|
|
read_write = set(rougailconfig["cli.inaccessible_read_write_modes"])
|
|
|
|
|
if rougailconfig["cli.inaccessible_modes"]:
|
|
|
|
|
read_only = set(rougailconfig["cli.inaccessible_modes"])
|
|
|
|
|
read_write |= read_only
|
|
|
|
|
if read_write:
|
|
|
|
|
subconfig.property.setdefault(
|
|
|
|
|
frozenset(subconfig.property.default("read_write", "append") | read_write), "read_write", "append"
|
|
|
|
|
)
|
|
|
|
|
subconfig.property.setdefault(
|
|
|
|
|
frozenset(subconfig.property.default("read_only", "remove") | (read_write - read_only)), "read_only", "remove"
|
|
|
|
|
)
|
2025-12-22 19:06:54 +01:00
|
|
|
for p in read_write:
|
|
|
|
|
subconfig.permissive.add(p)
|
2025-11-21 08:18:15 +01:00
|
|
|
if read_only:
|
|
|
|
|
subconfig.property.setdefault(
|
|
|
|
|
frozenset(subconfig.property.default("read_only", "append") | read_only), "read_only", "append"
|
|
|
|
|
)
|
2025-12-22 19:06:54 +01:00
|
|
|
for p in read_only:
|
|
|
|
|
subconfig.permissive.add(p)
|
2025-12-19 19:02:47 +01:00
|
|
|
if read_write or read_only:
|
2025-11-21 08:18:15 +01:00
|
|
|
subconfig.property.read_write()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2025-12-19 19:02:47 +01:00
|
|
|
subconfig.information.set("description_type", rougailconfig["cli.description_type"])
|
2025-11-06 06:32:13 +01:00
|
|
|
metaconfig = subconfig
|
2025-11-02 18:44:17 +01:00
|
|
|
if last_layers:
|
|
|
|
|
for layer in layers[:-1]:
|
|
|
|
|
layer_name = "_".join(layer)
|
|
|
|
|
metaconfig = MetaConfig([metaconfig], name=layer_name)
|
|
|
|
|
metaconfig.owner.set(metaconfig.path())
|
|
|
|
|
subconfig = metaconfig
|
|
|
|
|
err_warn = {"errors": [], "warnings": []}
|
2025-12-19 19:02:47 +01:00
|
|
|
invalid_user_data_error = rougailconfig["cli.invalid_user_data_error"]
|
|
|
|
|
unknown_user_data_error = rougailconfig["cli.unknown_user_data_error"]
|
|
|
|
|
interactive_user_data = {}
|
2025-11-02 18:44:17 +01:00
|
|
|
for idx, layer in enumerate(layers):
|
|
|
|
|
if idx:
|
|
|
|
|
subconfig = subconfig.config("_".join(layer))
|
2025-11-07 08:40:30 +01:00
|
|
|
layer_name = subconfig.path()
|
2025-11-06 06:32:13 +01:00
|
|
|
subconfig.owner.set(subconfig.path())
|
2025-11-07 08:40:30 +01:00
|
|
|
else:
|
|
|
|
|
layer_name = None
|
2025-11-02 18:44:17 +01:00
|
|
|
# data user
|
2025-12-19 19:02:47 +01:00
|
|
|
user_data = []
|
2025-11-07 13:05:28 +01:00
|
|
|
if has_layers:
|
|
|
|
|
layer_datas[layer_name] = {}
|
2025-11-02 18:44:17 +01:00
|
|
|
for user_data_name in layer:
|
|
|
|
|
path = (
|
|
|
|
|
Path(__file__).parent.parent
|
|
|
|
|
/ ("user_data_" + user_data_name)
|
|
|
|
|
/ "__init__.py"
|
2024-11-01 10:34:46 +01:00
|
|
|
)
|
2025-11-02 18:44:17 +01:00
|
|
|
if not path.is_file():
|
|
|
|
|
raise Exception(
|
|
|
|
|
_('cannot find "user_data" module "{0}"').format(user_data_name)
|
|
|
|
|
)
|
|
|
|
|
module = load_modules("rougail.user_data_" + user_data_name, str(path))
|
2025-12-19 19:02:47 +01:00
|
|
|
rougail_user_data = module.RougailUserData
|
|
|
|
|
if hasattr(rougail_user_data, 'interactive_user_data') and rougail_user_data.interactive_user_data:
|
|
|
|
|
interactive_user_data.setdefault(layer_name, {})[user_data_name] = rougail_user_data
|
2025-11-06 06:32:13 +01:00
|
|
|
continue
|
2025-12-19 19:02:47 +01:00
|
|
|
elif interactive_user_data:
|
2025-12-22 15:32:21 +01:00
|
|
|
raise Exception(_('interactive user data "{0}" is loader before uninteractive user data "{1}"').format(list(interactive_user_data), user_data_name))
|
2025-12-19 19:02:47 +01:00
|
|
|
for ud in rougail_user_data(
|
2025-11-02 18:44:17 +01:00
|
|
|
subconfig,
|
|
|
|
|
rougailconfig=rougailconfig,
|
2025-11-07 08:40:30 +01:00
|
|
|
).run():
|
2025-11-07 13:05:28 +01:00
|
|
|
if has_layers:
|
2025-12-19 19:02:47 +01:00
|
|
|
layer_datas[layer_name].setdefault(user_data_name, []).append(ud["source"])
|
|
|
|
|
user_data.append(ud)
|
|
|
|
|
if user_data:
|
|
|
|
|
new_err_warn = UserData(subconfig).user_data(user_data, invalid_user_data_error=invalid_user_data_error, unknown_user_data_error=unknown_user_data_error)
|
2025-11-06 06:32:13 +01:00
|
|
|
for level, datas in new_err_warn.items():
|
|
|
|
|
if datas:
|
2025-11-07 08:40:30 +01:00
|
|
|
err_warn[level].extend(datas)
|
2025-12-19 19:02:47 +01:00
|
|
|
for layer_name, interactive_user_data in interactive_user_data.items():
|
|
|
|
|
for layer, rougail_user_data in interactive_user_data.items():
|
2025-11-08 10:04:26 +01:00
|
|
|
if has_layers and len(layers) > 1:
|
|
|
|
|
subconfig = subconfig.config("_".join(layer))
|
|
|
|
|
subconfig.owner.set(subconfig.path())
|
2025-12-19 19:02:47 +01:00
|
|
|
for user_data in rougail_user_data(
|
2025-11-08 10:04:26 +01:00
|
|
|
subconfig,
|
|
|
|
|
rougailconfig=rougailconfig,
|
|
|
|
|
).run():
|
|
|
|
|
if has_layers:
|
2025-11-07 13:05:28 +01:00
|
|
|
layer_datas[layer_name].setdefault(user_data_name, []).append(user_data["source"])
|
2025-11-07 08:40:30 +01:00
|
|
|
|
|
|
|
|
return layer_datas, metaconfig, subconfig, err_warn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_output(rougailconfig, metaconfig, config, err_warn, layer_datas):
|
2024-08-02 10:41:11 +02:00
|
|
|
# output
|
2025-10-03 13:30:51 +02:00
|
|
|
if config and (not rougailconfig["cli.load_config"] or not rougailconfig["cli.read_write"]):
|
2025-05-02 08:09:14 +02:00
|
|
|
config.property.read_only()
|
2025-03-31 09:30:29 +02:00
|
|
|
output_name = rougailconfig["step.output"]
|
2024-11-01 10:34:46 +01:00
|
|
|
path = Path(__file__).parent.parent / ("output_" + output_name) / "__init__.py"
|
2024-08-02 10:41:11 +02:00
|
|
|
if not path.is_file():
|
2024-11-01 10:34:46 +01:00
|
|
|
raise Exception(
|
|
|
|
|
_('cannot find cli file for "output_name" module "{0}"').format(output_name)
|
|
|
|
|
)
|
|
|
|
|
module = load_modules("rougail.output_" + output_name, str(path))
|
2025-04-29 22:54:52 +02:00
|
|
|
output = module.RougailOutput(
|
2024-11-01 10:34:46 +01:00
|
|
|
config=config,
|
|
|
|
|
rougailconfig=rougailconfig,
|
|
|
|
|
user_data_errors=err_warn["errors"],
|
|
|
|
|
user_data_warnings=err_warn["warnings"],
|
2025-11-02 18:44:17 +01:00
|
|
|
config_owner_is_path=True,
|
2025-12-19 19:02:47 +01:00
|
|
|
root_config=metaconfig,
|
2025-11-07 08:40:30 +01:00
|
|
|
layer_datas=layer_datas,
|
2025-04-29 22:54:52 +02:00
|
|
|
)
|
2025-10-06 21:40:38 +02:00
|
|
|
return output
|
2024-10-31 10:01:39 +01:00
|
|
|
|
|
|
|
|
|
2025-04-29 22:54:52 +02:00
|
|
|
def main(arguments=None, do_not_print=False):
|
2025-02-17 10:01:23 +01:00
|
|
|
global print_traceback
|
2025-03-31 09:30:29 +02:00
|
|
|
print_traceback = True
|
2024-10-31 10:01:39 +01:00
|
|
|
try:
|
2025-04-29 22:54:52 +02:00
|
|
|
return _main(arguments, do_not_print)
|
2024-10-31 10:01:39 +01:00
|
|
|
except Exception as err:
|
2025-02-17 10:01:23 +01:00
|
|
|
if print_traceback:
|
|
|
|
|
import traceback
|
2025-05-11 19:15:11 +02:00
|
|
|
|
2025-02-17 10:01:23 +01:00
|
|
|
traceback.print_exc()
|
2025-05-02 08:09:14 +02:00
|
|
|
exit(_("ERROR: {0}").format(err))
|