Search in sources :

Example 1 with PyDetectedSdk

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

the class PyConfigurableInterpreterList method addDetectedSdks.

private void addDetectedSdks(@NotNull final List<Sdk> result) {
    final List<String> sdkHomes = new ArrayList<>();
    sdkHomes.addAll(VirtualEnvSdkFlavor.INSTANCE.suggestHomePaths());
    for (PythonSdkFlavor flavor : PythonSdkFlavor.getApplicableFlavors()) {
        if (flavor instanceof VirtualEnvSdkFlavor)
            continue;
        sdkHomes.addAll(flavor.suggestHomePaths());
    }
    Collections.sort(sdkHomes);
    for (String sdkHome : SdkConfigurationUtil.filterExistingPaths(PythonSdkType.getInstance(), sdkHomes, getModel().getSdks())) {
        result.add(new PyDetectedSdk(sdkHome));
    }
}
Also used : PythonSdkFlavor(com.jetbrains.python.sdk.flavors.PythonSdkFlavor) ArrayList(java.util.ArrayList) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) VirtualEnvSdkFlavor(com.jetbrains.python.sdk.flavors.VirtualEnvSdkFlavor)

Example 2 with PyDetectedSdk

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

the class PythonGenerateProjectCallback method consume.

@Override
public void consume(@Nullable ProjectSettingsStepBase step) {
    if (!(step instanceof ProjectSpecificSettingsStep))
        return;
    final ProjectSpecificSettingsStep settingsStep = (ProjectSpecificSettingsStep) step;
    final DirectoryProjectGenerator generator = settingsStep.getProjectGenerator();
    Sdk sdk = settingsStep.getSdk();
    if (sdk instanceof PyDetectedSdk) {
        sdk = addDetectedSdk(settingsStep, sdk);
    }
    if (generator instanceof PythonProjectGenerator) {
        final BooleanFunction<PythonProjectGenerator> beforeProjectGenerated = ((PythonProjectGenerator) generator).beforeProjectGenerated(sdk);
        if (beforeProjectGenerated != null) {
            final boolean result = beforeProjectGenerated.fun((PythonProjectGenerator) generator);
            if (!result) {
                Messages.showWarningDialog("Project can not be generated", "Error in Project Generation");
                return;
            }
        }
    }
    final Project newProject = generateProject(settingsStep);
    if (generator instanceof PythonProjectGenerator && sdk == null && newProject != null) {
        final PyNewProjectSettings settings = (PyNewProjectSettings) ((PythonProjectGenerator) generator).getProjectSettings();
        ((PythonProjectGenerator) generator).createAndAddVirtualEnv(newProject, settings);
        sdk = settings.getSdk();
    }
    if (newProject != null && generator instanceof PythonProjectGenerator) {
        SdkConfigurationUtil.setDirectoryProjectSdk(newProject, sdk);
        final List<Sdk> sdks = PythonSdkType.getAllSdks();
        for (Sdk s : sdks) {
            final SdkAdditionalData additionalData = s.getSdkAdditionalData();
            if (additionalData instanceof PythonSdkAdditionalData) {
                ((PythonSdkAdditionalData) additionalData).reassociateWithCreatedProject(newProject);
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PyNewProjectSettings(com.jetbrains.python.newProject.PyNewProjectSettings) PythonProjectGenerator(com.jetbrains.python.newProject.PythonProjectGenerator) DirectoryProjectGenerator(com.intellij.platform.DirectoryProjectGenerator) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Example 3 with PyDetectedSdk

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

the class PyStudyDirectoryProjectGenerator method createAndAddVirtualEnv.

public void createAndAddVirtualEnv(Project project, PyNewProjectSettings settings) {
    final ProjectSdksModel model = PyConfigurableInterpreterList.getInstance(project).getModel();
    final String baseSdk = getBaseSdk(project);
    if (baseSdk != null) {
        final PyPackageManager packageManager = PyPackageManager.getInstance(new PyDetectedSdk(baseSdk));
        try {
            final String path = packageManager.createVirtualEnv(project.getBasePath() + "/.idea/VirtualEnvironment", false);
            AbstractCreateVirtualEnvDialog.setupVirtualEnvSdk(path, true, new AbstractCreateVirtualEnvDialog.VirtualEnvCallback() {

                @Override
                public void virtualEnvCreated(Sdk createdSdk, boolean associateWithProject) {
                    settings.setSdk(createdSdk);
                    model.addSdk(createdSdk);
                    try {
                        model.apply();
                    } catch (ConfigurationException exception) {
                        LOG.error("Error adding created virtual env " + exception.getMessage());
                    }
                    if (associateWithProject) {
                        SdkAdditionalData additionalData = createdSdk.getSdkAdditionalData();
                        if (additionalData == null) {
                            additionalData = new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(createdSdk.getHomePath()));
                            ((ProjectJdkImpl) createdSdk).setSdkAdditionalData(additionalData);
                        }
                        ((PythonSdkAdditionalData) additionalData).associateWithNewProject();
                    }
                }
            });
        } catch (ExecutionException e) {
            LOG.warn("Failed to create virtual env " + e.getMessage());
        }
    }
}
Also used : ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) AbstractCreateVirtualEnvDialog(com.jetbrains.python.sdk.AbstractCreateVirtualEnvDialog) PyPackageManager(com.jetbrains.python.packaging.PyPackageManager) ConfigurationException(com.intellij.openapi.options.ConfigurationException) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) PyDetectedSdk(com.jetbrains.python.sdk.PyDetectedSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Aggregations

PyDetectedSdk (com.jetbrains.python.sdk.PyDetectedSdk)3 Sdk (com.intellij.openapi.projectRoots.Sdk)2 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)2 PythonSdkAdditionalData (com.jetbrains.python.sdk.PythonSdkAdditionalData)2 ExecutionException (com.intellij.execution.ExecutionException)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 Project (com.intellij.openapi.project.Project)1 ProjectSdksModel (com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel)1 DirectoryProjectGenerator (com.intellij.platform.DirectoryProjectGenerator)1 PyNewProjectSettings (com.jetbrains.python.newProject.PyNewProjectSettings)1 PythonProjectGenerator (com.jetbrains.python.newProject.PythonProjectGenerator)1 PyPackageManager (com.jetbrains.python.packaging.PyPackageManager)1 AbstractCreateVirtualEnvDialog (com.jetbrains.python.sdk.AbstractCreateVirtualEnvDialog)1 PythonSdkFlavor (com.jetbrains.python.sdk.flavors.PythonSdkFlavor)1 VirtualEnvSdkFlavor (com.jetbrains.python.sdk.flavors.VirtualEnvSdkFlavor)1 ArrayList (java.util.ArrayList)1