util.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (c) 2015, Nordic Semiconductor
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # * Neither the name of Nordic Semiconductor ASA nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. from random import randint
  29. import time
  30. import sys
  31. import math
  32. ON_POSIX = 'posix' in sys.builtin_module_names
  33. def process_pipe(pipe, queue):
  34. for line in iter(pipe.readline, b''):
  35. queue.put({'type': 'output', 'data': line})
  36. pipe.close()
  37. queue.put({'type': 'output_terminated'})
  38. def kill_process(target):
  39. if 'proc' in target:
  40. target['proc'].kill()
  41. # Close file descriptors
  42. target['proc'].stdin.close()
  43. time.sleep(1) # Let the application terminate before proceeding
  44. def kill_processes(context):
  45. targets = context.target_registry.get_all()
  46. for target in targets:
  47. kill_process(target)
  48. def generate_options_table_for_cucumber():
  49. retval = ""
  50. number_of_2_option_options = 1
  51. number_of_3_option_options = 4
  52. number_of_4_option_options = 1
  53. number_of_optional_option_permutations = 1
  54. number_of_optional_option_permutations *= int(math.pow(2, number_of_2_option_options))
  55. number_of_optional_option_permutations *= int(math.pow(3, number_of_3_option_options))
  56. number_of_optional_option_permutations *= int(math.pow(4, number_of_4_option_options))
  57. for x in xrange(0, number_of_optional_option_permutations):
  58. retval += "{0:<8}".format(" ")
  59. retval += "| {0:<12}| {1:<29}| {2:<29}|".format("blinky.bin", "not_set", "not_set")
  60. permutation_name = ""
  61. options_factor = 1
  62. option = int(x / options_factor % 3)
  63. options_factor *= 3
  64. permutation_name = str(option) + permutation_name
  65. if option == 0:
  66. retval += " {0:<8}|".format("none")
  67. if option == 1:
  68. retval += " {0:<8}|".format("not_set")
  69. if option == 2:
  70. retval += " {0:<8}|".format("0x{0:02x}".format(randint(0, 255)))
  71. option = int(x / options_factor % 3)
  72. options_factor *= 3
  73. permutation_name = str(option) + permutation_name
  74. if option == 0:
  75. retval += " {0:<8}|".format("none")
  76. if option == 1:
  77. retval += " {0:<8}|".format("not_set")
  78. if option == 2:
  79. retval += " {0:<8}|".format("0x{0:02x}".format(randint(0, 255)))
  80. option = int(x / options_factor % 3)
  81. options_factor *= 3
  82. permutation_name = str(option) + permutation_name
  83. if option == 0:
  84. retval += " {0:<9}|".format("none")
  85. if option == 1:
  86. retval += " {0:<9}|".format("not_set")
  87. if option == 2:
  88. retval += " {0:<9}|".format("0x{0:02x}".format(randint(0, 255)))
  89. option = int(x / options_factor % 4)
  90. options_factor *= 4
  91. permutation_name = str(option) + permutation_name
  92. if option == 0:
  93. retval += " {0:<8}|".format("not_set")
  94. if option == 1:
  95. retval += " {0:<8}|".format("0.5")
  96. if option == 2:
  97. retval += " {0:<8}|".format("0.6")
  98. if option == 3:
  99. retval += " {0:<8}|".format("0.7")
  100. option = int(x / options_factor % 3)
  101. options_factor *= 3
  102. permutation_name = str(option) + permutation_name
  103. if option == 0:
  104. retval += " {0:<28}|".format("none")
  105. if option == 1:
  106. retval += " {0:<28}|".format("not_set")
  107. if option == 2:
  108. sd_reqs = []
  109. for i in xrange(0, randint(1, 4)):
  110. sd_reqs.append("0x{0:04x}".format(randint(0, 65535)))
  111. retval += " {0:<28}|".format(",".join(sd_reqs))
  112. option = int(x / options_factor % 2)
  113. permutation_name = str(option) + permutation_name
  114. if option == 0:
  115. retval += " {0:<9}|".format("not_set")
  116. if option == 1:
  117. retval += " {0:<9}|".format("test.pem")
  118. retval += " {0:<15}|".format("100_{0:0>6}.zip".format(permutation_name))
  119. retval += "\n"
  120. return retval