Search in sources :

Example 1 with PyPackage

use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.

the class RestPythonUtil method updateSphinxQuickStartRequiredAction.

public static Presentation updateSphinxQuickStartRequiredAction(final AnActionEvent e) {
    final Presentation presentation = e.getPresentation();
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project != null) {
        Module module = e.getData(LangDataKeys.MODULE);
        if (module == null) {
            Module[] modules = ModuleManager.getInstance(project).getModules();
            module = modules.length == 0 ? null : modules[0];
        }
        if (module != null) {
            final Sdk sdk = PythonSdkType.findPythonSdk(module);
            if (sdk != null) {
                final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
                final PyPackage sphinx = packages != null ? PyPackageUtil.findPackage(packages, "Sphinx") : null;
                presentation.setEnabled(sphinx != null);
            }
        }
    }
    return presentation;
}
Also used : Project(com.intellij.openapi.project.Project) PyPackage(com.jetbrains.python.packaging.PyPackage) Sdk(com.intellij.openapi.projectRoots.Sdk) Presentation(com.intellij.openapi.actionSystem.Presentation) Module(com.intellij.openapi.module.Module)

Example 2 with PyPackage

use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.

the class IpnbConnectionManager method startIpythonServer.

public boolean startIpythonServer(@NotNull final String initUrl, @NotNull final IpnbFileEditor fileEditor) {
    final Module module = ProjectFileIndex.SERVICE.getInstance(myProject).getModuleForFile(fileEditor.getVirtualFile());
    if (module == null)
        return false;
    final Sdk sdk = PythonSdkType.findPythonSdk(module);
    if (sdk == null) {
        showWarning(fileEditor, "Please check Python Interpreter in Settings->Python Interpreter", null);
        return false;
    }
    final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
    final PyPackage ipythonPackage = packages != null ? PyPackageUtil.findPackage(packages, "ipython") : null;
    final PyPackage jupyterPackage = packages != null ? PyPackageUtil.findPackage(packages, "jupyter") : null;
    if (ipythonPackage == null && jupyterPackage == null) {
        showWarning(fileEditor, "Add Jupyter to the interpreter of the current project.", null);
        return false;
    }
    String url = showDialogUrl(initUrl);
    if (url == null)
        return false;
    final IpnbSettings ipnbSettings = IpnbSettings.getInstance(myProject);
    ipnbSettings.setURL(url);
    final Pair<String, String> hostPort = getHostPortFromUrl(url);
    if (hostPort == null) {
        showWarning(fileEditor, "Please, check Jupyter Notebook URL in <a href=\"\">Settings->Tools->Jupyter Notebook</a>", new IpnbSettingsAdapter());
        return false;
    }
    final String homePath = sdk.getHomePath();
    if (homePath == null) {
        showWarning(fileEditor, "Python Sdk is invalid, please check Python Interpreter in Settings->Python Interpreter", null);
        return false;
    }
    Map<String, String> env = null;
    final ArrayList<String> parameters = Lists.newArrayList(homePath);
    String ipython = findJupyterRunner(homePath);
    if (ipython == null) {
        ipython = findIPythonRunner(homePath);
        if (ipython == null) {
            ipython = PythonHelper.LOAD_ENTRY_POINT.asParamString();
            env = ImmutableMap.of("PYCHARM_EP_DIST", "ipython", "PYCHARM_EP_NAME", "ipython");
        }
        parameters.add(ipython);
        parameters.add("notebook");
    } else {
        parameters.add(ipython);
    }
    parameters.add("--no-browser");
    if (hostPort.getFirst() != null) {
        parameters.add("--ip");
        parameters.add(hostPort.getFirst());
    }
    if (hostPort.getSecond() != null) {
        parameters.add("--port");
        parameters.add(hostPort.getSecond());
    }
    final String arguments = ipnbSettings.getArguments();
    if (!StringUtil.isEmptyOrSpaces(arguments)) {
        parameters.addAll(StringUtil.split(arguments, " "));
    }
    final String directory = ipnbSettings.getWorkingDirectory();
    final String baseDir = !StringUtil.isEmptyOrSpaces(directory) ? directory : ModuleRootManager.getInstance(module).getContentRoots()[0].getCanonicalPath();
    final GeneralCommandLine commandLine = new GeneralCommandLine(parameters).withWorkDirectory(baseDir);
    if (env != null) {
        commandLine.withEnvironment(env);
    }
    try {
        final boolean[] serverStarted = { false };
        final KillableColoredProcessHandler processHandler = new KillableColoredProcessHandler(commandLine) {

            @Override
            protected void doDestroyProcess() {
                super.doDestroyProcess();
                myKernels.clear();
                myToken = null;
                UnixProcessManager.sendSigIntToProcessTree(getProcess());
            }

            @Override
            public void coloredTextAvailable(@NotNull @NonNls String text, @NotNull Key attributes) {
                super.coloredTextAvailable(text, attributes);
                if (text.toLowerCase().contains("active kernels")) {
                    serverStarted[0] = true;
                }
                final String token = "?token=";
                if (text.toLowerCase().contains(token)) {
                    myToken = text.substring(text.indexOf(token) + token.length()).trim();
                }
            }

            @Override
            public boolean isSilentlyDestroyOnClose() {
                return true;
            }
        };
        processHandler.setShouldDestroyProcessRecursively(true);
        GuiUtils.invokeLaterIfNeeded(() -> new RunContentExecutor(myProject, processHandler).withTitle("Jupyter Notebook").withStop(() -> {
            myKernels.clear();
            processHandler.destroyProcess();
            UnixProcessManager.sendSigIntToProcessTree(processHandler.getProcess());
        }, () -> !processHandler.isProcessTerminated()).withRerun(() -> startIpythonServer(url, fileEditor)).withHelpId("reference.manage.py").withFilter(new UrlFilter()).run(), ModalityState.defaultModalityState());
        int countAttempt = 0;
        while (!serverStarted[0] && countAttempt < MAX_ATTEMPTS) {
            countAttempt += 1;
            TimeoutUtil.sleep(1000);
        }
        return true;
    } catch (ExecutionException e) {
        return false;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) UrlFilter(com.intellij.execution.filters.UrlFilter) NotNull(org.jetbrains.annotations.NotNull) PyPackage(com.jetbrains.python.packaging.PyPackage) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunContentExecutor(com.intellij.execution.RunContentExecutor) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key) KillableColoredProcessHandler(com.intellij.execution.process.KillableColoredProcessHandler)

