Skip to content

Commit

Permalink
Cleanup of the sip_configure.py file. (#131)
Browse files Browse the repository at this point in the history
This is to do 2 main things:

1.  Move away from distutils.find_executable, which is deprecated.
2.  Take a bunch of error handling out of an exception handler.
    This just makes the code easier to follow if another exception
    occurs.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette authored Dec 8, 2023
1 parent e78372f commit ee4d43b
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions cmake/sip_configure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from copy import copy
from distutils.spawn import find_executable
import copy
import os
import re
import shutil
Expand All @@ -17,9 +16,9 @@
class Configuration(sipconfig.Configuration):

def __init__(self):
env = copy(os.environ)
env = copy.copy(os.environ)
env['QT_SELECT'] = '5'
qmake_exe = 'qmake-qt5' if find_executable('qmake-qt5') else 'qmake'
qmake_exe = 'qmake-qt5' if shutil.which('qmake-qt5') else 'qmake'
qtconfig = subprocess.check_output(
[qmake_exe, '-query'], env=env, universal_newlines=True)
qtconfig = dict(line.split(':', 1) for line in qtconfig.splitlines())
Expand All @@ -45,7 +44,7 @@ def __init__(self):
macros = sipconfig._default_macros.copy()
macros['INCDIR_QT'] = qtconfig['QT_INSTALL_HEADERS']
macros['LIBDIR_QT'] = qtconfig['QT_INSTALL_LIBS']
macros['MOC'] = 'moc-qt5' if find_executable('moc-qt5') else 'moc'
macros['MOC'] = 'moc-qt5' if shutil.which('moc-qt5') else 'moc'
self.set_build_macros(macros)


Expand All @@ -61,25 +60,30 @@ def get_sip_dir_flags(config):
sip_flags = config.pyqt_sip_flags
return sip_dir, sip_flags
except AttributeError:
# sipconfig.Configuration does not have a pyqt_sip_dir or pyqt_sip_flags AttributeError
sip_flags = QtCore.PYQT_CONFIGURATION['sip_flags']

# Archlinux installs sip files here by default
default_sip_dir = os.path.join(PyQt5.__path__[0], 'bindings')
if os.path.exists(default_sip_dir):
return default_sip_dir, sip_flags

# sip4 installs here by default
default_sip_dir = os.path.join(sipconfig._pkg_config['default_sip_dir'], 'PyQt5')
if os.path.exists(default_sip_dir):
return default_sip_dir, sip_flags

# Homebrew installs sip files here by default
default_sip_dir = os.path.join(sipconfig._pkg_config['default_sip_dir'], 'Qt5')
if os.path.exists(default_sip_dir):
return default_sip_dir, sip_flags
raise FileNotFoundError('The sip directory for PyQt5 could not be located. Please ensure' +
' that PyQt5 is installed')
pass

# We didn't find the sip_dir and sip_flags from the config, continue looking

# sipconfig.Configuration does not have a pyqt_sip_dir or pyqt_sip_flags AttributeError
sip_flags = QtCore.PYQT_CONFIGURATION['sip_flags']

candidate_sip_dirs = []

# Archlinux installs sip files here by default
candidate_sip_dirs.append(os.path.join(PyQt5.__path__[0], 'bindings'))

# sip4 installs here by default
candidate_sip_dirs.append(os.path.join(sipconfig._pkg_config['default_sip_dir'], 'PyQt5'))

# Homebrew installs sip files here by default
candidate_sip_dirs.append(os.path.join(sipconfig._pkg_config['default_sip_dir'], 'Qt5'))

for sip_dir in candidate_sip_dirs:
if os.path.exists(sip_dir):
return sip_dir, sip_flags

raise FileNotFoundError('The sip directory for PyQt5 could not be located. Please ensure' +
' that PyQt5 is installed')


if len(sys.argv) != 8:
Expand Down

0 comments on commit ee4d43b

Please sign in to comment.