Plugins/UiExtensionPlugins/PipInterface/Pip.py

changeset 6327
a1716d9210f4
parent 6301
f488d2cc5841
child 6331
758b1cb7a2e6
--- a/Plugins/UiExtensionPlugins/PipInterface/Pip.py	Tue Jun 05 19:14:10 2018 +0200
+++ b/Plugins/UiExtensionPlugins/PipInterface/Pip.py	Wed Jun 06 20:05:39 2018 +0200
@@ -669,6 +669,7 @@
         self.__editor = MiniEditor(cfgFile, "Properties")
         self.__editor.show()
     
+    # TODO: add support for --user
     def __installPip(self):
         """
         Private slot to install pip.
@@ -708,12 +709,15 @@
                             "PipExecutables", pipExecutables)
     
     @pyqtSlot()
-    def upgradePip(self, pip=""):
+    def upgradePip(self, pip="", userSite=False):
         """
         Public method to upgrade pip itself.
         
         @param pip pip command to be used
         @type str
+        @param userSite flag indicating an install to the user install
+            directory
+        @type bool
         @return flag indicating a successful execution
         @rtype bool
         """
@@ -721,19 +725,14 @@
         # it must be done using the python executable
         
         if not pip:
-            default = self.tr("<Default>")
-            pipExecutables = sorted(
-                self.__plugin.getPreferences("PipExecutables"))
-            pip, ok = QInputDialog.getItem(
-                None,
-                self.tr("Upgrade pip"),
-                self.tr("Select pip Executable:"),
-                [default] + pipExecutables,
-                0, False)
-            if not ok or not pip:
-                return False
+            from .PipSelectionDialog import PipSelectionDialog
+            dlg = PipSelectionDialog(self.__plugin)
+            if dlg.exec_() != QDialog.Accepted:
+                return
             
-            if pip == default:
+            pip, userSite = dlg.getData()
+            
+            if not pip:
                 pip = self.__plugin.getPreferences("CurrentPipExecutable")
         
         python = self.__getPython(pip)
@@ -751,15 +750,20 @@
             indexUrl = \
                 self.__plugin.getPreferences("PipSearchIndex") + "/simple"
             args = ["-m", "pip", "install", "--index-url", indexUrl,
-                    "--upgrade", "pip"]
+                    "--upgrade"]
         else:
-            args = ["-m", "pip", "install", "--upgrade", "pip"]
+            args = ["-m", "pip", "install", "--upgrade"]
+        if userSite:
+            args.append("--user")
+        args.append("pip")
+        
         dia = PipDialog(self.tr('Upgrade PIP'))
         res = dia.startProcess(python, args)
         if res:
             dia.exec_()
         return res
     
+    # TODO: add support for --user
     @pyqtSlot()
     def __repairPip(self):
         """
@@ -878,14 +882,19 @@
         
         return abort
     
-    # TODO: add parameter userSite=False; arg: --user
-    def upgradePackages(self, packages, cmd=""):
+    def upgradePackages(self, packages, cmd="", userSite=False):
         """
         Public method to upgrade the given list of packages.
         
-        @param packages list of packages to upgrade (list of string)
-        @param cmd pip command to be used (string)
-        @return flag indicating a successful execution (boolean)
+        @param packages list of packages to upgrade
+        @type list of str
+        @param cmd pip command to be used
+        @type str
+        @param userSite flag indicating an install to the user install
+            directory
+        @type bool
+        @return flag indicating a successful execution
+        @rtype bool
         """
         if self.__checkUpgradePyQt(packages):
             return False
@@ -898,6 +907,8 @@
             args = ["install", "--index-url", indexUrl, "--upgrade"]
         else:
             args = ["install", "--upgrade"]
+        if userSite:
+            args.append("--user")
         args += packages
         dia = PipDialog(self.tr('Upgrade Packages'))
         res = dia.startProcess(cmd, args)
@@ -913,17 +924,21 @@
         dlg = PipPackagesInputDialog(
             self.__plugin, self.tr("Upgrade Packages"))
         if dlg.exec_() == QDialog.Accepted:
