Search in sources :

Example 11 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyStudyDirectoryProjectGenerator method getBaseSdk.

private static String getBaseSdk(@NotNull final Project project) {
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    LanguageLevel baseLevel = LanguageLevel.PYTHON30;
    if (course != null) {
        final String version = course.getLanguageVersion();
        if (PyStudyLanguageManager.PYTHON_2.equals(version)) {
            baseLevel = LanguageLevel.PYTHON27;
        } else if (PyStudyLanguageManager.PYTHON_3.equals(version)) {
            baseLevel = LanguageLevel.PYTHON31;
        } else if (version != null) {
            baseLevel = LanguageLevel.fromPythonVersion(version);
        }
    }
    final PythonSdkFlavor flavor = PythonSdkFlavor.getApplicableFlavors(false).get(0);
    String baseSdk = null;
    final Collection<String> baseSdks = flavor.suggestHomePaths();
    for (String sdk : baseSdks) {
        final String versionString = flavor.getVersionString(sdk);
        final String prefix = flavor.getName() + " ";
        if (versionString != null && versionString.startsWith(prefix)) {
            final LanguageLevel level = LanguageLevel.fromPythonVersion(versionString.substring(prefix.length()));
            if (level.isAtLeast(baseLevel)) {
                baseSdk = sdk;
                break;
            }
        }
    }
    return baseSdk != null ? baseSdk : baseSdks.iterator().next();
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PythonSdkFlavor(com.jetbrains.python.sdk.flavors.PythonSdkFlavor) Course(com.jetbrains.edu.learning.courseFormat.Course)

Example 12 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyEnvTaskRunner method runTask.

// todo: doc
public void runTask(PyTestTask testTask, String testName, @NotNull final String... tagsRequiedByTest) {
    boolean wasExecuted = false;
    List<String> passedRoots = Lists.newArrayList();
    for (String root : myRoots) {
        LOG.warn(String.format("Running on root %s", root));
        final Set<String> requredTags = Sets.union(testTask.getTags(), Sets.newHashSet(tagsRequiedByTest));
        final boolean suitableForTask = isSuitableForTask(PyEnvTestCase.loadEnvTags(root), requredTags);
        final boolean shouldRun = shouldRun(root, testTask);
        if (!suitableForTask || !shouldRun) {
            LOG.warn(String.format("Skipping %s (compatible with tags: %s, should run:%s)", root, suitableForTask, shouldRun));
            continue;
        }
        try {
            testTask.setUp(testName);
            wasExecuted = true;
            if (isJython(root)) {
                testTask.useLongTimeout();
            } else {
                testTask.useNormalTimeout();
            }
            final String executable = getExecutable(root, testTask);
            if (executable == null) {
                throw new RuntimeException("Cannot find Python interpreter in " + root);
            }
            final Sdk sdk = createSdkByExecutable(executable);
            /**
         * Skipping test if {@link PyTestTask} reports it does not support this language level
         */
            final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(sdk);
            if (testTask.isLanguageLevelSupported(languageLevel)) {
                testTask.runTestOn(executable);
                passedRoots.add(root);
            } else {
                LOG.warn(String.format("Skipping root %s", root));
            }
        } catch (final Throwable e) {
            // Exception is thrown anyway, so we escape message before logging
            if (e.getMessage().contains("enteredTheMatrix")) {
                // .error( may lead to new exception with out of stacktrace.
                LOG.warn(PyEnvTestCase.escapeTestMessage(e.getMessage()));
            } else {
                LOG.error(e);
            }
            throw new RuntimeException(PyEnvTestCase.joinStrings(passedRoots, "Tests passed environments: ") + "Test failed on " + getEnvType() + " environment " + root, e);
        } finally {
            try {
                testTask.tearDown();
            } catch (Exception e) {
                throw new RuntimeException("Couldn't tear down task", e);
            }
        }
    }
    if (!wasExecuted) {
        throw new RuntimeException("test" + testName + " was not executed.\n" + PyEnvTestCase.joinStrings(myRoots, "All roots: ") + "\n" + PyEnvTestCase.joinStrings(testTask.getTags(), "Required tags in tags.txt in root: "));
    }
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) Sdk(com.intellij.openapi.projectRoots.Sdk) IOException(java.io.IOException) InvalidSdkException(com.jetbrains.python.sdk.InvalidSdkException)

Example 13 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PythonMockSdk method create.

public static Sdk create(final String version, @NotNull final VirtualFile... additionalRoots) {
    final String mock_path = PythonTestUtil.getTestDataPath() + "/MockSdk" + version + "/";
    String sdkHome = new File(mock_path, "bin/python" + version).getPath();
    SdkType sdkType = PythonSdkType.getInstance();
    final Sdk sdk = new ProjectJdkImpl(MOCK_SDK_NAME + " " + version, sdkType) {

        @Override
        public String getVersionString() {
            return "Python " + version + " Mock SDK";
        }
    };
    final SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.setHomePath(sdkHome);
    File libPath = new File(mock_path, "Lib");
    if (libPath.exists()) {
        sdkModificator.addRoot(LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libPath), OrderRootType.CLASSES);
    }
    sdkModificator.addRoot(PyUserSkeletonsUtil.getUserSkeletonsDirectory(), OrderRootType.CLASSES);
    final LanguageLevel level = LanguageLevel.fromPythonVersion(version);
    final VirtualFile typeShedDir = PyTypeShed.INSTANCE.getDirectory();
    PyTypeShed.INSTANCE.findRootsForLanguageLevel(level).forEach(path -> {
        final VirtualFile file = typeShedDir.findFileByRelativePath(path);
        if (file != null) {
            sdkModificator.addRoot(file, OrderRootType.CLASSES);
        }
    });
    String mock_stubs_path = mock_path + PythonSdkType.SKELETON_DIR_NAME;
    sdkModificator.addRoot(LocalFileSystem.getInstance().refreshAndFindFileByPath(mock_stubs_path), PythonSdkType.BUILTIN_ROOT_TYPE);
    for (final VirtualFile root : additionalRoots) {
        sdkModificator.addRoot(root, OrderRootType.CLASSES);
    }
    sdkModificator.commitChanges();
    final FileBasedIndex index = FileBasedIndex.getInstance();
    index.requestRebuild(StubUpdatingIndex.INDEX_ID);
    index.requestRebuild(PyModuleNameIndex.NAME);
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PythonSdkType(com.jetbrains.python.sdk.PythonSdkType) SdkType(com.intellij.openapi.projectRoots.SdkType) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 14 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PythonParsingTest method doTest.

public void doTest(LanguageLevel languageLevel) {
    LanguageLevel prev = myLanguageLevel;
    myLanguageLevel = languageLevel;
    try {
        doTest(true);
    } finally {
        myLanguageLevel = prev;
    }
    ensureEachFunctionHasStatementList(myFile, PyFunction.class);
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel)

Aggregations

LanguageLevel (com.jetbrains.python.psi.LanguageLevel)14 Sdk (com.intellij.openapi.projectRoots.Sdk)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)2 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)2 PythonSdkFlavor (com.jetbrains.python.sdk.flavors.PythonSdkFlavor)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 Application (com.intellij.openapi.application.Application)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 Project (com.intellij.openapi.project.Project)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 SdkType (com.intellij.openapi.projectRoots.SdkType)1 PsiComment (com.intellij.psi.PsiComment)1 PsiElement (com.intellij.psi.PsiElement)1