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