-            command, packages = dlg.getData()
+            command, packages, user = dlg.getData()
             if packages:
-                self.upgradePackages(packages, cmd=command)
+                self.upgradePackages(packages, cmd=command, userSite=user)
     
-    # TODO: add parameter userSite=False; arg: --user
-    def installPackages(self, packages, cmd=""):
+    def installPackages(self, packages, cmd="", userSite=False):
         """
         Public method to install the given list of packages.
         
-        @param packages list of packages to install (list of string)
-        @param cmd pip command to be used (string)
+        @param packages list of packages to install
+        @type list of str
+        @param cmd pip command to be used
+        @type str
+        @param userSite flag indicating an install to the user install
+            directory
+        @type bool
         """
         if not cmd:
             cmd = self.__plugin.getPreferences("CurrentPipExecutable")
@@ -933,6 +948,8 @@
             args = ["install", "--index-url", indexUrl]
         else:
             args = ["install"]
+        if userSite:
+            args.append("--user")
         args += packages
         dia = PipDialog(self.tr('Install Packages'))
         res = dia.startProcess(cmd, args)
@@ -947,9 +964,9 @@
         dlg = PipPackagesInputDialog(
             self.__plugin, self.tr("Install Packages"))
         if dlg.exec_() == QDialog.Accepted:
-            command, packages = dlg.getData()
+            command, packages, user = dlg.getData()
             if packages:
-                self.installPackages(packages, cmd=command)
+                self.installPackages(packages, cmd=command, userSite=user)
     
     def __installLocalPackage(self):
         """
@@ -958,9 +975,9 @@
         from .PipFileSelectionDialog import PipFileSelectionDialog
         dlg = PipFileSelectionDialog(self.__plugin, "package")
         if dlg.exec_() == QDialog.Accepted:
-            command, package = dlg.getData()
+            command, package, user = dlg.getData()
             if package and os.path.exists(package):
-                self.installPackages([package], cmd=command)
+                self.installPackages([package], cmd=command, userSite=user)
     
     def __installRequirements(self):
         """
@@ -969,7 +986,7 @@
         from .PipFileSelectionDialog import PipFileSelectionDialog
         dlg = PipFileSelectionDialog(self.__plugin, "requirements")
         if dlg.exec_() == QDialog.Accepted:
-            command, requirements = dlg.getData()
+            command, requirements, user = dlg.getData()
             if requirements and os.path.exists(requirements):
                 if not command:
                     command = self.__plugin.getPreferences(
@@ -981,6 +998,8 @@
                     args = ["install", "--index-url", indexUrl]
                 else:
                     args = ["install"]
+                if user:
+                    args.append("--user")
                 args += ["--requirement", requirements]
                 dia = PipDialog(self.tr('Install Packages from Requirements'))
                 res = dia.startProcess(command, args)
@@ -1021,9 +1040,9 @@
         """
         from .PipPackagesInputDialog import PipPackagesInputDialog
         dlg = PipPackagesInputDialog(
-            self.__plugin, self.tr("Uninstall Packages"))
+            self.__plugin, self.tr("Uninstall Packages"), install=False)
         if dlg.exec_() == QDialog.Accepted:
-            command, packages = dlg.getData()
+            command, packages, _user = dlg.getData()
             if packages:
                 self.uninstallPackages(packages, cmd=command)
     
@@ -1032,9 +1051,10 @@
         Private slot to uninstall packages as given in a requirements file.
         """
         from .PipFileSelectionDialog import PipFileSelectionDialog
-        dlg = PipFileSelectionDialog(self.__plugin, "requirements")
+        dlg = PipFileSelectionDialog(self.__plugin, "requirements",
+                                     install=False)
         if dlg.exec_() == QDialog.Accepted:
-            command, requirements = dlg.getData()
+            command, requirements, _user = dlg.getData()
             if requirements and os.path.exists(requirements):
                 try:
                     f = open(requirements, "r")

eric ide

mercurial