Skip to content

Commit ee4d43b

Browse files
authored
Cleanup of the sip_configure.py file. (#131)
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 <[email protected]>
1 parent e78372f commit ee4d43b

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

cmake/sip_configure.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from copy import copy
2-
from distutils.spawn import find_executable
1+
import copy
32
import os
43
import re
54
import shutil
@@ -17,9 +16,9 @@
1716
class Configuration(sipconfig.Configuration):
1817

1918
def __init__(self):
20-
env = copy(os.environ)
19+
env = copy.copy(os.environ)
2120
env['QT_SELECT'] = '5'
22-
qmake_exe = 'qmake-qt5' if find_executable('qmake-qt5') else 'qmake'
21+
qmake_exe = 'qmake-qt5' if shutil.which('qmake-qt5') else 'qmake'
2322
qtconfig = subprocess.check_output(
2423
[qmake_exe, '-query'], env=env, universal_newlines=True)
2524
qtconfig = dict(line.split(':', 1) for line in qtconfig.splitlines())
@@ -45,7 +44,7 @@ def __init__(self):
4544
macros = sipconfig._default_macros.copy()
4645
macros['INCDIR_QT'] = qtconfig['QT_INSTALL_HEADERS']
4746
macros['LIBDIR_QT'] = qtconfig['QT_INSTALL_LIBS']
48-
macros['MOC'] = 'moc-qt5' if find_executable('moc-qt5') else 'moc'
47+
macros['MOC'] = 'moc-qt5' if shutil.which('moc-qt5') else 'moc'
4948
self.set_build_macros(macros)
5049

5150

@@ -61,25 +60,30 @@ def get_sip_dir_flags(config):
6160
sip_flags = config.pyqt_sip_flags
6261
return sip_dir, sip_flags
6362
except AttributeError:
64-
# sipconfig.Configuration does not have a pyqt_sip_dir or pyqt_sip_flags AttributeError
65-
sip_flags = QtCore.PYQT_CONFIGURATION['sip_flags']
66-
67-
# Archlinux installs sip files here by default
68-
default_sip_dir = os.path.join(PyQt5.__path__[0], 'bindings')
69-
if os.path.exists(default_sip_dir):
70-
return default_sip_dir, sip_flags
71-
72-
# sip4 installs here by default
73-
default_sip_dir = os.path.join(sipconfig._pkg_config['default_sip_dir'], 'PyQt5')
74-
if os.path.exists(default_sip_dir):
75-
return default_sip_dir, sip_flags
76-
77-
# Homebrew installs sip files here by default
78-
default_sip_dir = os.path.join(sipconfig._pkg_config['default_sip_dir'], 'Qt5')
79-
if os.path.exists(default_sip_dir):
80-
return default_sip_dir, sip_flags
81-
raise FileNotFoundError('The sip directory for PyQt5 could not be located. Please ensure' +
82-
' that PyQt5 is installed')
63+
pass
64+
65+
# We didn't find the sip_dir and sip_flags from the config, continue looking
66+
67+
# sipconfig.Configuration does not have a pyqt_sip_dir or pyqt_sip_flags AttributeError
68+
sip_flags = QtCore.PYQT_CONFIGURATION['sip_flags']
69+
70+
candidate_sip_dirs = []
71+
72+
# Archlinux installs sip files here by default
73+
candidate_sip_dirs.append(os.path.join(PyQt5.__path__[0], 'bindings'))
74+
75+
# sip4 installs here by default
76+
candidate_sip_dirs.append(os.path.join(sipconfig._pkg_config['default_sip_dir'], 'PyQt5'))
77+
78+
# Homebrew installs sip files here by default
79+
candidate_sip_dirs.append(os.path.join(sipconfig._pkg_config['default_sip_dir'], 'Qt5'))
80+
81+
for sip_dir in candidate_sip_dirs:
82+
if os.path.exists(sip_dir):
83+
return sip_dir, sip_flags
84+
85+
raise FileNotFoundError('The sip directory for PyQt5 could not be located. Please ensure' +
86+
' that PyQt5 is installed')
8387

8488

8589
if len(sys.argv) != 8:

0 commit comments

Comments
 (0)