PY-20733: Guessing best place for remote project using helper

* We use user homedir + project name
* Utility method added to PyTask to launch it with out of console
This commit is contained in:
Ilya.Kazakevich
2016-09-15 17:45:38 +03:00
parent 3e89f97a14
commit 301e5a6357
4 changed files with 60 additions and 5 deletions
@@ -0,0 +1,14 @@
# First arg is project name. It searches for best place for project
import os, sys
project_name = sys.argv[1]
current_folder = os.getcwd()
while True:
project_folder = os.path.join(current_folder, project_name)
if not os.path.exists(project_name):
print project_folder
break
else:
project_name += "_"
@@ -67,7 +67,7 @@ public abstract class PythonProjectGenerator<T extends PyNewProjectSettings> imp
final PythonRemoteInterpreterManager remoteManager = PythonRemoteInterpreterManager.getInstance();
final Sdk sdk = (settings != null ? settings.getSdk() : null);
if (remoteManager != null && PythonSdkType.isRemote(sdk)) {
remoteManager.prepareRemoteSettingsIfNeeded(project, sdk, configureProject);
remoteManager.prepareRemoteSettingsIfNeeded(module, sdk, configureProject);
}
else {
configureProject.run();
@@ -22,6 +22,7 @@ import com.intellij.execution.configurations.ParamsGroup;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessOutput;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkAdditionalData;
@@ -140,7 +141,7 @@ public abstract class PythonRemoteInterpreterManager {
*
* @param callAfterCheck what to call after configuration completes (runs on AWT)
*/
public abstract void prepareRemoteSettingsIfNeeded(@NotNull final Project project,
public abstract void prepareRemoteSettingsIfNeeded(@NotNull final Module module,
@NotNull final Sdk sdk,
@NotNull final Runnable callAfterCheck);
@@ -17,6 +17,8 @@ package com.jetbrains.python.run;
import com.google.common.collect.Lists;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Output;
import com.intellij.execution.OutputListener;
import com.intellij.execution.RunContentExecutor;
import com.intellij.execution.configurations.EncodingEnvironmentUtil;
import com.intellij.execution.configurations.GeneralCommandLine;
@@ -39,9 +41,11 @@ import com.jetbrains.python.console.PydevConsoleRunner;
import com.jetbrains.python.sdk.PySdkUtil;
import com.jetbrains.python.sdk.PythonEnvUtil;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -71,11 +75,24 @@ public class PythonTask {
this(module, runTabTitle, PythonSdkType.findPythonSdk(module));
}
@NotNull
public static PythonTask create(@NotNull final Module module,
@NotNull final String runTabTitle,
@NotNull final Sdk sdk) {
// Ctor throws checked exception which is not good, so this wrapper saves user from dumb code
try {
return new PythonTask(module, runTabTitle, sdk);
}
catch (final ExecutionException ignored) {
throw new AssertionError("Exception thrown file should not be");
}
}
public PythonTask(final Module module, final String runTabTitle, @Nullable final Sdk sdk) throws ExecutionException {
myModule = module;
myRunTabTitle = runTabTitle;
mySdk = sdk;
if (mySdk == null) {
if (mySdk == null) { // TODO: Get rid of such a weird contract
throw new ExecutionException("Cannot find Python interpreter for selected module");
}
}
@@ -167,11 +184,11 @@ public class PythonTask {
NotNullFunction<String, String> escaperFunction = StringUtil.escaper(false, "|>$\"'& ");
StringBuilder paramString;
if (myHelper != null) {
paramString= new StringBuilder(escaperFunction.fun(homePath) + " " + escaperFunction.fun(myHelper.asParamString()));
paramString = new StringBuilder(escaperFunction.fun(homePath) + " " + escaperFunction.fun(myHelper.asParamString()));
myHelper.addToPythonPath(cmd.getEnvironment());
}
else {
paramString= new StringBuilder(escaperFunction.fun(homePath) + " " + escaperFunction.fun(myRunnerScript));
paramString = new StringBuilder(escaperFunction.fun(homePath) + " " + escaperFunction.fun(myRunnerScript));
}
for (String p : myParameters) {
paramString.append(" ").append(p);
@@ -246,4 +263,27 @@ public class PythonTask {
.withHelpId(myHelpId)
.run();
}
/**
* Runs task with out console
* @return stdout
* @throws ExecutionException in case of error. Consider using {@link com.intellij.execution.util.ExecutionErrorDialog}
*/
@NotNull //TODO: DOC, ExecutionErrorDialog
public final String runNoConsole() throws ExecutionException {
final ProcessHandler process = createProcess(new HashMap<>());
final OutputListener listener = new OutputListener();
process.addProcessListener(listener);
process.startNotify();
process.waitFor();
final Output output = listener.getOutput();
final int exitCode = output.getExitCode();
if (exitCode == 0) {
return output.getStdout();
}
throw new ExecutionException(String.format("Error on python side. " +
"Exit code: %s, err: %s out: %s", exitCode, output.getStderr(), output.getStdout()));
}
}