Introduce -c / --config command line option

option to specify settings.ini

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2021-05-23 14:19:34 +09:00
parent 089cc3427c
commit 827d8d53e1
3 changed files with 32 additions and 13 deletions

View File

@@ -73,14 +73,9 @@ class ExtractionError(Exception):
class Cli: class Cli:
"""CLI main class to parse command line argument and launch proper functions.""" """CLI main class to parse command line argument and launch proper functions."""
__slot__ = ["parser", "combinations", "logger"] __slot__ = ["parser", "combinations", "logger", "settings"]
def __init__(self, env_key="AQT_CONFIG"): def __init__(self):
config = os.getenv(env_key, None)
if config is not None and os.path.exists(config):
self.settings = Settings(config)
else:
self.settings = Settings()
self._create_parser() self._create_parser()
def _check_tools_arg_combination(self, os_name, tool_name, arch): def _check_tools_arg_combination(self, os_name, tool_name, arch):
@@ -454,7 +449,7 @@ class Cli:
sevenzip = self._set_sevenzip(args.external) sevenzip = self._set_sevenzip(args.external)
if EXT7Z and sevenzip is None: if EXT7Z and sevenzip is None:
# override when py7zr is not exist # override when py7zr is not exist
sevenzip = self._set_sevenzip("7z") sevenzip = self._set_sevenzip(self.settings.zipcmd)
version = args.version version = args.version
keep = args.keep keep = args.keep
if args.base is not None: if args.base is not None:
@@ -604,6 +599,13 @@ class Cli:
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
add_help=True, add_help=True,
) )
parser.add_argument(
"-c",
"--config",
type=argparse.FileType("r"),
nargs=1,
help="Configuration ini file.",
)
parser.add_argument( parser.add_argument(
"--logging-conf", "--logging-conf",
type=argparse.FileType("r"), type=argparse.FileType("r"),
@@ -756,8 +758,18 @@ class Cli:
else: else:
self.logger = logging.getLogger("aqt") self.logger = logging.getLogger("aqt")
def _setup_settings(self, args=None, env_key="AQT_CONFIG"):
config = os.getenv(env_key, None)
if args is not None and args.config is not None:
config = args.config
if config is not None and os.path.exists(config):
self.settings = Settings(config)
else:
self.settings = Settings()
def run(self, arg=None): def run(self, arg=None):
args = self.parser.parse_args(arg) args = self.parser.parse_args(arg)
self._setup_settings(args)
self._setup_logging(args) self._setup_logging(args)
return args.func(args) return args.func(args)

View File

@@ -6,9 +6,8 @@ Configuration
``aqtinstall`` can be configured through a configuration file. ``aqtinstall`` can be configured through a configuration file.
A default configuration is stored in ``aqt/settings.ini`` file. A default configuration is stored in ``aqt/settings.ini`` file.
You can specify custom configuration file through ``AQT_CONFIG`` environment variable. You can specify custom configuration file through ``AQT_CONFIG``
A configuration should read very early stage of ``aqtinstall``, it cannot be specified environment variable or "-c" or "--config" command line option.
through command line.
A file is like as follows: A file is like as follows:

View File

@@ -4,13 +4,15 @@ import aqt
def test_cli_help(capsys): def test_cli_help(capsys):
expected = "".join( expected = "".join(
[ [
"usage: aqt [-h] [--logging-conf LOGGING_CONF] [--logger LOGGER]\n", "usage: aqt [-h] [-c CONFIG] [--logging-conf LOGGING_CONF] [--logger LOGGER]\n",
" {install,doc,examples,src,tool,list,offline_installer,help} ...\n", " {install,doc,examples,src,tool,list,offline_installer,help} ...\n",
"\n", "\n",
"Installer for Qt SDK.\n", "Installer for Qt SDK.\n",
"\n", "\n",
"optional arguments:\n", "optional arguments:\n",
" -h, --help show this help message and exit\n", " -h, --help show this help message and exit\n",
" -c CONFIG, --config CONFIG\n",
" Configuration ini file.\n",
" --logging-conf LOGGING_CONF\n", " --logging-conf LOGGING_CONF\n",
" Logging configuration ini file.\n", " Logging configuration ini file.\n",
" --logger LOGGER Specify logger name\n", " --logger LOGGER Specify logger name\n",
@@ -30,6 +32,7 @@ def test_cli_help(capsys):
def test_cli_check_module(): def test_cli_check_module():
cli = aqt.installer.Cli() cli = aqt.installer.Cli()
cli._setup_settings()
assert cli._check_modules_arg("5.11.3", ["qtcharts", "qtwebengine"]) assert cli._check_modules_arg("5.11.3", ["qtcharts", "qtwebengine"])
assert not cli._check_modules_arg("5.7", ["not_exist"]) assert not cli._check_modules_arg("5.7", ["not_exist"])
assert cli._check_modules_arg("5.14.0", None) assert cli._check_modules_arg("5.14.0", None)
@@ -38,6 +41,7 @@ def test_cli_check_module():
def test_cli_check_combination(): def test_cli_check_combination():
cli = aqt.installer.Cli() cli = aqt.installer.Cli()
cli._setup_settings()
assert cli._check_qt_arg_combination("5.11.3", "linux", "desktop", "gcc_64") assert cli._check_qt_arg_combination("5.11.3", "linux", "desktop", "gcc_64")
assert cli._check_qt_arg_combination("5.11.3", "mac", "desktop", "clang_64") assert cli._check_qt_arg_combination("5.11.3", "mac", "desktop", "clang_64")
assert not cli._check_qt_arg_combination("5.14.0", "android", "desktop", "clang_64") assert not cli._check_qt_arg_combination("5.14.0", "android", "desktop", "clang_64")
@@ -45,12 +49,14 @@ def test_cli_check_combination():
def test_cli_check_version(): def test_cli_check_version():
cli = aqt.installer.Cli() cli = aqt.installer.Cli()
cli._setup_settings()
assert cli._check_qt_arg_versions("5.12.0") assert cli._check_qt_arg_versions("5.12.0")
assert not cli._check_qt_arg_versions("5.12") assert not cli._check_qt_arg_versions("5.12")
def test_cli_check_mirror(): def test_cli_check_mirror():
cli = aqt.installer.Cli() cli = aqt.installer.Cli()
cli._setup_settings()
assert cli._check_mirror(None) assert cli._check_mirror(None)
arg = ["install", "5.11.3", "linux", "desktop", "-b", "https://download.qt.io/"] arg = ["install", "5.11.3", "linux", "desktop", "-b", "https://download.qt.io/"]
args = cli.parser.parse_args(arg) args = cli.parser.parse_args(arg)
@@ -61,13 +67,15 @@ def test_cli_check_mirror():
def test_cli_launch_with_no_argument(capsys): def test_cli_launch_with_no_argument(capsys):
expected = "".join( expected = "".join(
[ [
"usage: aqt [-h] [--logging-conf LOGGING_CONF] [--logger LOGGER]\n", "usage: aqt [-h] [-c CONFIG] [--logging-conf LOGGING_CONF] [--logger LOGGER]\n",
" {install,doc,examples,src,tool,list,offline_installer,help} ...\n", " {install,doc,examples,src,tool,list,offline_installer,help} ...\n",
"\n", "\n",
"Installer for Qt SDK.\n", "Installer for Qt SDK.\n",
"\n", "\n",
"optional arguments:\n", "optional arguments:\n",
" -h, --help show this help message and exit\n", " -h, --help show this help message and exit\n",
" -c CONFIG, --config CONFIG\n",
" Configuration ini file.\n",
" --logging-conf LOGGING_CONF\n", " --logging-conf LOGGING_CONF\n",
" Logging configuration ini file.\n", " Logging configuration ini file.\n",
" --logger LOGGER Specify logger name\n", " --logger LOGGER Specify logger name\n",