install.py

changeset 6211
4f0e08c95ca9
parent 6208
76445467adf8
child 6213
d7695d0aa4e7
diff -r f5b392aac28e -r 4f0e08c95ca9 install.py
--- a/install.py	Tue Apr 03 17:54:42 2018 +0200
+++ b/install.py	Wed Apr 04 15:36:10 2018 +0200
@@ -11,13 +11,15 @@
 
 from __future__ import unicode_literals, print_function
 try:
+    # Python2 only
     import cStringIO as io
     import sip
     sip.setapi('QString', 2)
     sip.setapi('QVariant', 2)
     sip.setapi('QTextStream', 2)
+    input = raw_input       # __IGNORE_WARNING__
 except (ImportError):
-    import io    # __IGNORE_WARNING__
+    import io               # __IGNORE_WARNING__
 
 import sys
 import os
@@ -221,7 +223,8 @@
                 pyqtVariant = "PyQt4"
                 del sys.modules["PyQt4"]
             except ImportError:
-                pyqtVariant = ""
+                # default to PyQt5, installation will be asked for
+                pyqtVariant = "PyQt5"
 
 
 def initGlobals():
@@ -240,6 +243,10 @@
         if not os.path.exists(platBinDir):
             platBinDir = platBinDirOld
     else:
+        # TODO: install scripts into {venv}/bin for virtual environments
+        # 1.) assume it is venv if os.path.dirname(sys.executable) is not in
+        # (/opt/local/bin, /usr/local/bin, /usr/bin) and
+        # 2.) directory is writable by user
         platBinDir = "/usr/local/bin"
 
     modDir = distutils.sysconfig.get_python_lib(True)
@@ -1126,6 +1133,29 @@
     copyToFile(fn, config)
 
 
+def pipInstall(packageName, message):
+    """
+    Install the given package via pip.
+    
+    @param packageName name of the package to be installed
+    @type str
+    @param message message to be shown to the user
+    @type str
+    @return flag indicating a successful installation
+    @rtype bool
+    """
+    ok = False
+    print(message, "\n")
+    answer = input("Shall '{0}' be installed using pip? (Y/n) "
+                   .format(packageName))
+    if answer in ("", "Y", "y"):
+        exitCode = subprocess.call(
+            [sys.executable, "-m", "pip", "install", packageName])
+        ok = (exitCode == 0)
+    
+    return ok
+
+
 def doDependancyChecks():
     """
     Perform some dependency checks.
@@ -1164,10 +1194,20 @@
         try:
             from PyQt5.QtCore import qVersion
         except ImportError as msg:
-            # TODO: install PyQt5 via pip upon request
-            print('Sorry, please install PyQt5.')
-            print('Error: {0}'.format(msg))
-            exit(1)
+            installed = pipInstall(
+                "PyQt5",
+                "PyQt5 could not be detected.\nError: {0}".format(msg)
+            )
+            if installed:
+                # try to import it again
+                try:
+                    from PyQt5.QtCore import qVersion
+                except ImportError as msg:
+                    print('Sorry, please install PyQt5.')
+                    print('Error: {0}'.format(msg))
+                    exit(1)
+            else:
+                exit(1)
         print("Found PyQt5")
     
     try:
@@ -1190,10 +1230,25 @@
             from PyQt5 import Qsci      # __IGNORE_WARNING__
     except ImportError as msg:
         # TODO: install QScintilla2 via pip upon request (PyQt5 only)
-        print("Sorry, please install QScintilla2 and")
-        print("its PyQt5/PyQt4 wrapper.")
-        print('Error: {0}'.format(msg))
-        exit(1)
+        if pyqtVariant == "PyQt4":
+            message = str(msg)
+        else:
+            installed = pipInstall(
+                "QScintilla",
+                "QScintilla could not be detected.\nError: {0}".format(msg)
+            )
+            if installed:
+                # try to import it again
+                try:
+                    from PyQt5 import Qsci      # __IGNORE_WARNING__
+                    message = None
+                except ImportError as msg:
+                    message = str(msg)
+        if message:
+            print("Sorry, please install QScintilla2 and")
+            print("its PyQt5/PyQt4 wrapper.")
+            print('Error: {0}'.format(msg))
+            exit(1)
     print("Found QScintilla2")
     
     if pyqtVariant == "PyQt4":

eric ide

mercurial