<commit_before>import os
import signal
import socket
import subprocess
import re

class Util:
    @staticmethod
    def read_file(path):
        f = open(path)
        result = f.read()
        f.close()
        return result

    @staticmethod
    def write_file(path, contents):
        f = open(path, "w")
        result = f.write(contents)
        f.close()
        return result

    @staticmethod
    def mkdir_p(path):
        if not os.path.exists(path):
            os.makedirs(path)

class EnsimeProcess:
    def __init__(self, cache_dir, process, log_path, cleanup):
        self.log_path = log_path
        self.cache_dir = cache_dir
        self.process = process
        self.__stopped_manually = False
        self.__cleanup = cleanup

    def stop(self):
        os.kill(self.process.pid, signal.SIGTERM)
        self.__cleanup()
        self.__stopped_manually = True

    def aborted(self):
        return not (self.__stopped_manually or self.is_running())

    def is_running(self):
        return self.process.poll() == None

    def is_ready(self):
        if not self.is_running():
            return False
        try:
            port = self.http_port()
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(("127.0.0.1", port))
            s.close()
            return True
        except:
            return False

    def http_port(self):
        return int(Util.read_file(os.path.join(self.cache_dir, "http")))

class EnsimeLauncher:
    def __init__(self, vim, base_dir =  "/tmp/classpath_project_ensime"):
        self.vim = vim
        self.base_dir = os.path.abspath(base_dir)
        self.ensime_version = "0.9.10-SNAPSHOT"
        self.sbt_version = "0.13.8"

    def launch(self, conf_path):
        conf = self.parse_conf(conf_path)
        classpath = self.load_classpath(conf['scala-version'], conf['java-home'])
        return self.start_process(<commit_msg>do not start ensime server is it is already running externally<commit_after>import os
import signal
import socket
import subprocess
import re

class Util:
    @staticmethod
    def read_file(path):
        f = open(path)
        result = f.read()
        f.close()
        return result

    @staticmethod
    def write_file(path, contents):
        f = open(path, "w")
        result = f.write(contents)
        f.close()
        return result

    @staticmethod
    def mkdir_p(path):
        if not os.path.exists(path):
            os.makedirs(path)

class EnsimeProcess:
    def __init__(self, cache_dir, process, log_path, cleanup):
        self.log_path = log_path
        self.cache_dir = cache_dir
        self.process = process
        self.__stopped_manually = False
        self.__cleanup = cleanup

    def stop(self):
        if self.process is None: return
        os.kill(self.process.pid, signal.SIGTERM)
        self.__cleanup()
        self.__stopped_manually = True

    def aborted(self):
        return not (self.__stopped_manually or self.is_running())

    def is_running(self):
        return self.process is None or self.process.poll() == None

    def is_ready(self):
        if not self.is_running():
            return False
        try:
            port = self.http_port()
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(("127.0.0.1", port))
            s.close()
            return True
        except:
            return False

    def http_port(self):
        return int(Util.read_file(os.path.join(self.cache_dir, "http")))

class EnsimeLauncher:
    def __init__(self, vim, base_dir =  "/tmp/classpath_project_ensime"):
        self.vim = vim
        self.base_dir = os.path.abspath(base_dir)
        self.ensime_version = "0.9.10-SNAPSHOT"
        self.sbt_version = "0.13.8"

    def launch(self, conf_path):
        conf = self.parse_conf(conf_path)
        process = EnsimeProcess(conf['cache-dir'], None, None, lambda: None)
        if process.is_ready():
            return process

        classpath = self.load_classpath(conf['scala-version'], conf['java-home'])
        return self.start_process(
{'fraction_non_alphanumeric': 0.10484067136949647, 'fraction_numerical': 0.006810994891753831, 'mean_word_length': 2.202492211838006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1046905', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1269, 'n_words': 286}