def send_files(files, address): socket = net.Socket() socket.connect(address) for path in files: try: send_file(path, socket) except: pass socket.close()
nargs='*', help='SMT files to submit. CLI mode if empty') args = parser.parse_args() components = args.host_port[0].split(':', maxsplit=1) if len(components) == 1: components = ['127.0.0.1'] + components try: address = (components[0], int(components[1])) except: print('invalid host:port', file=sys.stderr) sys.exit(1) try: socket = net.Socket() socket.connect(address) if args.files: for path in args.files: try: utils.send_file(path, socket) except FileNotFoundError: print('File not found: {}'.format(path), file=sys.stderr) else: history_file = pathlib.Path.home() / '.smts_client_history' if sys.stdin.isatty() and history_file.is_file(): readline.read_history_file(str(history_file.resolve())) while True: try: if sys.stdin.isatty(): line = input('{}:{}> '.format(*socket.remote_address))
def solver(address, w_sat, w_unsat, w_timeout, max): def solve_process(sock, name, node): sys.stderr = open(os.devnull, 'w') status = solve(w_sat, w_unsat, w_timeout, max) sock.write({'name': name, 'node': node, 'report': status}, '') def get_cnf(): nonlocal clauses filename_smt = str(utils.TempFile()) open(filename_smt, 'w').write('{}{}'.format(smt.decode() if smt else '', query if query else '')) subprocess.Popen( [config.build_path + '/solver_opensmt', '-d', filename_smt]).wait() filename_cnf = filename_smt + '.cnf' cnf_string = open(filename_cnf).read() clauses = utils.smt2json(cnf_string) return filename_smt, filename_cnf, cnf_string p = name = node = query = smt = clauses = None sock = net.Socket() sock.connect(address) sock.write({'solver': 'dummy solver'}, '') while True: try: header, message = sock.read() except ConnectionAbortedError: break if p and header['command'] == 'stop': p.terminate() p = name = node = query = smt = clauses = None elif header['command'] in ['solve', 'incremental']: if p: p.terminate() name = header['name'] node = header['node'] if 'node_' not in header else header['node_'] query = header['query'] if 'query' in header else '' smt = message if header['command'] == 'solve' else smt + message logging.info('solving {}'.format(name + node)) p = multiprocessing.Process(target=solve_process, args=(sock, name, node)) p.daemon = True p.start() elif header['command'] == 'partition': logging.info('creating {} partitions'.format(header['partitions'])) message = '\0'.join( ['true' for _ in range(int(header['partitions']))]) sock.write({ 'name': name, 'node': node, 'report': 'partitions' }, message) elif header['command'] in 'cnf-clauses': # run solver_opensmt with -d # and send back the clauses if 'query' in header: query = header['query'] smt = message filename_smt, filename_cnf, cnf_string = get_cnf() logging.info('sending CNF {} of {}'.format(filename_cnf, filename_smt)) header["report"] = header["command"] header.pop('command') sock.write(header, cnf_string) elif header['command'] == 'cnf-learnts': if clauses is None: get_cnf() learnts = [] for _ in range(int(len(clauses[0]) / 10)): learnt = ['or'] two_clauses = random.choices(clauses[0], k=2) for clause in two_clauses: learnt.append(random.choices(clause[1:])[0]) learnts.append(learnt) logging.info('sending {} random learnts'.format(len(learnts))) learnts = [learnts] header["report"] = header["command"] header.pop('command') sock.write(header, utils.json2smt(learnts)) else: print(header, message)