Example 3 with PyPackage

use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.

the class VFSTestFrameworkListener method checkTestFrameworksInstalled.

@NotNull
private Map<String, Boolean> checkTestFrameworksInstalled(@Nullable Sdk sdk, @NotNull String... testPackageNames) {
    final Map<String, Boolean> result = new HashMap<>();
    if (sdk == null || StringUtil.isEmptyOrSpaces(sdk.getHomePath())) {
        LOG.info("Searching test runner in empty sdk");
        return result;
    }
    final PyPackageManager manager = PyPackageManager.getInstance(sdk);
    final boolean refreshed = PyPackageUtil.updatePackagesSynchronouslyWithGuard(manager, myIsUpdating);
    if (refreshed) {
        final List<PyPackage> packages = manager.getPackages();
        if (packages != null) {
            for (String name : testPackageNames) {
                result.put(name, PyPackageUtil.findPackage(packages, name) != null);
            }
        }
    }
    return result;
}
Also used : PyPackage(com.jetbrains.python.packaging.PyPackage) HashMap(java.util.HashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PyPackageManager(com.jetbrains.python.packaging.PyPackageManager) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PyPackage

use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.

the class ProjectSpecificSettingsStep method checkValid.

@Override
public boolean checkValid() {
    myInstallFramework = false;
    if (!super.checkValid()) {
        return false;
    }
    if (myProjectGenerator instanceof PythonProjectGenerator) {
        final Sdk sdk = getSdk();
        if (sdk == null) {
            if (!((PythonProjectGenerator) myProjectGenerator).hideInterpreter()) {
                setErrorText("No Python interpreter selected");
                return false;
            }
            return true;
        }
        if (PythonSdkType.isInvalid(sdk)) {
            setErrorText("Choose valid python interpreter");
            return false;
        }
        final List<String> warningList = new ArrayList<>();
        final boolean isPy3k = PythonSdkType.getLanguageLevelForSdk(sdk).isPy3K();
        try {
            acceptsSdk(myProjectGenerator, sdk, new File(myLocationField.getText()));
        } catch (final PythonProjectGenerator.PyNoProjectAllowedOnSdkException e) {
            setErrorText(e.getMessage());
            return false;
        }
        if (myRemotePathRequired && StringUtil.isEmpty(myRemotePathField.getTextField().getText())) {
            setErrorText("Remote path not provided");
            return false;
        }
        if (myProjectGenerator instanceof PyFrameworkProjectGenerator) {
            PyFrameworkProjectGenerator frameworkProjectGenerator = (PyFrameworkProjectGenerator) myProjectGenerator;
            String frameworkName = frameworkProjectGenerator.getFrameworkTitle();
            if (!isFrameworkInstalled(sdk)) {
                if (PyPackageUtil.packageManagementEnabled(sdk)) {
                    myInstallFramework = true;
                    final List<PyPackage> packages = PyPackageUtil.refreshAndGetPackagesModally(sdk);
                    if (packages == null) {
                        warningList.add(frameworkName + " will be installed on the selected interpreter");
                        return false;
                    }
                    if (!PyPackageUtil.hasManagement(packages)) {
                        warningList.add("Python packaging tools and " + frameworkName + " will be installed on the selected interpreter");
                    } else {
                        warningList.add(frameworkName + " will be installed on the selected interpreter");
                    }
                } else {
                    warningList.add(frameworkName + " is not installed on the selected interpreter");
                }
            }
            final ValidationResult warningResult = ((PythonProjectGenerator) myProjectGenerator).warningValidation(sdk);
            if (!warningResult.isOk()) {
                warningList.add(warningResult.getErrorMessage());
            }
            if (!warningList.isEmpty()) {
                final String warning = StringUtil.join(warningList, "<br/>");
                setWarningText(warning);
            }
            if (isPy3k && !((PyFrameworkProjectGenerator) myProjectGenerator).supportsPython3()) {
                setErrorText(frameworkName + " is not supported for the selected interpreter");
                return false;
            }
        }
    }
    return true;
}
Also used : PyPackage(com.jetbrains.python.packaging.PyPackage) PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) PyFrameworkProjectGenerator(com.jetbrains.python.newProject.PyFrameworkProjectGenerator) ArrayList(java.util.ArrayList) Sdk(com.intellij.openapi.projectRoots.Sdk) ValidationResult(com.intellij.facet.ui.ValidationResult) File(java.io.File)

