<commit_before>#!/usr/bin/env python

# This code is GPLv3. See LICENSE file.

import time
import random
import subprocess
import logging
import rpyc
import os

l = logging.getLogger("idalink")
l.setLevel(logging.DEBUG)

# various locations
module_dir = os.path.dirname(os.path.realpath(__file__))
log_file = "/tmp/idalink.log"
ida_script = module_dir + "/run_ida.sh"
ida_dir = module_dir

print "Script: %s" % ida_script

import collections
IDA = collections.namedtuple("IDA", [ "link", "idc", "idaapi", "idautils" ])

def spawn_ida(filename, port):
	fullpath = os.path.realpath(os.path.expanduser(filename))
	l.info("Launching IDA on %s" % fullpath)
	subprocess.call([ "screen", "-d", "-m", "--", ida_script, ida_dir, fullpath, log_file, module_dir + "/server.py", str(port) ])

def connect_ida(port):
	ida = rpyc.classic.connect("localhost", port)

	idc = ida.root.getmodule("idc")
	idaapi = ida.root.getmodule("idaapi")
	idautils = ida.root.getmodule("idautils")

	return IDA(ida, idc, idaapi, idautils)

def make_idalink(filename):
	port = random.randint(40000, 49999)
	spawn_ida(filename, port)

	while True:
		try:
			time.sleep(1)
			l.debug("Trying to connect to IDA on port %d" % port)
			return connect_ida(port)
		except Exception:
			l.debug("... failed. Retrying.")<commit_msg>connect timeout, logging changes<commit_after>#!/usr/bin/env python

# This code is GPLv3. See LICENSE file.

import time
import random
import subprocess
import logging
import rpyc
import os

l = logging.getLogger("idalink")

# various locations
module_dir = os.path.dirname(os.path.realpath(__file__))
log_file = "/tmp/idalink.log"
ida_script = module_dir + "/run_ida.sh"
ida_dir = module_dir

l.info("IDA launch script: %s" % ida_script)

import collections
IDA = collections.namedtuple("IDA", [ "link", "idc", "idaapi", "idautils" ])

def spawn_ida(filename, port):
	fullpath = os.path.realpath(os.path.expanduser(filename))
	l.info("Launching IDA on %s" % fullpath)
	subprocess.call([ "screen", "-d", "-m", "--", ida_script, ida_dir, fullpath, log_file, module_dir + "/server.py", str(port) ])

def connect_ida(port):
	ida = rpyc.classic.connect("localhost", port)

	idc = ida.root.getmodule("idc")
	idaapi = ida.root.getmodule("idaapi")
	idautils = ida.root.getmodule("idautils")

	return IDA(ida, idc, idaapi, idautils)

def make_idalink(filename, connect_retries=60):
	port = random.randint(40000, 49999)
	spawn_ida(filename, port)

	while connect_retries:
		try:
			time.sleep(1)
			l.debug("Trying to connect to IDA on port %d" % port)
			return connect_ida(port)
		except Exception:
			l.debug("... failed. Retrying.")

		connect_retries -= 1

	raise IDALinkError("Failed to connect to IDA on port %d for filename %s" % (port, filename))
{'fraction_non_alphanumeric': 0.14755295836376917, 'fraction_numerical': 0.009861212563915266, 'mean_word_length': 4.877682403433476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4780988', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1006, 'n_words': 241}