Search in sources :

Example 11 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class SimpleRunner method makePythonPathEnvString.

/**
 * Creates a string that can be passed as the PYTHONPATH
 *
 * @param project the project we want to get the settings from. If it is null, the system pythonpath is returned
 * @param interpreter this is the interpreter to be used to create the env.
 * @return a string that can be used as the PYTHONPATH env variable
 */
public static String makePythonPathEnvString(IPythonNature pythonNature, IInterpreterInfo interpreter, IInterpreterManager manager) {
    if (pythonNature == null) {
        if (interpreter == null) {
            // no pythonpath can be gotten (set to empty, so that the default is gotten)
            return makePythonPathEnvFromPaths(new ArrayList<String>());
        } else {
            List<String> pythonPath = interpreter.getPythonPath();
            return makePythonPathEnvFromPaths(pythonPath);
        }
    }
    List<String> paths;
    // if we have a project, get its complete pythonpath
    IPythonPathNature pythonPathNature = pythonNature.getPythonPathNature();
    if (pythonPathNature == null) {
        IProject project = pythonNature.getProject();
        String projectName;
        if (project == null) {
            projectName = "null?";
        } else {
            projectName = project.getName();
        }
        throw new RuntimeException("The project " + projectName + " does not have the pythonpath configured, \n" + "please configure it correcly (please check the pydev getting started guide at \n" + "http://pydev.org/manual_101_root.html for better information on how to do it).");
    }
    paths = pythonPathNature.getCompleteProjectPythonPath(interpreter, manager);
    return makePythonPathEnvFromPaths(paths);
}
Also used : IPythonPathNature(org.python.pydev.core.IPythonPathNature) IProject(org.eclipse.core.resources.IProject)

Example 12 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class AbstractAppEngineHandler method executeInObject.

