Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

executor.py 3.8KB

  1. import shutil
  2. import os, re
  3. import sys
  4. from .server.servicethread import OneShotServiceThread
  5. from .config import (
  6. MODULE_PATH,
  7. ELMO_TOOL_REPOSITORY,
  8. ELMO_INPUT_FILE_NAME,
  9. DEFAULT_HOST,
  10. DEFAULT_PORT,
  11. )
  12. from .project_base import SimulationProject
  13. from .manage import execute_simulation
  14. from .utils import Color
  15. class Executor(OneShotServiceThread):
  16. def execute(self):
  17. data = self.protocol.get_data()
  18. self.protocol.please_assert(data)
  19. elmo_path = os.path.join(MODULE_PATH, ELMO_TOOL_REPOSITORY)
  20. # Set the input of ELMO
  21. self.protocol.please_assert('input' in data)
  22. with open(os.path.join(elmo_path, ELMO_INPUT_FILE_NAME), 'w') as _input_file:
  23. _input_file.write(data['input'])
  24. self.protocol.send_ack()
  25. # Get the binary
  26. binary_content = self.protocol.get_file()
  27. binary_path = os.path.join(elmo_path, 'project.bin')
  28. with open(binary_path, 'wb') as _binary_file:
  29. _binary_file.write(binary_content)
  30. self.protocol.send_ack()
  31. ### Generate the traces by launching ELMO
  32. print(Color.OKGREEN + ' - Simulation accepted...' + Color.ENDC)
  33. simulation = SimulationProject()
  34. simulation.get_binary_path = lambda: os.path.abspath(binary_path)
  35. data = execute_simulation(simulation)
  36. if data['error']:
  37. print(Color.FAIL + ' - Simulation failed.' + Color.ENDC)
  38. self.protocol.send_data(results)
  39. self.protocol.close()
  40. return
  41. print(Color.OKGREEN + ' - Simulation finished: {} traces, {} instructions'.format(
  42. data['nb_traces'],
  43. data['nb_instructions'],
  44. ) + Color.ENDC)
  45. output_path = os.path.join(elmo_path, 'output')
  46. ### Get the trace
  47. data['results'] = []
  48. for i in range(data['nb_traces']):
  49. filename = os.path.join(output_path, 'traces', 'trace%05d.trc' % (i+1))
  50. with open(filename, 'r') as _file:
  51. data['results'].append(
  52. list(map(float, _file.readlines()))
  53. )
  54. ### Get asmtrace and printed data
  55. asmtrace = None
  56. if ('asmtrace' not in data) or data['asmtrace']:
  57. with open(os.path.join(output_path, 'asmoutput', 'asmtrace00001.txt'), 'r') as _file:
  58. data['asmtrace'] = _file.read()
  59. printed_data = None
  60. if ('printdata' not in data) or data['printdata']:
  61. with open(os.path.join(output_path, 'printdata.txt'), 'r') as _file:
  62. data['printed_data'] = list(map(lambda x: int(x, 16), _file.readlines()))
  63. ### Send results
  64. print(Color.OKCYAN + ' - Sending results...' + Color.ENDC, end='')
  65. sys.stdout.flush()
  66. self.protocol.send_data(data)
  67. print(Color.OKGREEN + ' Sent!' + Color.ENDC)
  68. self.protocol.close()
  69. def launch_executor(host=DEFAULT_HOST, port=DEFAULT_PORT, waiting_function=True):
  70. from .server.servicethread import ListeningThread
  71. def do_main_program():
  72. thread = ListeningThread(host, port, Executor, debug=True)
  73. thread.start()
  74. return thread
  75. def program_cleanup(signum, frame):
  76. thread.stop()
  77. thread = do_main_program()
  78. import signal
  79. signal.signal(signal.SIGINT, program_cleanup)
  80. signal.signal(signal.SIGTERM, program_cleanup)
  81. if waiting_function is True:
  82. import time
  83. while thread.is_running():
  84. time.sleep(1)
  85. return
  86. return_value = None
  87. if waiting_function:
  88. return_value = waiting_function()
  89. if thread.is_running():
  90. program_cleanup(None, None)
  91. return return_value