Example 5 with PyPackage

use of com.jetbrains.python.packaging.PyPackage in project intellij-community by JetBrains.

the class SphinxRunConfiguration method createConfigurationEditor.

@Override
protected SettingsEditor<? extends RunConfiguration> createConfigurationEditor() {
    final SphinxTasksModel model = new SphinxTasksModel();
    if (!model.contains("pdf") && getSdk() != null) {
        final List<PyPackage> packages = PyPackageManager.getInstance(getSdk()).getPackages();
        if (packages != null) {
            final PyPackage rst2pdf = PyPackageUtil.findPackage(packages, "rst2pdf");
            if (rst2pdf != null) {
                model.add(13, "pdf");
            }
        }
    }
    RestConfigurationEditor editor = new RestConfigurationEditor(getProject(), this, model);
    editor.setConfigurationName("Sphinx task");
    editor.setOpenInBrowserVisible(false);
    editor.setInputDescriptor(FileChooserDescriptorFactory.createSingleFolderDescriptor());
    editor.setOutputDescriptor(FileChooserDescriptorFactory.createSingleFolderDescriptor());
    return editor;
}
Also used : RestConfigurationEditor(com.jetbrains.rest.run.RestConfigurationEditor) PyPackage(com.jetbrains.python.packaging.PyPackage)

Aggregations

PyPackage (com.jetbrains.python.packaging.PyPackage)8 Sdk (com.intellij.openapi.projectRoots.Sdk)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)2 NotNull (org.jetbrains.annotations.NotNull)2 ExecutionException (com.intellij.execution.ExecutionException)1 RunContentExecutor (com.intellij.execution.RunContentExecutor)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 UrlFilter (com.intellij.execution.filters.UrlFilter)1 KillableColoredProcessHandler (com.intellij.execution.process.KillableColoredProcessHandler)1 ValidationResult (com.intellij.facet.ui.ValidationResult)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 Key (com.intellij.openapi.util.Key)1 PyFrameworkProjectGenerator (com.jetbrains.python.newProject.PyFrameworkProjectGenerator)1 PythonProjectGenerator (com.jetbrains.python.newProject.PythonProjectGenerator)1 PyPackageManager (com.jetbrains.python.packaging.PyPackageManager)1 PyClass (com.jetbrains.python.psi.PyClass)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 RestConfigurationEditor (com.jetbrains.rest.run.RestConfigurationEditor)1 File (java.io.File)1