From d2a47ab8f27af7e74d34797464da85c128c17c37 Mon Sep 17 00:00:00 2001 From: Roman Lacko Date: Wed, 24 Jul 2013 15:19:22 +0200 Subject: Register qt.conf in Qt resource system to override the Qt builtins Register qt.conf in Qt resource system to override the built-in configuration variables if there is no default qt.conf in executable folder and qt.conf was not already registered in Qt resource system Change-Id: Ibed9f9e791dab575ef26bc54b351e5f5d4870542 Reviewed-by: John Ehresman --- PySide/__init__.py.in | 34 ++++------- PySide/_utils.py.in | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 24 deletions(-) (limited to 'PySide') diff --git a/PySide/__init__.py.in b/PySide/__init__.py.in index 183df2d30..5dd82ed9f 100644 --- a/PySide/__init__.py.in +++ b/PySide/__init__.py.in @@ -10,6 +10,16 @@ def _setupQtDirectories(): pysideDir = _utils.get_pyside_dir() + # Register PySide qt.conf to override the built-in + # configuration variables, if there is no default qt.conf in + # executable folder + prefix = pysideDir.replace('\\', '/') + _utils.register_qt_conf(prefix=prefix, + binaries=prefix, + plugins=prefix+"/plugins", + imports=prefix+"/imports", + translations=prefix+"/translations") + # On Windows add the PySide\openssl folder (if it exists) to the # PATH so the SSL DLLs can be found when Qt tries to dynamically # load them. Tell Qt to load them and then reset the PATH. @@ -28,28 +38,4 @@ def _setupQtDirectories(): finally: os.environ['PATH'] = path - # Tell Qt to look for plugins in the PySide package, if the - # plugins folder exists there, instead of just the default of - # looking only in Qt's install or build folder. - try: - from . import QtCore - except ImportError: - pass - else: - pluginsDir = os.path.join(pysideDir, 'plugins') - if os.path.exists(pluginsDir) and \ - pluginsDir not in QtCore.QCoreApplication.libraryPaths(): - QtCore.QCoreApplication.addLibraryPath(pluginsDir) - - # Tell Qt to look for qml imports in the PySide package, if the - # imports folder exists there. - importsDir = os.path.join(pysideDir, 'imports') - if os.path.exists(importsDir): - if 'QML_IMPORT_PATH' in os.environ: - qml_import_path = os.environ['QML_IMPORT_PATH'] - os.environ['QML_IMPORT_PATH'] = importsDir + os.pathsep + qml_import_path - else: - os.environ['QML_IMPORT_PATH'] = importsDir - - _setupQtDirectories() diff --git a/PySide/_utils.py.in b/PySide/_utils.py.in index b2fdd9306..b8199fc43 100644 --- a/PySide/_utils.py.in +++ b/PySide/_utils.py.in @@ -20,6 +20,7 @@ import sys import os +import fnmatch if sys.platform == 'win32': @@ -99,3 +100,165 @@ else: return os.path.abspath(os.path.dirname(__file__)) else: return os.path.abspath(os.path.dirname(QtCore.__file__)) + + +def _filter_match(name, patterns): + for pattern in patterns: + if pattern is None: + continue + if fnmatch.fnmatch(name, pattern): + return True + return False + + +def _dir_contains(dir, filter): + names = os.listdir(dir) + for name in names: + srcname = os.path.join(dir, name) + if not os.path.isdir(srcname) and _filter_match(name, filter): + return True + return False + + +def _rcc_write_number(out, number, width): + dividend = 1 + if width == 2: + dividend = 256 + elif width == 3: + dividend = 65536 + elif width == 4: + dividend = 16777216 + while dividend >= 1: + tmp = int(number / dividend) + out.append("%02x" % tmp) + number -= tmp * dividend + dividend = int(dividend / 256) + + +def _rcc_write_data(out, data): + _rcc_write_number(out, len(data), 4) + for d in data: + _rcc_write_number(out, ord(d), 1) + + +def _get_qt_conf_resource(prefix, binaries, plugins, imports, translations): + """ + Generate Qt resource with embedded qt.conf + """ + qt_conf_template = "\ +[Paths]\x0d\x0a\ +Prefix = %(prefix)s\x0d\x0a\ +Binaries = %(binaries)s\x0d\x0a\ +Imports = %(imports)s\x0d\x0a\ +Plugins = %(plugins)s\x0d\x0a\ +Translations = %(translations)s" + + rc_data_input = qt_conf_template % {"prefix": prefix, + "binaries": binaries, + "plugins": plugins, + "imports": imports, + "translations": translations} + rc_data_ouput = [] + _rcc_write_data(rc_data_ouput, rc_data_input) + + # The rc_struct and rc_name was pre-generated by pyside-rcc from file: + # + # + # qt/etc/qt.conf + # + # + PY_2 = sys.version_info[0] < 3 + if PY_2: + rc_struct = "\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\ +\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x0a\x00\x02\x00\x00\ +\x00\x01\x00\x00\x00\x03\x00\x00\x00\x16\x00\x00\x00\x00\x00\x01\x00\x00\ +\x00\x00" + rc_name = "\ +\x00\x02\x00\x00\x07\x84\x00q\x00t\x00\x03\x00\x00l\xa3\x00e\x00t\x00c\x00\ +\x07\x08t\xa6\xa6\x00q\x00t\x00.\x00c\x00o\x00n\x00f" + rc_data = "".join(rc_data_ouput).decode('hex') + else: + rc_struct = b"\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\ +\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x0a\x00\x02\x00\x00\ +\x00\x01\x00\x00\x00\x03\x00\x00\x00\x16\x00\x00\x00\x00\x00\x01\x00\x00\ +\x00\x00" + rc_name = b"\ +\x00\x02\x00\x00\x07\x84\x00q\x00t\x00\x03\x00\x00l\xa3\x00e\x00t\x00c\x00\ +\x07\x08t\xa6\xa6\x00q\x00t\x00.\x00c\x00o\x00n\x00f" + rc_data = bytes.fromhex("".join(rc_data_ouput)) + + return rc_struct, rc_name, rc_data + + +def register_qt_conf(prefix, binaries, plugins, imports, translations, + force=False): + """ + Register qt.conf in Qt resource system to override the built-in + configuration variables, if there is no default qt.conf in + executable folder and another qt.conf is not already registered in + Qt resource system. + """ + try: + from . import QtCore + except ImportError: + return + + # Check folder structure + if not prefix or not os.path.exists(prefix): + if force: + raise RuntimeError("Invalid prefix path specified: %s" % prefix) + else: + return + if not binaries or not os.path.exists(binaries): + if force: + raise RuntimeError("Invalid binaries path specified: %s" % binaries) + else: + return + else: + # Check if required Qt libs exists in binaries folder + if sys.platform == 'win32': + pattern = ["QtCore*.dll"] + else: + pattern = ["libQtCore.so.*"] + if not _dir_contains(binaries, pattern): + if force: + raise RuntimeError("QtCore lib not found in folder: %s" % \ + binaries) + else: + return + if not plugins or not os.path.exists(plugins): + if force: + raise RuntimeError("Invalid plugins path specified: %s" % plugins) + else: + return + if not imports or not os.path.exists(imports): + if force: + raise RuntimeError("Invalid imports path specified: %s" % imports) + else: + return + if not translations or not os.path.exists(translations): + if force: + raise RuntimeError("Invalid translations path specified: %s" \ + % translations) + else: + return + + # Check if there is no default qt.conf in executable folder + exec_prefix = os.path.dirname(sys.executable) + qtconf_path = os.path.join(exec_prefix, 'qt.conf') + if os.path.exists(qtconf_path) and not force: + return + + # Check if another qt.conf is not already registered in Qt resource system + if QtCore.QFile.exists(":/qt/etc/qt.conf") and not force: + return + + rc_struct, rc_name, rc_data = _get_qt_conf_resource(prefix, binaries, + plugins, imports, + translations) + QtCore.qRegisterResourceData(0x01, rc_struct, rc_name, rc_data) + + # Initialize the Qt library by querying the QLibraryInfo + prefixPath = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath) -- cgit v1.2.3