summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/packaging/PyPackageManagerUI.java
blob: 28dfa6c834a4fb879a4e6609bd39a0477c5d1c60 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright 2000-2014 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;

import com.intellij.icons.AllIcons;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import com.intellij.webcore.packaging.PackagesNotificationPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.event.HyperlinkEvent;
import java.util.*;

/**
* @author vlan
*/
public class PyPackageManagerUI {
  private static final Logger LOG = Logger.getInstance(PyPackageManagerUI.class);
  @Nullable private Listener myListener;
  @NotNull private Project myProject;
  @NotNull private Sdk mySdk;

  public interface Listener {
    void started();

    void finished(List<PyExternalProcessException> exceptions);
  }

  public PyPackageManagerUI(@NotNull Project project, @NotNull Sdk sdk, @Nullable Listener listener) {
    myProject = project;
    mySdk = sdk;
    myListener = listener;
  }

  public void installManagement() {
    ProgressManager.getInstance().run(new InstallManagementTask(myProject, mySdk, myListener));
  }

  public void install(@NotNull final List<PyRequirement> requirements, @NotNull final List<String> extraArgs) {
    ProgressManager.getInstance().run(new InstallTask(myProject, mySdk, requirements, extraArgs, myListener));
  }

  public void uninstall(@NotNull final List<PyPackage> packages) {
    if (checkDependents(packages)) {
      return;
    }
    ProgressManager.getInstance().run(new UninstallTask(myProject, mySdk, myListener, packages));
  }

  private boolean checkDependents(@NotNull final List<PyPackage> packages) {
    try {
      final Map<String, Set<PyPackage>> dependentPackages = collectDependents(packages, mySdk);
      final int[] warning = {0};
      if (!dependentPackages.isEmpty()) {
        ApplicationManager.getApplication().invokeAndWait(new Runnable() {
          @Override
          public void run() {
            if (dependentPackages.size() == 1) {
              String message = "You are attempting to uninstall ";
              List<String> dep = new ArrayList<String>();
              int size = 1;
              for (Map.Entry<String, Set<PyPackage>> entry : dependentPackages.entrySet()) {
                final Set<PyPackage> value = entry.getValue();
                size = value.size();
                dep.add(entry.getKey() + " package which is required for " + StringUtil.join(value, ", "));
              }
              message += StringUtil.join(dep, "\n");
              message += size == 1 ? " package" : " packages";
              message += "\n\nDo you want to proceed?";
              warning[0] = Messages.showYesNoDialog(message, "Warning",
                                                    AllIcons.General.BalloonWarning);
            }
            else {
              String message = "You are attempting to uninstall packages which are required for another packages.\n\n";
              List<String> dep = new ArrayList<String>();
              for (Map.Entry<String, Set<PyPackage>> entry : dependentPackages.entrySet()) {
                dep.add(entry.getKey() + " -> " + StringUtil.join(entry.getValue(), ", "));
              }
              message += StringUtil.join(dep, "\n");
              message += "\n\nDo you want to proceed?";
              warning[0] = Messages.showYesNoDialog(message, "Warning",
                                                    AllIcons.General.BalloonWarning);
            }
          }
        }, ModalityState.current());
      }
      if (warning[0] != Messages.YES) return true;
    }
    catch (PyExternalProcessException e) {
      LOG.info("Error loading packages dependents: " + e.getMessage(), e);
    }
    return false;
  }

  private static Map<String, Set<PyPackage>> collectDependents(@NotNull final List<PyPackage> packages, Sdk sdk)
    throws PyExternalProcessException {
    Map<String, Set<PyPackage>> dependentPackages = new HashMap<String, Set<PyPackage>>();
    for (PyPackage pkg : packages) {
      final Set<PyPackage> dependents = PyPackageManager.getInstance(sdk).getDependents(pkg);
      if (dependents != null && !dependents.isEmpty()) {
        for (PyPackage dependent : dependents) {
          if (!packages.contains(dependent)) {
            dependentPackages.put(pkg.getName(), dependents);
          }
        }
      }
    }
    return dependentPackages;
  }

  private abstract static class PackagingTask extends Task.Backgroundable {
    private static final String PACKAGING_GROUP_ID = "Packaging";

    @Nullable protected final Listener myListener;

    public PackagingTask(@Nullable Project project, @NotNull String title, @Nullable Listener listener) {
      super(project, title);
      myListener = listener;
    }

    @Override
    public void run(@NotNull ProgressIndicator indicator) {
      taskStarted(indicator);
      taskFinished(runTask(indicator));
    }

    @NotNull
    protected abstract List<PyExternalProcessException> runTask(@NotNull ProgressIndicator indicator);

    @NotNull
    protected abstract String getSuccessTitle();

    @NotNull
    protected abstract String getSuccessDescription();

    @NotNull
    protected abstract String getFailureTitle();

