protected GeneralCommandLine createCommandLine(Module module, List<String> params)
      throws ExecutionException {
    GeneralCommandLine cmd = new GeneralCommandLine();

    Sdk sdk = PythonSdkType.findPythonSdk(module);
    if (sdk == null) {
      throw new ExecutionException("No sdk specified");
    }

    ReSTService service = ReSTService.getInstance(module);
    cmd.setWorkDirectory(
        service.getWorkdir().isEmpty()
            ? module.getProject().getBaseDir().getPath()
            : service.getWorkdir());
    PythonCommandLineState.createStandardGroupsIn(cmd);
    ParamsGroup script_params =
        cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
    assert script_params != null;

    String commandPath = getCommandPath(sdk);
    if (commandPath == null) {
      throw new ExecutionException("Cannot find sphinx-quickstart.");
    }
    cmd.setExePath(commandPath);

    if (params != null) {
      for (String p : params) {
        script_params.addParameter(p);
      }
    }

    cmd.setPassParentEnvironment(true);
    setPythonIOEncoding(cmd.getEnvironment(), "utf-8");
    setPythonUnbuffered(cmd.getEnvironment());

    List<String> pathList = Lists.newArrayList(PythonCommandLineState.getAddedPaths(sdk));
    pathList.addAll(PythonCommandLineState.collectPythonPath(module));

    PythonCommandLineState.initPythonPath(cmd, true, pathList, sdk.getHomePath());

    PythonSdkType.patchCommandLineForVirtualenv(cmd, sdk.getHomePath(), true);
    BuildoutFacet facet = BuildoutFacet.getInstance(module);
    if (facet != null) {
      facet.patchCommandLineForBuildout(cmd);
    }

    return cmd;
  }
  public static Map<String, String> addDefaultEnvironments(Sdk sdk, Map<String, String> envs) {
    Charset defaultCharset = EncodingManager.getInstance().getDefaultCharset();

    final String encoding = defaultCharset != null ? defaultCharset.name() : "utf-8";
    setPythonIOEncoding(setPythonUnbuffered(envs), encoding);

    PythonSdkFlavor.initPythonPath(envs, true, PythonCommandLineState.getAddedPaths(sdk));
    return envs;
  }
  private XDebugSession connectToDebugger() throws ExecutionException {
    final ServerSocket serverSocket = PythonCommandLineState.createServerSocket();

    final XDebugSession session =
        XDebuggerManager.getInstance(getProject())
            .startSessionAndShowTab(
                "Python Console Debugger",
                PythonIcons.Python.Python,
                null,
                true,
                new XDebugProcessStarter() {
                  @NotNull
                  public XDebugProcess start(@NotNull final XDebugSession session) {
                    PythonDebugLanguageConsoleView debugConsoleView =
                        new PythonDebugLanguageConsoleView(getProject(), mySdk);

                    PyConsoleDebugProcessHandler consoleDebugProcessHandler =
                        new PyConsoleDebugProcessHandler(myProcessHandler);

                    PyConsoleDebugProcess consoleDebugProcess =
                        new PyConsoleDebugProcess(
                            session, serverSocket, debugConsoleView, consoleDebugProcessHandler);

                    PythonDebugConsoleCommunication communication =
                        PyDebugRunner.initDebugConsoleView(
                            getProject(),
                            consoleDebugProcess,
                            debugConsoleView,
                            consoleDebugProcessHandler);

                    myPydevConsoleCommunication.setDebugCommunication(communication);
                    debugConsoleView.attachToProcess(consoleDebugProcessHandler);

                    consoleDebugProcess.waitForNextConnection();

                    try {
                      consoleDebugProcess.connect(myPydevConsoleCommunication);
                    } catch (Exception e) {
                      LOG.error(e); // TODO
                    }

                    myProcessHandler.notifyTextAvailable(
                        "\nDebugger connected.\n", ProcessOutputTypes.STDERR);

                    return consoleDebugProcess;
                  }
                });

    return session;
  }