Search in sources :

Example 1 with PythonSdkAdditionalData

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

the class PySdkFlavorTest method createMockSdk.

// TODO: Add tests for MayaPy and IronPython SDK flavors
@NotNull
private static Sdk createMockSdk(@NotNull PythonSdkFlavor flavor, @NotNull String versionOutput) {
    final String versionString = flavor.getVersionStringFromOutput(versionOutput);
    final ProjectJdkImpl sdk = new ProjectJdkImpl("Test", PythonSdkType.getInstance(), "/path/to/sdk", versionString);
    sdk.setSdkAdditionalData(new PythonSdkAdditionalData(flavor));
    return sdk;
}
Also used : PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PythonSdkAdditionalData

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

the class PythonCommandLineState method getAddedPaths.

public static List<String> getAddedPaths(Sdk pythonSdk) {
    List<String> pathList = new ArrayList<>();
    final SdkAdditionalData sdkAdditionalData = pythonSdk.getSdkAdditionalData();
    if (sdkAdditionalData instanceof PythonSdkAdditionalData) {
        final Set<VirtualFile> addedPaths = ((PythonSdkAdditionalData) sdkAdditionalData).getAddedPathFiles();
        for (VirtualFile file : addedPaths) {
            addToPythonPath(file, pathList);
        }
    }
    return pathList;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData)

Example 3 with PythonSdkAdditionalData

use of com.jetbrains.python.sdk.PythonSdkAdditionalData 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 4 with PythonSdkAdditionalData

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

the class PyTestSdkTools method generateTempSkeletonsOrPackages.

/**
   * Adds installed eggs to SDK, generates skeletons (optionally) and associates it with module.
   *
   * @param sdk          sdk to process
   * @param addSkeletons add skeletons or only packages
   * @param module       module to associate with (if provided)
   * @throws InvalidSdkException bas sdk
   * @throws IOException         failed to read eggs
   */
public static void generateTempSkeletonsOrPackages(@NotNull final Sdk sdk, final boolean addSkeletons, @Nullable final Module module) throws InvalidSdkException, IOException {
    Project project = null;
    if (module != null) {
        project = module.getProject();
        final Project finalProject = project;
        // Associate with module
        ModuleRootModificationUtil.setModuleSdk(module, sdk);
        EdtTestUtil.runInEdtAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManager.getInstance(finalProject).setProjectSdk(sdk)));
    }
    final SdkModificator modificator = sdk.getSdkModificator();
    modificator.removeRoots(OrderRootType.CLASSES);
    modificator.setSdkAdditionalData(new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(sdk)));
    for (final String path : PythonSdkType.getSysPathsFromScript(sdk.getHomePath())) {
        addTestSdkRoot(modificator, path);
    }
    if (!addSkeletons) {
        EdtTestUtil.runInEdtAndWait(modificator::commitChanges);
        return;
    }
    final String skeletonsPath = PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath());
    addTestSdkRoot(modificator, skeletonsPath);
    EdtTestUtil.runInEdtAndWait(modificator::commitChanges);
    PySkeletonRefresher.refreshSkeletonsOfSdk(project, null, skeletonsPath, sdk);
}
Also used : Project(com.intellij.openapi.project.Project) PythonSdkAdditionalData(com.jetbrains.python.sdk.PythonSdkAdditionalData) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 5 with PythonSdkAdditionalData

use of com.jetbrains.python.sdk.PythonSdkAdditionalData 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

PythonSdkAdditionalData (com.jetbrains.python.sdk.PythonSdkAdditionalData)6 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 Project (com.intellij.openapi.project.Project)2 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PyDetectedSdk (com.jetbrains.python.sdk.PyDetectedSdk)2 ExecutionException (com.intellij.execution.ExecutionException)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 ProjectSdksModel (com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel)1 VirtualFileFilter (com.intellij.openapi.vfs.VirtualFileFilter)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 PreferredSdkComparator (com.jetbrains.python.sdk.PreferredSdkComparator)1 PythonSdkType (com.jetbrains.python.sdk.PythonSdkType)1 File (java.io.File)1