    protected void taskStarted(@NotNull ProgressIndicator indicator) {
      indicator.setText(getTitle() + "...");
      if (myListener != null) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          @Override
          public void run() {
            myListener.started();
          }
        });
      }
    }

    protected void taskFinished(@NotNull final List<PyExternalProcessException> exceptions) {
      final Ref<Notification> notificationRef = new Ref<Notification>(null);
      if (exceptions.isEmpty()) {
        notificationRef.set(new Notification(PACKAGING_GROUP_ID, getSuccessTitle(), getSuccessDescription(),
                                             NotificationType.INFORMATION));
      }
      else {
        final String firstLine = getTitle() + ": error occurred.";
        final String description = createDescription(exceptions, firstLine);
        notificationRef.set(new Notification(PACKAGING_GROUP_ID, getFailureTitle(),
                                             firstLine + " <a href=\"xxx\">Details...</a>",
                                             NotificationType.ERROR,
                                             new NotificationListener() {
                                               @Override
                                               public void hyperlinkUpdate(@NotNull Notification notification,
                                                                           @NotNull HyperlinkEvent event) {
                                                 assert myProject != null;
                                                 PackagesNotificationPanel.showError(myProject, getFailureTitle(), description);
                                               }
                                             }
        ));
      }
      ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
          if (myListener != null) {
            myListener.finished(exceptions);
          }
          final Notification notification = notificationRef.get();
          if (notification != null) {
            notification.notify(myProject);
          }
        }
      });
    }
  }

  private static class InstallTask extends PackagingTask {
    @NotNull protected final Sdk mySdk;
    @NotNull private final List<PyRequirement> myRequirements;
    @NotNull private final List<String> myExtraArgs;

    public InstallTask(@Nullable Project project,
                       @NotNull Sdk sdk,
                       @NotNull List<PyRequirement> requirements,
                       @NotNull List<String> extraArgs,
                       @Nullable Listener listener) {
      super(project, "Installing packages", listener);
      mySdk = sdk;
      myRequirements = requirements;
      myExtraArgs = extraArgs;
    }

    @NotNull
    @Override
    protected List<PyExternalProcessException> runTask(@NotNull ProgressIndicator indicator) {
      final List<PyExternalProcessException> exceptions = new ArrayList<PyExternalProcessException>();
      final int size = myRequirements.size();
      final PyPackageManager manager = PyPackageManagers.getInstance().forSdk(mySdk);
      for (int i = 0; i < size; i++) {
        final PyRequirement requirement = myRequirements.get(i);
        indicator.setText(String.format("Installing package '%s'...", requirement));
        indicator.setFraction((double)i / size);
        try {
          manager.install(Arrays.asList(requirement), myExtraArgs);
        }
        catch (PyExternalProcessException e) {
          exceptions.add(e);
        }
      }
      manager.refresh();
      return exceptions;
    }

    @NotNull
    @Override
    protected String getSuccessTitle() {
      return "Packages installed successfully";
    }

    @NotNull
    @Override
    protected String getSuccessDescription() {
      return "Installed packages: " + PyPackageUtil.requirementsToString(myRequirements);
    }

    @NotNull
    @Override
    protected String getFailureTitle() {
      return "Install packages failed";
    }
  }

  private static class InstallManagementTask extends InstallTask {

    public InstallManagementTask(@Nullable Project project,
                                 @NotNull Sdk sdk,
                                 @Nullable Listener listener) {
      super(project, sdk, Collections.<PyRequirement>emptyList(), Collections.<String>emptyList(), listener);
    }

    @NotNull
    @Override
    protected List<PyExternalProcessException> runTask(@NotNull ProgressIndicator indicator) {
      final List<PyExternalProcessException> exceptions = new ArrayList<PyExternalProcessException>();
      final PyPackageManager manager = PyPackageManagers.getInstance().forSdk(mySdk);
      indicator.setText("Installing packaging tools...");
      indicator.setIndeterminate(true);
      try {
        manager.installManagement();
      }
      catch (PyExternalProcessException e) {
        exceptions.add(e);
      }
      manager.refresh();
      return exceptions;
    }

    @NotNull
    @Override
    protected String getSuccessDescription() {
      return "Installed Python packaging tools";
    }
  }

  private static class UninstallTask extends PackagingTask {
    @NotNull private final Sdk mySdk;
    @NotNull private final List<PyPackage> myPackages;

    public UninstallTask(@Nullable Project project,
                         @NotNull Sdk sdk,
                         @Nullable Listener listener,
                         @NotNull List<PyPackage> packages) {
      super(project, "Uninstalling packages", listener);
      mySdk = sdk;
      myPackages = packages;
    }

    @NotNull
    @Override
    protected List<PyExternalProcessException> runTask(@NotNull ProgressIndicator indicator) {
      final PyPackageManager manager = PyPackageManagers.getInstance().forSdk(mySdk);
      try {
        manager.uninstall(myPackages);
        return Arrays.asList();
      }
      catch (PyExternalProcessException e) {
        return Arrays.asList(e);
      }
      finally {
        manager.refresh();
      }
    }

    @NotNull
    @Override
    protected String getSuccessTitle() {
      return "Packages uninstalled successfully";
    }

    @NotNull
    @Override
    protected String getSuccessDescription() {
      final String packagesString = StringUtil.join(myPackages, new Function<PyPackage, String>() {
        @Override
        public String fun(PyPackage pkg) {
          return "'" + pkg.getName() + "'";
        }
      }, ", ");
      return "Uninstalled packages: " + packagesString;
    }

    @NotNull
    @Override
    protected String getFailureTitle() {
      return "Uninstall packages failed";
    }
  }

  public static String createDescription(List<PyExternalProcessException> exceptions, String firstLine) {
    final StringBuilder b = new StringBuilder();
    b.append(firstLine);
    b.append("\n\n");
    for (PyExternalProcessException exception : exceptions) {
      b.append(exception.toString());
      b.append("\n");
    }
    return b.toString();
  }
}