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 3.8KB

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