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();
}
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: "));
}
}
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;
}
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);
}
Aggregations