Search in sources :

Example 1 with PythonSdkType

use of com.jetbrains.python.sdk.PythonSdkType in project intellij-community by JetBrains.

the class PyConfigurableInterpreterList method getAllPythonSdks.

public List<Sdk> getAllPythonSdks(@Nullable final Project project) {
    List<Sdk> result = new ArrayList<>();
    for (Sdk sdk : getModel().getSdks()) {
        if (sdk.getSdkType() instanceof PythonSdkType) {
            result.add(sdk);
        }
    }
    Collections.sort(result, new PyInterpreterComparator(project));
    addDetectedSdks(result);
    return result;
}
Also used : ArrayList(java.util.ArrayList) Sdk(com.intellij.openapi.projectRoots.Sdk) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType)

Example 2 with PythonSdkType

use of com.jetbrains.python.sdk.PythonSdkType in project intellij-community by JetBrains.

the class PyProjectScopeBuilder method excludeSdkTestsScope.

@Nullable
private static GlobalSearchScope excludeSdkTestsScope(Project project, Sdk sdk) {
    if (sdk != null && sdk.getSdkType() instanceof PythonSdkType) {
        List<VirtualFile> excludedDirs = new ArrayList<>();
        VirtualFile libDir = findLibDir(sdk);
        if (libDir != null) {
            // superset of test dirs found in Python 2.5 to 3.1
            excludedDirs.addAll(findTestDirs(libDir, "test", "bsddb/test", "ctypes/test", "distutils/tests", "email/test", "importlib/test", "json/tests", "lib2to3/tests", "sqlite3/test", "tkinter/test", "idlelib/testcode.py"));
        }
        // XXX: Disable resolving to any third-party libraries from typeshed in the same places where we don't want SDK tests
        excludedDirs.addAll(Arrays.stream(sdk.getRootProvider().getFiles(OrderRootType.CLASSES)).filter(file -> PyTypeShed.INSTANCE.isInside(file) || PyTypeShed.INSTANCE.isInThirdPartyLibraries(file)).collect(Collectors.toList()));
        if (!excludedDirs.isEmpty()) {
            GlobalSearchScope scope = buildUnionScope(project, excludedDirs);
            return GlobalSearchScope.notScope(scope);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PythonSdkType

use of com.jetbrains.python.sdk.PythonSdkType in project intellij-community by JetBrains.

the class PythonSdkConfigurator method configureProject.

public void configureProject(final Project project, @NotNull final VirtualFile baseDir, Ref<Module> moduleRef) {
    // it it a virtualenv?
    final List<VirtualFile> children = VfsUtil.getChildren(baseDir, new VirtualFileFilter() {

        @Override
        public boolean accept(VirtualFile file) {
            return !Project.DIRECTORY_STORE_FOLDER.equals(file.getName());
        }
    });
    if (children.isEmpty())
        return;
    final PythonSdkType sdkType = PythonSdkType.getInstance();
    //find virtualEnv in project directory
    final List<String> candidates = new ArrayList<>();
    if (project == null)
        return;
    final VirtualFile rootDir = project.getBaseDir();
    if (rootDir != null)
        candidates.addAll(VirtualEnvSdkFlavor.findInDirectory(rootDir));
    if (!candidates.isEmpty()) {
        String filePath = candidates.get(0);
        if (StringUtil.startsWithChar(filePath, '~')) {
            final String home = SystemProperties.getUserHome();
            filePath = home + filePath.substring(1);
        }
        final Sdk virtualEnvSdk = SdkConfigurationUtil.createAndAddSDK(filePath, sdkType);
        if (virtualEnvSdk != null) {
            SdkConfigurationUtil.setDirectoryProjectSdk(project, virtualEnvSdk);
            SdkAdditionalData additionalData = virtualEnvSdk.getSdkAdditionalData();
            if (additionalData == null) {
                additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(virtualEnvSdk.getHomePath()));
                ((ProjectJdkImpl) virtualEnvSdk).setSdkAdditionalData(additionalData);
            }
            ((PythonSdkAdditionalData) additionalData).associateWithProject(project);
            return;
        }
        return;
    }
    final Sdk existingSdk = ProjectRootManager.getInstance(project).getProjectSdk();
    // SdkConfigurationUtil does the same
    if (existingSdk != null && existingSdk.getSdkType() == sdkType)
        return;
    final File executableFile = PythonSdkType.findExecutableFile(new File(project.getBasePath(), "bin"), "python");
    if (executableFile != null) {
        final File virtualEnvRoot = PythonSdkType.getVirtualEnvRoot(executableFile.getPath());
        if (virtualEnvRoot != null) {
            // yes, an unknown virtualenv; set it up as SDK
            final Sdk sdk = SdkConfigurationUtil.createAndAddSDK(executableFile.getPath(), sdkType);
            if (sdk != null) {
                SdkConfigurationUtil.setDirectoryProjectSdk(project, sdk);
                return;
            }
        }
    }
    // default
    SdkConfigurationUtil.configureDirectoryProjectSdk(project, new PreferredSdkComparator(), sdkType);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PreferredSdkComparator(com.jetbrains.python.sdk.PreferredSdkComparator) ArrayList(java.util.ArrayList) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) Sdk(com.intellij.openapi.projectRoots.Sdk) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Example 4 with PythonSdkType

use of com.jetbrains.python.sdk.PythonSdkType in project intellij-community by JetBrains.

the class PyIntegratedToolsProjectConfigurator method updateIntegratedTools.

private static void updateIntegratedTools(final Module module, final int delay) {
    ModalityState modality = ModalityState.current();
    final PyDocumentationSettings docSettings = PyDocumentationSettings.getInstance(module);
    AppExecutorUtil.getAppScheduledExecutorService().schedule(() -> ApplicationManager.getApplication().invokeLater(() -> {
        LOG.debug("Integrated tools configurator has started");
        if (module.isDisposed())
            return;
        @NotNull DocStringFormat docFormat = DocStringFormat.PLAIN;
        //check setup.py
        @NotNull String testRunner = detectTestRunnerFromSetupPy(module);
        if (!testRunner.isEmpty()) {
            LOG.debug("Test runner '" + testRunner + "' was discovered from setup.py in the module '" + module.getModuleFilePath() + "'");
        }
        //try to find test_runner import
        final String extension = PythonFileType.INSTANCE.getDefaultExtension();
        // Module#getModuleScope() and GlobalSearchScope#getModuleScope() search only in source roots
        final GlobalSearchScope searchScope = module.getModuleScope();
        final Collection<VirtualFile> pyFiles = FilenameIndex.getAllFilesByExt(module.getProject(), extension, searchScope);
        for (VirtualFile file : pyFiles) {
            if (file.getName().startsWith("test")) {
                if (testRunner.isEmpty()) {
                    //find test runner import
                    testRunner = checkImports(file, module);
                    if (!testRunner.isEmpty()) {
                        LOG.debug("Test runner '" + testRunner + "' was detected from imports in the file '" + file.getPath() + "'");
                    }
                }
            } else if (docFormat == DocStringFormat.PLAIN) {
                // detect docstring type
                docFormat = checkDocstring(file, module);
                if (docFormat != DocStringFormat.PLAIN) {
                    LOG.debug("Docstring format '" + docFormat + "' was detected from content of the file '" + file.getPath() + "'");
                }
            }
            if (!testRunner.isEmpty() && docFormat != DocStringFormat.PLAIN) {
                break;
            }
        }
        // Check test runners available in the module SDK
        if (testRunner.isEmpty()) {
            //check if installed in sdk
            final Sdk sdk = PythonSdkType.findPythonSdk(module);
            if (sdk != null && sdk.getSdkType() instanceof PythonSdkType) {
                final List<PyPackage> packages = PyPackageUtil.refreshAndGetPackagesModally(sdk);
                if (packages != null) {
                    final boolean nose = PyPackageUtil.findPackage(packages, PyNames.NOSE_TEST) != null;
                    final boolean pytest = PyPackageUtil.findPackage(packages, PyNames.PY_TEST) != null;
                    if (nose)
                        testRunner = PythonTestConfigurationsModel.PYTHONS_NOSETEST_NAME;
                    else if (pytest)
                        testRunner = PythonTestConfigurationsModel.PY_TEST_NAME;
                    if (!testRunner.isEmpty()) {
                        LOG.debug("Test runner '" + testRunner + "' was detected from SDK " + sdk);
                    }
                }
            }
        }
        final TestRunnerService runnerService = TestRunnerService.getInstance(module);
        if (runnerService != null) {
            if (testRunner.isEmpty()) {
                runnerService.setProjectConfiguration(PythonTestConfigurationsModel.PYTHONS_UNITTEST_NAME);
            } else {
                runnerService.setProjectConfiguration(testRunner);
                LOG.info("Test runner '" + testRunner + "' was detected by project configurator");
            }
        }
        // Documentation settings should have meaningful default already
        if (docFormat != DocStringFormat.PLAIN) {
            docSettings.setFormat(docFormat);
            LOG.info("Docstring format '" + docFormat + "' was detected by project configurator");
        }
    }, modality), delay, TimeUnit.MILLISECONDS);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocStringFormat(com.jetbrains.python.documentation.docstrings.DocStringFormat) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ModalityState(com.intellij.openapi.application.ModalityState) Collection(java.util.Collection) List(java.util.List) Sdk(com.intellij.openapi.projectRoots.Sdk) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) PyDocumentationSettings(com.jetbrains.python.documentation.PyDocumentationSettings)

Example 5 with PythonSdkType

use of com.jetbrains.python.sdk.PythonSdkType in project intellij-community by JetBrains.

the class PyBuiltinCache method getSkeletonFile.

@Nullable
private static PyFile getSkeletonFile(@NotNull final Project project, @NotNull Sdk sdk, @NotNull String name) {
    SdkTypeId sdkType = sdk.getSdkType();
    if (sdkType instanceof PythonSdkType) {
        final int index = name.indexOf(".");
        if (index != -1) {
            name = name.substring(0, index);
        }
        final List<PsiElement> results = PyResolveImportUtil.resolveQualifiedName(QualifiedName.fromComponents(name), PyResolveImportUtil.fromSdk(project, sdk));
        return as(ContainerUtil.getFirstItem(results), PyFile.class);
    }
    return null;
}
Also used : SdkTypeId(com.intellij.openapi.projectRoots.SdkTypeId) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PythonSdkType (com.jetbrains.python.sdk.PythonSdkType)6 Sdk (com.intellij.openapi.projectRoots.Sdk)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 SdkTypeId (com.intellij.openapi.projectRoots.SdkTypeId)2 NamedLibraryElement (com.intellij.ide.projectView.impl.nodes.NamedLibraryElement)1 NamedLibraryElementNode (com.intellij.ide.projectView.impl.nodes.NamedLibraryElementNode)1 ModalityState (com.intellij.openapi.application.ModalityState)1 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)1 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)1 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)1 LibraryOrSdkOrderEntry (com.intellij.openapi.roots.LibraryOrSdkOrderEntry)1 VirtualFileFilter (com.intellij.openapi.vfs.VirtualFileFilter)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 PyDocumentationSettings (com.jetbrains.python.documentation.PyDocumentationSettings)1 DocStringFormat (com.jetbrains.python.documentation.docstrings.DocStringFormat)1 PreferredSdkComparator (com.jetbrains.python.sdk.PreferredSdkComparator)1 PyDetectedSdk (com.jetbrains.python.sdk.PyDetectedSdk)1 PythonSdkAdditionalData (com.jetbrains.python.sdk.PythonSdkAdditionalData)1