1#!/usr/bin/env python
2import sipconfig
3import subprocess
4import os
5import site
6
7from distutils import sysconfig
8
9def commandOutput(cmd, arguments):
10    return subprocess.check_output([cmd] + arguments).strip().decode('utf-8')
11
12def getEnv(name, default):
13    return os.environ.get(name) or default
14
15class Config:
16    def __init__(self, qmakePath):
17        self.__qmakePath = qmakePath
18
19        qtVersion = self.qmakeVariable('QT_VERSION')
20        self.__hasQt4 = qtVersion.startswith('4.')
21        self.__hasQt5 = qtVersion.startswith('5.')
22
23        if self.__hasQt5:
24            from PyQt5.QtCore import PYQT_CONFIGURATION
25            self.__sipFlags = PYQT_CONFIGURATION['sip_flags']
26        elif self.__hasQt4:
27            try:
28                from PyQt4.QtCore import PYQT_CONFIGURATION
29                self.__sipFlags = PYQT_CONFIGURATION['sip_flags']
30            except ImportError:
31                from PyQt4 import pyqtconfig
32                config = pyqtconfig.Configuration()
33                self.__sipFlags = config.pyqt_sip_flags
34        else:
35            raise Exception('Qt version 4 or 5 is required!')
36
37    def qmakeOutput(self, *arguments):
38        return commandOutput(self.__qmakePath, list(arguments))
39
40    def qmakeVariable(self, name):
41        return self.qmakeOutput('-query', name)
42
43    def sipFlags(self):
44        return self.__sipFlags
45
46    def hasQt4(self):
47        return self.__hasQt4
48
49    def hasQt5(self):
50        return self.__hasQt5
51
52def main():
53    qmakePath = getEnv('QMAKE', 'qmake')
54    sipPath = getEnv('SIP', 'sip')
55
56    sipConfig = sipconfig.Configuration()
57    config = Config(qmakePath)
58
59    projectPath = getEnv('PROJECT_PATH', os.getcwd() + '/..')
60    includePath = getEnv('INCLUDE_PATH', projectPath)
61    libraryPath = getEnv('LIBRARY_PATH', projectPath + '/fakevim')
62    sipFilePath = getEnv('SIP_FILE_PATH', projectPath + '/python/fakevim.sip')
63    pyQtIncludePath = getEnv('PYQT_INCLUDE_PATH',
64            '/usr/share/sip/PyQt' + (config.hasQt5() and '5' or '4'))
65
66    commandOutput(sipPath, config.sipFlags().split(' ') + [
67        '-I', pyQtIncludePath,
68        '-b', 'fakevim_python.pro',
69        '-o', '-c', '.',
70        sipFilePath
71        ])
72
73    # Find libpython
74    pythonLibDir = sysconfig.get_config_var('LIBDIR')
75    pythonLdLibrary = sysconfig.get_config_var('LDLIBRARY')
76    pythonLibrary = pythonLibDir + "/" + pythonLdLibrary
77    if not os.path.isfile(pythonLibrary):
78        pythonLibrary = pythonLibDir + "/" \
79                + sysconfig.get_config_var('MULTIARCH') + "/" \
80                + pythonLdLibrary
81
82    with open('fakevim_python.pro', 'a') as pro:
83        pro.write(
84        '''
85        TEMPLATE = lib
86        CONFIG += release plugin no_plugin_name_prefix
87        QT += widgets
88
89        TARGET = $$target
90        HEADERS = $$headers "{projectPythonInclude}/fakevimproxy.h"
91        SOURCES = $$sources "{projectPythonInclude}/fakevimproxy.cpp"
92
93        INCLUDEPATH += "{sipInclude}" "{pythonInclude}" "{projectInclude}" "{projectPythonInclude}" "{includePath}" "{includePath}/fakevim"
94        LIBS += -Wl,-rpath,"{libraryPath}" -L"{libraryPath}" -lfakevim "{pythonLibrary}"
95        DEFINES += FAKEVIM_PYQT_MAJOR_VERSION={qtVersion}
96
97        isEmpty(PREFIX) {{
98            PREFIX = "{installPath}"
99        }}
100
101        target.path = $$PREFIX
102        INSTALLS += target
103        '''.format(
104            pythonInclude = sysconfig.get_python_inc(),
105            sipInclude = sipConfig.sip_inc_dir,
106            projectInclude = projectPath,
107            projectPythonInclude = projectPath + "/python",
108            includePath = includePath,
109            libraryPath = libraryPath,
110            pythonLibrary = pythonLibrary,
111            qtVersion = config.hasQt5() and 5 or 4,
112            installPath = site.getusersitepackages()
113            ).replace('\n        ', '\n')
114        )
115
116if __name__ == "__main__":
117    main()
118
119