summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/packaging/ui/PyInstalledPackagesPanel.java
blob: 348d2817ec75350cc70c1fabf590e9723444fd1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetbrains.python.packaging.ui;

import com.google.common.collect.Sets;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.util.Consumer;
import com.intellij.webcore.packaging.InstalledPackage;
import com.intellij.webcore.packaging.InstalledPackagesPanel;
import com.intellij.webcore.packaging.PackagesNotificationPanel;
import com.jetbrains.python.packaging.PyExternalProcessException;
import com.jetbrains.python.packaging.PyPackage;
import com.jetbrains.python.packaging.PyPackageManager;
import com.jetbrains.python.packaging.PyPackageManagerImpl;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.python.sdk.flavors.IronPythonSdkFlavor;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Set;

/**
 * @author yole
 */
public class PyInstalledPackagesPanel extends InstalledPackagesPanel {
  public static final String INSTALL_SETUPTOOLS = "installSetuptools";
  public static final String INSTALL_PIP = "installPip";
  public static final String CREATE_VENV = "createVEnv";

  private boolean myHasSetuptools;
  private boolean myHasPip = true;

  public PyInstalledPackagesPanel(Project project, PackagesNotificationPanel area) {
    super(project, area);
    myNotificationArea.addLinkHandler(INSTALL_SETUPTOOLS, new Runnable() {
      @Override
      public void run() {
        final Sdk sdk = getSelectedSdk();
        if (sdk != null) {
          installManagementTool(sdk, PyPackageManagerImpl.SETUPTOOLS);
        }
      }
    });
    myNotificationArea.addLinkHandler(INSTALL_PIP, new Runnable() {
      @Override
      public void run() {
        final Sdk sdk = getSelectedSdk();
        if (sdk != null) {
          installManagementTool(sdk, PyPackageManagerImpl.PIP);
        }
      }
    });
  }

  private Sdk getSelectedSdk() {
    PyPackageManagementService service = (PyPackageManagementService)myPackageManagementService;
    return service != null ? service.getSdk() : null;
  }

  public void updateNotifications(@Nullable final Sdk selectedSdk) {
    if (selectedSdk == null) {
      myNotificationArea.hide();
      return;
    }
    final Application application = ApplicationManager.getApplication();
    application.executeOnPooledThread(new Runnable() {
      @Override
      public void run() {
        PyExternalProcessException exc = null;
        try {
          PyPackageManagerImpl packageManager = (PyPackageManagerImpl)PyPackageManager.getInstance(selectedSdk);
          myHasSetuptools = packageManager.findInstalledPackage(PyPackageManagerImpl.PACKAGE_SETUPTOOLS) != null;
          if (!myHasSetuptools) {
            myHasSetuptools = packageManager.findInstalledPackage(PyPackageManagerImpl.PACKAGE_DISTRIBUTE) != null;
          }
          myHasPip = packageManager.findInstalledPackage(PyPackageManagerImpl.PACKAGE_PIP) != null;
        }
        catch (PyExternalProcessException e) {
          exc = e;
        }
        final PyExternalProcessException externalProcessException = exc;
        application.invokeLater(new Runnable() {
          @Override
          public void run() {
            if (selectedSdk == getSelectedSdk()) {
              final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(selectedSdk);
              final boolean invalid = PythonSdkType.isInvalid(selectedSdk);
              boolean allowCreateVirtualEnv =
                !(PythonSdkType.isRemote(selectedSdk) || flavor instanceof IronPythonSdkFlavor) &&
                !PythonSdkType.isVirtualEnv(selectedSdk) &&
                myNotificationArea.hasLinkHandler(CREATE_VENV);
              final String createVirtualEnvLink = "<a href=\"" + CREATE_VENV + "\">create new VirtualEnv</a>";
              myNotificationArea.hide();
              if (!invalid) {
                String text = null;
                if (externalProcessException != null) {
                  final int retCode = externalProcessException.getRetcode();
                  if (retCode == PyPackageManagerImpl.ERROR_NO_PIP) {
                    myHasPip = false;
                  }
                  else if (retCode == PyPackageManagerImpl.ERROR_NO_SETUPTOOLS) {
                    myHasSetuptools = false;
                  }
                  else {
                    text = externalProcessException.getMessage();
                  }
                  final boolean hasPackagingTools = myHasPip && myHasSetuptools;
                  allowCreateVirtualEnv &= !hasPackagingTools;

                  if (externalProcessException.hasHandler()) {
                    final String key = externalProcessException.getHandler().first;
                    myNotificationArea.addLinkHandler(key,
                                                      new Runnable() {
                                                        @Override
                                                        public void run() {
                                                          externalProcessException.getHandler().second.run();
                                                          myNotificationArea.removeLinkHandler(key);
                                                          updateNotifications(selectedSdk);
                                                        }
                                                      }
                    );
                  }
                }

                if (text == null) {
                  if (!myHasSetuptools) {
                    text = "Python package management tools not found. <a href=\"" + INSTALL_SETUPTOOLS + "\">Install 'setuptools'</a>";
                  }
                  else if (!myHasPip) {
                    text = "Python packaging tool 'pip' not found. <a href=\"" + INSTALL_PIP + "\">Install 'pip'</a>";
                  }
                }
                if (text != null) {
                  if (allowCreateVirtualEnv) {
                    text += " or " + createVirtualEnvLink;
                  }
                  myNotificationArea.showWarning(text);
                }
              }

              myInstallButton.setEnabled(!invalid && externalProcessException == null && myHasPip);
            }
          }
        }, ModalityState.any());
      }
    });
  }

