<commit_before>/*
 * Author: atotic
 * Created on Mar 23, 2004
 * License: Common Public License v1.0
 */
package com.jetbrains.python.debugger.pydev;

import com.google.common.collect.Lists;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.debugger.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class RemoteDebugger {

  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.pydev.remote.RemoteDebugger");

  private static final String LOCAL_VERSION = "0.1";
  public static final String TEMP_VAR_PREFIX = "__py_debug_temp_var_";

  private static final SecureRandom ourRandom = new SecureRandom();

  private final IPyDebugProcess myDebugProcess;
  private final ServerSocket myServerSocket;
  private final int myTimeout;
  private Socket mySocket;
  private volatile boolean myConnected = false;
  private int mySequence = -1;
  private final Map<String, PyThreadInfo> myThreads = new ConcurrentHashMap<String, PyThreadInfo>();
  private final Map<Integer, ProtocolFrame> myResponseQueue = new HashMap<Integer, ProtocolFrame>();
  private final TempVarsHolder myTempVars = new TempVarsHolder();

  private final List<RemoteDebuggerCloseListener> myCloseListeners = Lists.newArrayList();

  public RemoteDebugger(final IPyDebugProcess debugProcess, final ServerSocket serverSocket, final int timeout) {
    myDebugProcess = debugProcess;
    myServerSocket = serverSocket;
    myTimeout = timeout * 1000;  // to milliseconds
  }

  public IPyDebugProcess getDebugProcess() {
    return myDebugProcess;
  }

  public boolean isConnected() {
    return myConnected;
  }

  public void waitForConnect() throws Exception {
    try {
      //noinspection SocketOpenedButNotSafelyClosed
      mySocket = myServerSocket.accept();
    }
    finally {
      myServerSocket.close();
    }

    try {
      final DebuggerReader reader = new DebuggerReader();
      ApplicationManager.getApplication().executeOnPooledThread(reader);
    }
    catch (Exception e) {
      mySocket.close();
      throw e;
    }

    myConnected = true;
  }

  public void disconnect() {
    myConnected = false;

    if (mySocket != null && !mySocket.isClosed()) {
      try {
        mySocket.close();
      }
      catch (IOException ignore) {
      }
      mySocket = null;
    }
  }

  public String handshake() throws PyDebuggerException {
    final VersionCommand command = new VersionCommand(this, LOCAL_VERSION);
    command.execute();
    return command.getRemoteVersion();
  }

  public PyDebugValue evaluate(final String threadId,
                               final String frameId,
                               final String expression, final boolean execute) throws PyDebuggerException {
    return evaluate(threadId, frameId, expression, execute, true);
  }


  public PyDebugValue evaluate(final String threadId,
                               final String frameId,
                               final String expression,
                               final boolean execute,
                               boolean trimResult)
    throws PyDebuggerException {
    final EvaluateCommand command = new EvaluateCommand(this, threadId, frameId, expression, execute, trimResult);
    command.execute();
    return command.getValue();
  }

  public String consoleExec(String threadId, String frameId, String expression) throws PyDebuggerException {
    final ConsoleExecCommand command = new ConsoleExecCommand(this, threadId, frameId, expression);
    command.execute();
    return command.getValue();
  }

  public List<PyDebugValue> loadFrame(final String threadId, final String frameId) throws PyDebuggerException {
    final GetFrameCommand command = new GetFrameCommand(this, threadId, frameId);
    command.execute();
    return command.getVariables();
  }

  // todo: don't generate temp variables for qualified expressions - just split 'em
  public List<PyDebugValue> loadVariable(final String threadId, final String frameId, final PyDebugValue var) throws PyDebuggerException {
    setTempVariable(threadId, frameId, var);
    final GetVariableCommand command = new GetVariableCommand(this, threadId, frameId, composeName(var), var);
    command.execute();
    return command.getVariables();
  }

  public PyDebugValue changeVariable(final String threadId, final String frameId, final PyDebugValue var, final String value)
    throws PyDebuggerException {
    setTempVariable(threadId, frameId, var);
    return doChangeVariable(threadId, frameId, var.getEvaluationExpression(), value);
  }

  private PyDebugValue doChangeVariable(final String threadId, final String frameId, final String varName, final String value)
    throws PyDebuggerException {
    final ChangeVariableCommand command = new ChangeVariableCommand(this, threadId, frameId, varName, value);
    command.execute();
    return command.getNewValue();
  }

  @Nullable
  public String loadSource(String path) {
    LoadSourceCommand command = new LoadSourceCommand(this, path);
    try {
      command.execute();
      return command.getContent();
    }
    catch (PyDebuggerException e) {
      return "print 1";
    }
  }

  private static String composeName(final PyDebugValue var) {
    final StringBuilder sb = new StringBuilder(var.getTempName());
    PyDebugValue p = var;
    while ((p = p.getParent()) != null) {
      sb.insert(0, '\t').insert(0, p.getTempName());<commit_msg>small fixes in remote debugger<commit_after>/*
 * Author: atotic
 * Created on Mar 23, 2004
 * License: Common Public License v1.0
 */
package com.jetbrains.python.debugger.pydev;

import com.google.common.collect.Lists;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.debugger.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class RemoteDebugger {

  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.pydev.remote.RemoteDebugger");

  private static final String LOCAL_VERSION = "0.1";
  public static final String TEMP_VAR_PREFIX = "__py_debug_temp_var_";

  private static final SecureRandom ourRandom = new SecureRandom();

  private final IPyDebugProcess myDebugProcess;
  private final ServerSocket myServerSocket;
  private final int myTimeout;
  private Socket mySocket;
  private volatile boolean myConnected = false;
  private int mySequence = -1;
  private final Map<String, PyThreadInfo> myThreads = new ConcurrentHashMap<String, PyThreadInfo>();
  private final Map<Integer, ProtocolFrame> myResponseQueue = new HashMap<Integer, ProtocolFrame>();
  private final TempVarsHolder myTempVars = new TempVarsHolder();

  private final List<RemoteDebuggerCloseListener> myCloseListeners = Lists.newArrayList();

  public RemoteDebugger(final IPyDebugProcess debugProcess, final ServerSocket serverSocket, final int timeout) {
    myDebugProcess = debugProcess;
    myServerSocket = serverSocket;
    myTimeout = timeout * 1000;  // to milliseconds
  }

  public IPyDebugProcess getDebugProcess() {
    return myDebugProcess;
  }

  public boolean isConnected() {
    return myConnected;
  }

  public void waitForConnect() throws Exception {
    try {
      //noinspection SocketOpenedButNotSafelyClosed
      mySocket = myServerSocket.accept();
    }
    finally {
      myServerSocket.close();
    }

    try {
      final DebuggerReader reader = new DebuggerReader();
      ApplicationManager.getApplication().executeOnPooledThread(reader);
    }
    catch (Exception e) {
      mySocket.close();
      throw e;
    }

    myConnected = true;
  }

  public void disconnect() {
    myConnected = false;

    if (mySocket != null && !mySocket.isClosed()) {
      try {
        mySocket.close();
      }
      catch (IOException ignore) {
      }
      mySocket = null;
    }
  }

  public String handshake() throws PyDebuggerException {
    final VersionCommand command = new VersionCommand(this, LOCAL_VERSION);
    command.execute();
    return command.getRemoteVersion();
  }

  public PyDebugValue evaluate(final String threadId,
                               final String frameId,
                               final String expression, final boolean execute) throws PyDebuggerException {
    return evaluate(threadId, frameId, expression, execute, true);
  }


  public PyDebugValue evaluate(final String threadId,
                               final String frameId,
                               final String expression,
                               final boolean execute,
                               boolean trimResult)
    throws PyDebuggerException {
    final EvaluateCommand command = new EvaluateCommand(this, threadId, frameId, expression, execute, trimResult);
    command.execute();
    return command.getValue();
  }

  public String consoleExec(String threadId, String frameId, String expression) throws PyDebuggerException {
    final ConsoleExecCommand command = new ConsoleExecCommand(this, threadId, frameId, expression);
    command.execute();
    return command.getValue();
  }

  public List<PyDebugValue> loadFrame(final String threadId, final String frameId) throws PyDebuggerException {
    final GetFrameCommand command = new GetFrameCommand(this, threadId, frameId);
    command.execute();
    return command.getVariables();
  }

  // todo: don't generate temp variables for qualified expressions - just split 'em
  public List<PyDebugValue> loadVariable(final String threadId, final String frameId, final PyDebugValue var) throws PyDebuggerException {
    setTempVariable(threadId, frameId, var);
    final GetVariableCommand command = new GetVariableCommand(this, threadId, frameId, composeName(var), var);
    command.execute();
    return command.getVariables();
  }

  public PyDebugValue changeVariable(final String threadId, final String frameId, final PyDebugValue var, final String value)
    throws PyDebuggerException {
    setTempVariable(threadId, frameId, var);
    return doChangeVariable(threadId, frameId, var.getEvaluationExpression(), value);
  }

  private PyDebugValue doChangeVariable(final String threadId, final String frameId, final String varName, final String value)
    throws PyDebuggerException {
    final ChangeVariableCommand command = new ChangeVariableCommand(this, threadId, frameId, varName, value);
    command.execute();
    return command.getNewValue();
  }

  @Nullable
  public String loadSource(String path) {
    LoadSourceCommand command = new LoadSourceCommand(this, path);
    try {
      command.execute();
      return command.getContent();
    }
    catch (PyDebuggerException e) {
      return "#Couldn't load source of file " + path;
    }
  }

  private static String composeName(final PyDebugValue var) {
    final StringBuilder sb = new StringBuilder(var.getTempName());
    PyDebugValue p = var;
    while ((p = p.getParent()) != null) {
      sb.insert(0, '\t').insert(0, p.getTempName());
{'fraction_non_alphanumeric': 0.08463043293357149, 'fraction_numerical': 0.002976949902185932, 'mean_word_length': 3.7277844792923203, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251253', 'n_tokens_mistral': 3141, 'n_tokens_neox': 3073, 'n_words': 883}