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.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

executor.py 4.1KB

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