Plugins/UiExtensionPlugins/PipInterface/Pip.py

changeset 6331
758b1cb7a2e6
parent 6327
a1716d9210f4
child 6342
c79ecba9cde7
--- a/Plugins/UiExtensionPlugins/PipInterface/Pip.py	Fri Jun 08 19:07:18 2018 +0200
+++ b/Plugins/UiExtensionPlugins/PipInterface/Pip.py	Fri Jun 08 19:24:24 2018 +0200
@@ -31,7 +31,6 @@
 
 
 # TODO: 1) change all uses of pip to python3 -m pip
-# TODO: 2) support --user for install, install --upgrade and list
 class Pip(QObject):
     """
     Class implementing the pip GUI logic.
@@ -183,11 +182,28 @@
             'Install the pip package itself'))
         self.installPipAct.setWhatsThis(self.tr(
             """<b>Install Pip</b>"""
-            """<p>This install the pip package itself.</p>"""
+            """<p>This installs the pip package itself.</p>"""
         ))
-        self.installPipAct.triggered.connect(self.__installPip)
+        self.installPipAct.triggered.connect(
+            lambda: self.__installPip(userSite=False))
         self.actions.append(self.installPipAct)
         
+        self.installPipUserAct = E5Action(
+            self.tr('Install Pip to User-Site'),
+            self.tr('Install Pip to User-Site'),
+            0, 0,
+            self, 'pip_install_pip_user')
+        self.installPipUserAct.setStatusTip(self.tr(
+            'Install the pip package itself to the user directory'))
+        self.installPipUserAct.setWhatsThis(self.tr(
+            """<b>Install Pip to User-Site</b>"""
+            """<p>This installs the pip package itself to the user"""
+            """ directory.</p>"""
+        ))
+        self.installPipUserAct.triggered.connect(
+            lambda: self.__installPip(userSite=True))
+        self.actions.append(self.installPipUserAct)
+        
         self.repairPipAct = E5Action(
             self.tr('Repair Pip'),
             self.tr('Repair Pip'),
@@ -371,6 +387,8 @@
         menu.addAction(self.listOutdatedPackagesAct)
         menu.addSeparator()
         menu.addAction(self.installPipAct)
+        menu.addAction(self.installPipUserAct)
+        menu.addSeparator()
         menu.addAction(self.installPackagesAct)
         menu.addAction(self.installLocalPackageAct)
         menu.addAction(self.installRequirementsAct)
@@ -406,6 +424,7 @@
         for act in self.actions:
             if act not in [self.selectExecutableAct,
                            self.installPipAct,
+                           self.installPipUserAct,
                            self.editUserConfigAct,
                            self.editVirtualenvConfigAct,
                            self.pipConfigAct]:
@@ -669,10 +688,13 @@
         self.__editor = MiniEditor(cfgFile, "Properties")
         self.__editor.show()
     
-    # TODO: add support for --user
-    def __installPip(self):
+    def __installPip(self, userSite=False):
         """
         Private slot to install pip.
+        
+        @param userSite flag indicating an install to the user install
+            directory
+        @type bool
         """
         python = E5FileDialog.getOpenFileName(
             None,
@@ -680,17 +702,21 @@
         if python:
             python = QDir.toNativeSeparators(python)
             dia = PipDialog(self.tr('Install PIP'))
-            commands = [(python, ["-m", "ensurepip"])]
+            if userSite:
+                commands = [(python, ["-m", "ensurepip", "--user"])]
+            else:
+                commands = [(python, ["-m", "ensurepip"])]
             if self.__plugin.getPreferences("PipSearchIndex"):
                 indexUrl = \
                     self.__plugin.getPreferences("PipSearchIndex") + "/simple"
-                commands.append(
-                    (python,
-                     ["-m", "pip", "install", "--index-url", indexUrl,
-                      "--upgrade", "pip"]))
+                args = ["-m", "pip", "install", "--index-url", indexUrl,
+                        "--upgrade"]
             else:
-                commands.append(
-                    (python, ["-m", "pip", "install", "--upgrade", "pip"]))
+                args = ["-m", "pip", "install", "--upgrade"]
+            if userSite:
+                args.append("--user")
+            args.append("pip")
+            commands.append((python, args[:]))
         
             res = dia.startProcesses(commands)
             if res:
@@ -763,7 +789,6 @@
             dia.exec_()
         return res
     
-    # TODO: add support for --user
     @pyqtSlot()
     def __repairPip(self):
         """
@@ -772,19 +797,14 @@
         @return flag indicating a successful execution
         @rtype bool
         """
-        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:
+        from .PipSelectionDialog import PipSelectionDialog
+        dlg = PipSelectionDialog(self.__plugin)
+        if dlg.exec_() != QDialog.Accepted:
             return False
         
-        if pip == default:
+        pip, userSite = dlg.getData()
+        
+        if not pip:
             pip = self.__plugin.getPreferences("CurrentPipExecutable")
         
         python = self.__getPython(pip)
@@ -803,9 +823,13 @@
             indexUrl = \
                 self.__plugin.getPreferences("PipSearchIndex") + "/simple"
             args = ["-m", "pip", "install", "--index-url", indexUrl,
-                    "--ignore-installed", "pip"]
+                    "--ignore-installed"]
         else:
-            args = ["-m", "pip", "install", "--ignore-installed", "pip"]
+            args = ["-m", "pip", "install", "--ignore-installed"]
+        if userSite:
+            args.append("--user")
+        args.append("pip")
+        
         dia = PipDialog(self.tr('Repair PIP'))
         res = dia.startProcess(python, args)
         if res:

eric ide

mercurial