All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.python.packaging.ui.PyPackageManagementService Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition python-community library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * 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.ui;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.RunCanceledByUserException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.CatchingConsumer;
import com.intellij.webcore.packaging.InstalledPackage;
import com.intellij.webcore.packaging.PackageManagementService;
import com.intellij.webcore.packaging.RepoPackage;
import com.jetbrains.python.packaging.*;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.sdk.PySdkUtil;
import com.jetbrains.python.sdk.PythonSdkType;
import org.apache.xmlrpc.AsyncCallback;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yole
 */
public class PyPackageManagementService extends PackageManagementService {
  @NotNull private static final Pattern PATTERN_ERROR_LINE = Pattern.compile(".*error:.*", Pattern.CASE_INSENSITIVE);

  private final Project myProject;
  private final Sdk mySdk;

  public PyPackageManagementService(Project project, Sdk sdk) {
    myProject = project;
    mySdk = sdk;
  }

  public Sdk getSdk() {
    return mySdk;
  }

  @Override
  public List getAllRepositories() {
    List result = new ArrayList();
    result.add(PyPIPackageUtil.PYPI_URL);
    result.addAll(PyPackageService.getInstance().additionalRepositories);
    return result;
  }

  @Override
  public void addRepository(String repositoryUrl) {
    PyPackageService.getInstance().addRepository(repositoryUrl);
  }

  @Override
  public void removeRepository(String repositoryUrl) {
    PyPackageService.getInstance().removeRepository(repositoryUrl);
  }

  @Override
  public List getAllPackages() throws IOException {
    final Map packageToVersionMap;
    try {
      packageToVersionMap = PyPIPackageUtil.INSTANCE.loadAndGetPackages();
    }
    catch (IOException e) {
      throw new IOException("Could not reach URL " + e.getMessage() + ". Please, check your internet connection.");
    }
    List packages = versionMapToPackageList(packageToVersionMap);
    packages.addAll(PyPIPackageUtil.INSTANCE.getAdditionalPackageNames());
    return packages;
  }

  private static List versionMapToPackageList(Map packageToVersionMap) {
    final boolean customRepoConfigured = !PyPackageService.getInstance().additionalRepositories.isEmpty();
    String url = customRepoConfigured ? PyPIPackageUtil.PYPI_URL : "";
    List packages = new ArrayList();
    for (Map.Entry entry : packageToVersionMap.entrySet()) {
      packages.add(new RepoPackage(entry.getKey(), url, entry.getValue()));
    }
    return packages;
  }

  @Override
  public List reloadAllPackages() throws IOException {
    final PyPackageService service = PyPackageService.getInstance();
    PyPIPackageUtil.INSTANCE.updatePyPICache(service);
    service.LAST_TIME_CHECKED = System.currentTimeMillis();
    return getAllPackages();
  }

  @Override
  public List getAllPackagesCached() {
    return versionMapToPackageList(PyPIPackageUtil.getPyPIPackages());
  }

  @Override
  public boolean canInstallToUser() {
    return !PythonSdkType.isVirtualEnv(mySdk);
  }

  @Override
  public String getInstallToUserText() {
    String userSiteText = "Install to user's site packages directory";
    if (!PythonSdkType.isRemote(mySdk))
      userSiteText += " (" + PySdkUtil.getUserSite() + ")";
    return userSiteText;
  }

  @Override
  public boolean isInstallToUserSelected() {
    return PyPackageService.getInstance().useUserSite(mySdk.getHomePath());
  }

  @Override
  public void installToUserChanged(boolean newValue) {
    PyPackageService.getInstance().addSdkToUserSite(mySdk.getHomePath(), newValue);
  }

  @Override
  public Collection getInstalledPackages() throws IOException {
    List packages;
    try {
      packages = PyPackageManager.getInstance(mySdk).getPackages(false);
      if (packages != null) {
        Collections.sort(packages, new Comparator() {
          @Override
          public int compare(@NotNull PyPackage pkg1, @NotNull PyPackage pkg2) {
            return pkg1.getName().compareTo(pkg2.getName());
          }
        });
      }
    }
    catch (ExecutionException e) {
      throw new IOException(e);
    }
    return packages != null ? new ArrayList(packages) : new ArrayList();
  }

  @Override
  public void installPackage(final RepoPackage repoPackage, String version, boolean forceUpgrade, String extraOptions,
                             final Listener listener, boolean installToUser) {
    final String packageName = repoPackage.getName();
    final String repository = PyPIPackageUtil.PYPI_URL.equals(repoPackage.getRepoUrl()) ? null : repoPackage.getRepoUrl();
    final List extraArgs = new ArrayList();
    if (installToUser) {
      extraArgs.add(PyPackageManager.USE_USER_SITE);
    }
    if (extraOptions != null) {
      // TODO: Respect arguments quotation
      Collections.addAll(extraArgs, extraOptions.split(" +"));
    }
    if (!StringUtil.isEmptyOrSpaces(repository)) {
      extraArgs.add("--extra-index-url");
      extraArgs.add(repository);
    }
    if (forceUpgrade) {
      extraArgs.add("-U");
    }
    final PyRequirement req;
    if (version != null) {
      req = new PyRequirement(packageName, version);
    }
    else {
      req = new PyRequirement(packageName);
    }

    final PyPackageManagerUI ui = new PyPackageManagerUI(myProject, mySdk, new PyPackageManagerUI.Listener() {
      @Override
      public void started() {
        listener.operationStarted(packageName);
      }

      @Override
      public void finished(@Nullable List exceptions) {
        listener.operationFinished(packageName, toErrorDescription(exceptions, mySdk));
      }
    });
    ui.install(Collections.singletonList(req), extraArgs);
  }