public Object executeInObject(Object firstElement) {
    IContainer container = CustomizationCommons.getContainerFromObject(firstElement);
    if (container == null) {
        return null;
    }
    IPythonPathNature pythonPathNature = CustomizationCommons.getPythonPathNatureFromObject(firstElement);
    if (pythonPathNature == null) {
        return null;
    }
    Map<String, String> variableSubstitution;
    try {
        variableSubstitution = pythonPathNature.getVariableSubstitution();
        // Only consider a google app engine a project that has a google app engine variable!
        if (variableSubstitution == null || !variableSubstitution.containsKey(AppEngineConstants.GOOGLE_APP_ENGINE_VARIABLE)) {
            return null;
        }
        File appEngineLocation = new File(variableSubstitution.get(AppEngineConstants.GOOGLE_APP_ENGINE_VARIABLE));
        if (!appEngineLocation.isDirectory()) {
            MessageDialog.openError(EditorUtils.getShell(), "Error", "Expected: " + appEngineLocation + " to be a directory.");
            return null;
        }
        File appcfg = new File(appEngineLocation, "appcfg.py");
        if (!appcfg.isFile()) {
            MessageDialog.openError(EditorUtils.getShell(), "Error", "Expected: " + appcfg + " to be a file.");
            return null;
        }
        handleExecution(container, pythonPathNature, appcfg, appEngineLocation);
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : IPythonPathNature(org.python.pydev.core.IPythonPathNature) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 13 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class AppEnginePropertyTester method test.

/**
 * Expected value is ignored.
 *
 * Considers as available for the run a container of a project with the GOOGLE_APP_ENGINE variable
 * declared in it and has a app.yaml or app.yml under it.
 */
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    IContainer container = CustomizationCommons.getContainerFromObject(receiver);
    if (container == null) {
        return false;
    }
    IPythonPathNature nature = CustomizationCommons.getPythonPathNatureFromObject(receiver);
    if (nature == null) {
        return false;
    }
    // dev_appserver.py [options] <application root>
    // 
    // Application root must be the path to the application to run in this server.
    // Must contain a valid app.yaml or app.yml file.
    IFile file = container.getFile(new Path("app.yaml"));
    if (file == null || !file.exists()) {
        file = container.getFile(new Path("app.yml"));
        if (file == null || !file.exists()) {
            return false;
        }
    }
    try {
        Map<String, String> variableSubstitution = nature.getVariableSubstitution();
        // Only consider a google app engine a project that has a google app engine variable!
        if (variableSubstitution != null && variableSubstitution.containsKey(AppEngineConstants.GOOGLE_APP_ENGINE_VARIABLE)) {
            return true;
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return false;
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IContainer(org.eclipse.core.resources.IContainer)

Example 14 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class DjangoAction method launchDjangoCommand.

/**
 * May be used to run some command that uses the manage.py file.
 */
@SuppressWarnings("restriction")
public ILaunch launchDjangoCommand(final String command, boolean refreshAndShowMessageOnFinish) {
    PythonNature nature = PythonNature.getPythonNature(selectedProject);
    if (nature == null) {
        MessageDialog.openError(EditorUtils.getShell(), "PyDev nature not found", "Unable to perform action because the Pydev nature is not properly set.");
        return null;
    }
    IPythonPathNature pythonPathNature = nature.getPythonPathNature();
    String manageVarible = null;
    Map<String, String> variableSubstitution = null;
    try {
        variableSubstitution = pythonPathNature.getVariableSubstitution();
        manageVarible = variableSubstitution.get(DjangoConstants.DJANGO_MANAGE_VARIABLE);
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }
    if (manageVarible == null) {
        manageVarible = askNewManageSubstitution(pythonPathNature, variableSubstitution, StringUtils.format("Unable to perform action because the %s \n" + "substitution variable is not set.\n\n" + "Please select the manage.py to be used to run the action.", DjangoConstants.DJANGO_MANAGE_VARIABLE));
        if (manageVarible == null) {
            return null;
        }
    }
    IFile manageDotPy = selectedProject.getFile(manageVarible);
    if (manageDotPy == null || !manageDotPy.exists()) {
        manageVarible = askNewManageSubstitution(pythonPathNature, variableSubstitution, StringUtils.format("Unable to perform action because the %s \n" + "substitution variable is set to a non existing file.\n\n" + "Please select the manage.py to be used to run the action.", DjangoConstants.DJANGO_MANAGE_VARIABLE));
        if (manageVarible == null) {
            return null;
        }
        // we shouldn't need to validate again (he can't choose a wrong file there right?)
        manageDotPy = selectedProject.getFile(manageVarible);
    }
    final IFile finalManageDotPy = manageDotPy;
    try {
        ILaunch launch = PythonFileRunner.launch(manageDotPy, command);
        // After the command completes, refresh and put message for user.
        final IProcess[] processes = launch.getProcesses();
        ProcessConsoleManager consoleManager = DebugUIPlugin.getDefault().getProcessConsoleManager();
        if (processes.length >= 1) {
            IConsole console = consoleManager.getConsole(processes[0]);
            final IOConsoleOutputStream outputStream = ((IOConsole) console).newOutputStream();
            HashMap<IOConsoleOutputStream, String> themeConsoleStreamToColor = new HashMap<IOConsoleOutputStream, String>();
            themeConsoleStreamToColor.put(outputStream, "console.output");
            ((IOConsole) console).setAttribute("themeConsoleStreamToColor", themeConsoleStreamToColor);
            ConsoleColorCache.getDefault().keepConsoleColorsSynched((IOConsole) console);
            Job j = new Job("Refresh on finish") {

                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    boolean allTerminated = false;
                    while (!allTerminated) {
                        allTerminated = true;
                        for (IProcess p : processes) {
                            if (!p.isTerminated()) {
                                allTerminated = false;
                                break;
                            }
                        }
                        synchronized (this) {
                            try {
                                this.wait(50);
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                    try {
                        outputStream.write(StringUtils.format("Finished \"" + finalManageDotPy.getLocation().toOSString() + " " + command + "\" execution."));
                    } catch (IOException e1) {
                        Log.log(e1);
                    }
                    try {
                        outputStream.close();
                    } catch (IOException e1) {
                        Log.log(e1);
                    }
                    try {
                        selectedProject.refreshLocal(IResource.DEPTH_INFINITE, null);
                    } catch (CoreException e) {
                        Log.log(e);
                    }
                    return Status.OK_STATUS;
                }
            };
            j.setSystem(true);
            j.schedule();
        }
        return launch;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) PythonNature(org.python.pydev.plugin.nature.PythonNature) ProcessConsoleManager(org.eclipse.debug.internal.ui.views.console.ProcessConsoleManager) IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) HashMap(java.util.HashMap) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IConsole(org.eclipse.ui.console.IConsole) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ILaunch(org.eclipse.debug.core.ILaunch) Job(org.eclipse.core.runtime.jobs.Job) IProcess(org.eclipse.debug.core.model.IProcess) IOConsole(org.eclipse.ui.console.IOConsole)

Example 15 with IPythonPathNature

use of org.python.pydev.core.IPythonPathNature in project Pydev by fabioz.

the class DjangoProjectProperties method createContents.

@Override
protected Control createContents(Composite parent) {
    project = getElement().getAdapter(IProject.class);
    Composite topComp = new Composite(parent, SWT.NONE);
    GridLayout innerLayout = new GridLayout();
    innerLayout.numColumns = 2;
    innerLayout.marginHeight = 0;
    innerLayout.marginWidth = 0;
    topComp.setLayout(innerLayout);
    GridData gd = new GridData(GridData.FILL_BOTH);
    topComp.setLayoutData(gd);
    if (project != null) {
        try {
            IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
            final PythonNature nature = PythonNature.getPythonNature(project);
            Map<String, String> variableSubstitution = pathNature.getVariableSubstitution(false);
            Label label = new Label(topComp, SWT.None);
            label.setText("Django manage.py");
            Text text = new Text(topComp, SWT.BORDER);
            textDjangoManage = text;
            textDjangoManage.setToolTipText("This is the name of the project-relative location of manage.py (i.e.: src/myapp/manage.py)");
            label = new Label(topComp, SWT.None);
            labelErrorManage = new Label(topComp, SWT.None);
            labelErrorManage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
            ModifyListener manageValidator = new ModifyListener() {

                @Override
                public void modifyText(ModifyEvent e) {
                    try {
                        String path = textDjangoManage.getText().trim();
                        if (path.trim().length() == 0) {
                            labelErrorSettings.setText("Please specify the manage.py relative name (i.e.: src/myapp/manage.py)");
                            return;
                        }
                        IFile file = project.getFile(new Path(path));
                        if (!file.exists()) {
                            labelErrorManage.setText(StringUtils.format("File: %s could not be found.", path));
                        } else {
                            labelErrorManage.setText("");
                        }
                    } catch (Exception e1) {
                        Log.log(e1);
                    }
                }
            };
            text.addModifyListener(manageValidator);
            text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
            String string = variableSubstitution.get(DjangoConstants.DJANGO_MANAGE_VARIABLE);
            if (string != null) {
                text.setText(string);
            } else {
                text.setText("");
            }
            // Settings
            label = new Label(topComp, SWT.None);
            label.setText("Django settings module");
            text = new Text(topComp, SWT.BORDER);
            textDjangoSettings = text;
            textDjangoSettings.setToolTipText("This is the name of the django settings module (i.e.: myapp.settings)");
            label = new Label(topComp, SWT.None);
            labelErrorSettings = new Label(topComp, SWT.None);
            labelErrorSettings.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
            ModifyListener settingsValidator = new ModifyListener() {

                @Override
                public void modifyText(ModifyEvent e) {
                    try {
                        String moduleName = textDjangoSettings.getText().trim();
                        if (moduleName.trim().length() == 0) {
                            labelErrorSettings.setText("Please specify the name of the module (i.e.: myapp.settings)");
                            return;
                        }
                        ICodeCompletionASTManager astManager = nature.getAstManager();
                        ProjectModulesManager modulesManager = (ProjectModulesManager) astManager.getModulesManager();
                        IModule moduleInDirectManager = modulesManager.getModuleInDirectManager(moduleName, nature, true, new BaseModuleRequest(false));
                        if (moduleInDirectManager == null) {
                            labelErrorSettings.setText(StringUtils.format("Module: %s could not be found.", moduleName));
                        } else {
                            labelErrorSettings.setText("");
                        }
                    } catch (Exception e1) {
                        Log.log(e1);
                    }
                }
            };
            text.addModifyListener(settingsValidator);
            text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
            string = variableSubstitution.get(DjangoConstants.DJANGO_SETTINGS_MODULE);
            if (string != null) {
                text.setText(string);
            } else {
                text.setText("");
            }
        } catch (Exception e) {
            Log.log(e);
        }
    } else {
        Label label = new Label(topComp, SWT.None);
        label.setText("Internal error: project not set!");
    }
    return topComp;
}
Also used : Path(org.eclipse.core.runtime.Path) IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) Composite(org.eclipse.swt.widgets.Composite) PythonNature(org.python.pydev.plugin.nature.PythonNature) ModifyListener(org.eclipse.swt.events.ModifyListener) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) IPythonPathNature(org.python.pydev.core.IPythonPathNature) Label(org.eclipse.swt.widgets.Label) Text(org.eclipse.swt.widgets.Text) ProjectModulesManager(org.python.pydev.ast.codecompletion.revisited.ProjectModulesManager) IProject(org.eclipse.core.resources.IProject) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) GridData(org.eclipse.swt.layout.GridData)

Aggregations

IPythonPathNature (org.python.pydev.core.IPythonPathNature)26 IProject (org.eclipse.core.resources.IProject)15 CoreException (org.eclipse.core.runtime.CoreException)10 PythonNature (org.python.pydev.plugin.nature.PythonNature)9 IPythonNature (org.python.pydev.core.IPythonNature)7 ArrayList (java.util.ArrayList)5 IFile (org.eclipse.core.resources.IFile)5 IFolder (org.eclipse.core.resources.IFolder)5 IResource (org.eclipse.core.resources.IResource)5 Path (org.eclipse.core.runtime.Path)5 File (java.io.File)3 IContainer (org.eclipse.core.resources.IContainer)3 IPath (org.eclipse.core.runtime.IPath)3 Composite (org.eclipse.swt.widgets.Composite)3 Label (org.eclipse.swt.widgets.Label)3 HashMap (java.util.HashMap)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Shell (org.eclipse.swt.widgets.Shell)2