  @Override
  protected Set<String> getPackagesToPostpone() {
    return Sets.newHashSet("pip", "distutils", "setuptools");
  }

  private void installManagementTool(@NotNull final Sdk sdk, final String name) {
    final PyPackageManagerImpl.UI ui = new PyPackageManagerImpl.UI(myProject, sdk, new PyPackageManagerImpl.UI.Listener() {
      @Override
      public void started() {
        myPackagesTable.setPaintBusy(true);
      }

      @Override
      public void finished(List<PyExternalProcessException> exceptions) {
        myPackagesTable.setPaintBusy(false);
        PyPackageManagerImpl packageManager = (PyPackageManagerImpl)PyPackageManager.getInstance(sdk);
        if (!exceptions.isEmpty()) {
          final String firstLine = "Install package failed. ";
          final String description = PyPackageManagerImpl.UI.createDescription(exceptions, firstLine);
          packageManager.showInstallationError(myProject, "Failed to install " + name, description);
        }
        packageManager.refresh();
        updatePackages(new PyPackageManagementService(myProject, sdk));
        for (Consumer<Sdk> listener : myPathChangedListeners) {
          listener.consume(sdk);
        }
        updateNotifications(sdk);
      }
    });
    ui.installManagement(name);
  }

  @Override
  protected boolean canUninstallPackage(InstalledPackage pkg) {
    if (!myHasPip) return false;
    if (PythonSdkType.isVirtualEnv(getSelectedSdk()) && pkg instanceof PyPackage) {
      final String location = ((PyPackage)pkg).getLocation();
      if (location != null && location.startsWith(PyPackageManagerImpl.getUserSite())) {
        return false;
      }
    }
    final String name = pkg.getName();
    if (PyPackageManagerImpl.PACKAGE_PIP.equals(name) ||
        PyPackageManagerImpl.PACKAGE_SETUPTOOLS.equals(name) ||
        PyPackageManagerImpl.PACKAGE_DISTRIBUTE.equals(name)) {
      return false;
    }
    return true;
  }

  @Override
  protected boolean canUpgradePackage(InstalledPackage pyPackage) {
    return myHasPip;
  }
}