  @Nullable
  public static ErrorDescription toErrorDescription(@Nullable List exceptions, @NotNull Sdk sdk) {
    if (exceptions != null && !exceptions.isEmpty() && !isCancelled(exceptions)) {
      return createDescription(exceptions.get(0), sdk);
    }
    return null;
  }

  @Override
  public void uninstallPackages(List installedPackages, final Listener listener) {
    final String packageName = installedPackages.size() == 1 ? installedPackages.get(0).getName() : null;
    PyPackageManagerUI ui = new PyPackageManagerUI(myProject, mySdk, new PyPackageManagerUI.Listener() {
      @Override
      public void started() {
        listener.operationStarted(packageName);
      }

      @Override
      public void finished(final List exceptions) {
        listener.operationFinished(packageName, toErrorDescription(exceptions, mySdk));
      }
    });

    List pyPackages = new ArrayList();
    for (InstalledPackage aPackage : installedPackages) {
      if (aPackage instanceof PyPackage) {
        pyPackages.add((PyPackage)aPackage);
      }
    }
    ui.uninstall(pyPackages);
  }

  @Override
  public void fetchPackageVersions(final String packageName, final CatchingConsumer, Exception> consumer) {
    PyPIPackageUtil.INSTANCE.usePackageReleases(packageName, new AsyncCallback() {
      @Override
      public void handleResult(Object result, URL url, String method) {
        final List releases = (List)result;
        PyPIPackageUtil.INSTANCE.addPackageReleases(packageName, releases);
        consumer.consume(releases);
      }

      @Override
      public void handleError(Exception exception, URL url, String method) {
        consumer.consume(exception);
      }
    });
  }

  @Override
  public void fetchPackageDetails(final String packageName, final CatchingConsumer consumer) {
    PyPIPackageUtil.INSTANCE.fillPackageDetails(packageName, new AsyncCallback() {
      @Override
      public void handleResult(Object result, URL url, String method) {
        final Hashtable details = (Hashtable)result;
        PyPIPackageUtil.INSTANCE.addPackageDetails(packageName, details);
        consumer.consume(formatPackageDetails(details));
      }

      @Override
      public void handleError(Exception exception, URL url, String method) {
        consumer.consume(exception);
      }
    });
  }

  @NonNls private static final String TEXT_PREFIX = "" +
                                                    "    " +
                                                    "";
  @NonNls private static final String TEXT_SUFFIX = "";

  private static String formatPackageDetails(Hashtable details) {
    Object description = details.get("summary");
    StringBuilder stringBuilder = new StringBuilder(TEXT_PREFIX);
    if (description instanceof String) {
      stringBuilder.append(description).append("
"); } Object version = details.get("version"); if (version instanceof String && !StringUtil.isEmpty((String)version)) { stringBuilder.append("

Version

"); stringBuilder.append(version); } Object author = details.get("author"); if (author instanceof String && !StringUtil.isEmpty((String)author)) { stringBuilder.append("

Author

"); stringBuilder.append(author).append("

"); } Object authorEmail = details.get("author_email"); if (authorEmail instanceof String && !StringUtil.isEmpty((String)authorEmail)) { stringBuilder.append("
"); stringBuilder.append(composeHref("mailto:" + authorEmail)); } Object homePage = details.get("home_page"); if (homePage instanceof String && !StringUtil.isEmpty((String)homePage)) { stringBuilder.append("
"); stringBuilder.append(composeHref((String)homePage)); } stringBuilder.append(TEXT_SUFFIX); return stringBuilder.toString(); } @NonNls private static final String HTML_PREFIX = "" + vendorUrl + HTML_SUFFIX; } private static boolean isCancelled(@NotNull List exceptions) { for (ExecutionException e : exceptions) { if (e instanceof RunCanceledByUserException) { return true; } } return false; } @NotNull private static ErrorDescription createDescription(@NotNull ExecutionException e, @NotNull Sdk sdk) { if (e instanceof PyExecutionException) { final PyExecutionException ee = (PyExecutionException)e; final String stdoutCause = findErrorCause(ee.getStdout()); final String stderrCause = findErrorCause(ee.getStderr()); final String cause = stdoutCause != null ? stdoutCause : stderrCause; final String message = cause != null ? cause : ee.getMessage(); final String command = ee.getCommand() + " " + StringUtil.join(ee.getArgs(), " "); return new ErrorDescription(message, command, ee.getStdout() + "\n" + ee.getStderr(), findErrorSolution(ee, cause, sdk)); } else { return ErrorDescription.fromMessage(e.getMessage()); } } @Nullable private static String findErrorSolution(@NotNull PyExecutionException e, @Nullable String cause, @NotNull Sdk sdk) { if (cause != null) { if (StringUtil.containsIgnoreCase(cause, "SyntaxError")) { final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(sdk); return "Make sure that you use a version of Python supported by this package. Currently you are using Python " + languageLevel + "."; } } if (SystemInfo.isLinux && (containsInOutput(e, "pyconfig.h") || containsInOutput(e, "Python.h"))) { return "Make sure that you have installed Python development packages for your operating system."; } if ("pip".equals(e.getCommand())) { return "Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' " + "installed for your Python interpreter located at '" + sdk.getHomePath() + "'."; } return null; } private static boolean containsInOutput(@NotNull PyExecutionException e, @NotNull String text) { return StringUtil.containsIgnoreCase(e.getStdout(), text) || StringUtil.containsIgnoreCase(e.getStderr(), text); } @Nullable private static String findErrorCause(@NotNull String output) { final Matcher m = PATTERN_ERROR_LINE.matcher(output); if (m.find()) { final String result = m.group(); return result != null ? result.trim() : null; } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy