Introduce Settings class

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2019-11-24 11:59:09 +09:00
parent f0facbac77
commit 3e65a0c695
4 changed files with 140 additions and 5 deletions

View File

@@ -21,7 +21,6 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import json
import logging
import logging.config
import os
@@ -32,6 +31,7 @@ from packaging.version import Version, parse
from aqt.archives import QtArchives, ToolArchives
from aqt.installer import QtInstaller
from aqt.settings import Settings
class Cli():
@@ -40,18 +40,17 @@ class Cli():
__slot__ = ['parser', 'combinations', 'logger']
def __init__(self):
with open(os.path.join(os.path.dirname(__file__), 'combinations.json'), 'r') as j:
self.combinations = json.load(j)[0]
self.settings = Settings()
self._create_parser()
def _check_tools_arg_combination(self, os_name, tool_name, arch):
for c in self.combinations['tools']:
for c in self.settings.tools_combinations:
if c['os_name'] == os_name and c['tool_name'] == tool_name and c['arch'] == arch:
return True
return False
def _check_qt_arg_combination(self, qt_version, os_name, target, arch):
for c in self.combinations['qt']:
for c in self.settings.qt_combinations:
if c['os_name'] == os_name and c['target'] == target and c['arch'] == arch:
return True
return False
@@ -101,6 +100,15 @@ class Cli():
exit(1)
return mirror
def _check_modules_arg(self, qt_version, modules):
result = True
available = self.settings.available_modules(qt_version)
for m in modules:
if m not in available:
result = False
break
return result
def run_install(self, args):
arch = args.arch
target = args.target
@@ -113,6 +121,8 @@ class Cli():
mirror = self._check_mirror(args)
if not self._check_qt_arg_combination(qt_version, os_name, target, arch):
self.logger.warning("Specified target combination is not valid: {} {} {}".format(os_name, target, arch))
if not self._check_modules_arg(qt_version, modules):
self.logger.warning("Some of specified modules are unknown.")
QtInstaller(QtArchives(os_name, target, qt_version, arch, modules=modules, mirror=mirror, logging=self.logger),
logging=self.logger).install(command=sevenzip, target_dir=output_dir)
sys.stdout.write("\033[K")

View File

@@ -68,4 +68,25 @@
{"os_name": "windows", "target": "desktop", "tool_name": "tools_qtcreator", "arch": "qt.tools.qtcreator"},
{"os_name": "windows", "target": "desktop", "tool_name": "tools_qt3dstudio", "arch": "qt.tools.qt3dstudio"},
{"os_name": "windows", "target": "desktop", "tool_name": "tools_telemetry", "arch": "qt.tools.qtcreator.telemetry"}
], "modules": [
{"qt_version": "5.5", "moduels": ["qtquick1", "qtquickcontrols", "qtlocation", "qtcanvas3d", "qt3d",
"qtscript", "qtwebengine"]},
{"qt_version": "5.6", "moduels": ["qtquickcontrols2", "qtquickcontrols", "qtlocation", "qtcanvas3d", "qt3d",
"qtscript", "qtserialbus", "qtwebengine", "qtwebview"]},
{"qt_version": "5.7", "moduels": ["qtcharts", "qtgamepad", "qtquick2drenderer", "qtscxml", "qtpurchasing", "qtscript",
"qtserialbus", "qtvirtualkeyboard", "qtwebengine"]},
{"qt_version": "5.8", "moduels": ["qtcharts", "qtgamepad", "qtnetworkauth", "qtpurchasing", "qtdatavis3d",
"qtscript", "qtspeech", "qtvirtualkeyboard", "qtwebengine"]},
{"qt_version": "5.9", "moduels": ["qtcharts", "qtnetworkauth", "qtpurchasing", "qtdatavis3d", "qtremoteobjects",
"qtscript", "qtspeech", "qtvirtualkeyboard", "qtwebengine"]},
{"qt_version": "5.10", "moduels": ["qtcharts", "qtnetworkauth", "qtpurchasing", "qtdatavis3d", "qtremoteobjects",
"qtscript", "qtvirtualkeyboard", "qtwebengine", "qtwebglplugin"]},
{"qt_version": "5.11", "moduels": ["qtcharts", "qtnetworkauth", "qtpurchasing", "qtdatavis3d", "qtremoteobjects",
"qtscript", "qtvirtualkeyboard", "qtwebengine","qtwebglplugin"]},
{"qt_version": "5.12", "moduels": ["qtcharts", "qtnetworkauth", "qtpurchasing", "qtdatavis3d",
"qtscript", "qtvirtualkeyboard", "qtwebengine","qtwebglplugin"]},
{"qt_version": "5.13", "moduels": ["qtcharts", "qtlottie", "qtnetworkauth", "qtpurchasing", "qtdatavis3d",
"qtquicktimeline", "qtscript", "qtvirtualkeyboard", "qtwebglplugin"]},
{"qt_version": "5.14", "moduels": ["qtcharts", "qtlottie", "qtnetworkauth", "qtpurchasing", "qtdatavis3d",
"qtquick3d", "qtquicktimeline", "qtscript", "qtvirtualkeyboard", "qtwebglplugin"]}
]}]

7
aqt/settings.ini Normal file
View File

@@ -0,0 +1,7 @@
[DEFAULTS]
[aqt]
concurrency: 4
[mirrors]
blacklist: ['http://mirrors.ustc.edu.cn', 'http://mirrors.tuna.tsinghua.edu.cn', 'http://mirrors.geekpie.club']

97
aqt/settings.py Normal file
View File

@@ -0,0 +1,97 @@
#!/usr/bin/env python3
#
# Copyright (C) 2018 Linus Jahn <lnj@kaidan.im>
# Copyright (C) 2019 Hiroshi Miura <miurahr@linux.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import ast
import configparser
import json
import os
import threading
class Settings(object):
"""Class to hold configuration and settings.
Actual values are stored in 'settings.ini' file.
It also holds a combinations database.
"""
# this class is Borg/Singleton
_shared_state = {
'_config': None,
'_combinations': None,
'_lock': threading.Lock()
}
def __init__(self, config=None):
self.__dict__ = self._shared_state
if self._config is None:
with self._lock:
if self._config is None:
if config is None:
self.inifile = os.path.join(os.path.dirname(__file__), 'settings.ini')
else:
self.inifile = config
self._config = self.configParse(self.inifile)
with open(os.path.join(os.path.dirname(__file__), 'combinations.json'), 'r') as j:
self._combinations = json.load(j)[0]
def configParse(self, file_path):
if not os.path.exists(file_path):
raise IOError(file_path)
config = configparser.ConfigParser()
config.read(file_path)
return config
@property
def qt_combinations(self):
return self._combinations['qt']
@property
def tools_combinations(self):
return self._combinations['tools']
def available_modules(self, qt_version):
"""Known module names
:returns: dictionary of qt_version and module names
:rtype: List[str]
"""
modules = self._combinations['modules']
major, minor, _ = qt_version.split('.')
version = "{}.{}".format(major, minor)
return modules.get(version, None)
@property
def concurrency(self):
"""concurrency configuraiton.
:return: concurrency
:rtype: int
"""
return self._config.getint("aqt", "concurrency")
@property
def blacklist(self):
"""list of sites in a blacklist
:returns: list of site URLs(scheme and host part)
:rtype: List[str]
"""
return ast.literal_eval(self._config.get("mirrors", "blacklist"))